【问题标题】:Bash script to Stow Linux configuration files not working with files in root of home or existing folders用于存放 Linux 配置文件的 Bash 脚本不适用于家庭根目录或现有文件夹中的文件
【发布时间】:2017-08-20 15:41:55
【问题描述】:

基本上,我正在尝试使用GNU Stow 轻松地将我的点文件/配置文件同步到我的主目录。每个配置都存储在相对于我的主目录的命名文件夹中。例如,vim/.vimrci3/.config/i3/config。其中一些文件夹有多个文件。

脚本

#!/usr/bin/env bash

# This script will Stow all the dotfiles located in the ~/dotfiles folder.
# The -R flag is used to force a 'restow' which will remove any existing symlinks before attempting to Stow.

echo "Stowing Dotfiles...";

cd ~/.dotfiles
for file in ~/dotfiles/*; do
  # Only run Stow on the directories in the dotfiles folder and not the individual files.
  # Using 'basename' strips the filepath from the directory name. 
  if [ -d ${file} ]; then
    stow -R $(basename $file)
    echo "$(basename $file) stowed."; 
  fi
done

# Return back to the your PWD from before you ran the script
cd ~- 

echo 'All stowed';

当我运行此程序时,~/.config 文件夹中的某些文件或目录被存放。但是,如果文件存在,它似乎不会被收起。此外,我的主目录根目录中的文件似乎没有被存放,例如bash/.bashrc 和上述目录中的其他文件。

输出

Stowing Dotfiles...
stow_all.sh: line 8: cd: /home/devon/.dotfiles: No such file or directory
WARNING! unstowing bash would cause conflicts:
  * existing target is neither a link nor a directory: .bash_aliases
  * existing target is neither a link nor a directory: .bashrc
WARNING! stowing bash would cause conflicts:
  * existing target is neither a link nor a directory: .bash_aliases
  * existing target is neither a link nor a directory: .bashrc
All operations aborted.
bash stowed.
bin stowed.
compton stowed.
WARNING! unstowing fonts would cause conflicts:
  * existing target is neither a link nor a directory: .local/share/fonts/Roboto Mono Bold Italic for Powerline.ttf
  * existing target is neither a link nor a directory: .local/share/fonts/Roboto Mono Bold for Powerline.ttf
  * existing target is neither a link nor a directory: .local/share/fonts/Roboto Mono Italic for Powerline.ttf
  * existing target is neither a link nor a directory: .local/share/fonts/Roboto Mono Light Italic for Powerline.ttf
  * existing target is neither a link nor a directory: .local/share/fonts/Roboto Mono Light for Powerline.ttf
  * existing target is neither a link nor a directory: .local/share/fonts/Roboto Mono Medium Italic for Powerline.ttf
  * existing target is neither a link nor a directory: .local/share/fonts/Roboto Mono Medium for Powerline.ttf
  * existing target is neither a link nor a directory: .local/share/fonts/Roboto Mono Thin Italic for Powerline.ttf
  * existing target is neither a link nor a directory: .local/share/fonts/Roboto Mono Thin for Powerline.ttf
  * existing target is neither a link nor a directory: .local/share/fonts/Roboto Mono for Powerline.ttf
  * existing target is neither a link nor a directory: .local/share/fonts/RobotoMono-Bold.ttf
  * existing target is neither a link nor a directory: .local/share/fonts/RobotoMono-BoldItalic.ttf
  * existing target is neither a link nor a directory: .local/share/fonts/RobotoMono-Italic.ttf
  * existing target is neither a link nor a directory: .local/share/fonts/RobotoMono-Light.ttf
  * existing target is neither a link nor a directory: .local/share/fonts/RobotoMono-LightItalic.ttf
  * existing target is neither a link nor a directory: .local/share/fonts/RobotoMono-Medium.ttf
  * existing target is neither a link nor a directory: .local/share/fonts/RobotoMono-MediumItalic.ttf
  * existing target is neither a link nor a directory: .local/share/fonts/RobotoMono-Regular.ttf
  * existing target is neither a link nor a directory: .local/share/fonts/RobotoMono-Thin.ttf
  * existing target is neither a link nor a directory: .local/share/fonts/RobotoMono-ThinItalic.ttf
WARNING! stowing fonts would cause conflicts:
  * existing target is neither a link nor a directory: .local/share/fonts/Roboto Mono Bold Italic for Powerline.ttf
  * existing target is neither a link nor a directory: .local/share/fonts/Roboto Mono Bold for Powerline.ttf
  * existing target is neither a link nor a directory: .local/share/fonts/Roboto Mono Italic for Powerline.ttf
  * existing target is neither a link nor a directory: .local/share/fonts/Roboto Mono Light Italic for Powerline.ttf
  * existing target is neither a link nor a directory: .local/share/fonts/Roboto Mono Light for Powerline.ttf
  * existing target is neither a link nor a directory: .local/share/fonts/Roboto Mono Medium Italic for Powerline.ttf
  * existing target is neither a link nor a directory: .local/share/fonts/Roboto Mono Medium for Powerline.ttf
  * existing target is neither a link nor a directory: .local/share/fonts/Roboto Mono Thin Italic for Powerline.ttf
  * existing target is neither a link nor a directory: .local/share/fonts/Roboto Mono Thin for Powerline.ttf
  * existing target is neither a link nor a directory: .local/share/fonts/Roboto Mono for Powerline.ttf
  * existing target is neither a link nor a directory: .local/share/fonts/RobotoMono-Bold.ttf
  * existing target is neither a link nor a directory: .local/share/fonts/RobotoMono-BoldItalic.ttf
  * existing target is neither a link nor a directory: .local/share/fonts/RobotoMono-Italic.ttf
  * existing target is neither a link nor a directory: .local/share/fonts/RobotoMono-Light.ttf
  * existing target is neither a link nor a directory: .local/share/fonts/RobotoMono-LightItalic.ttf
  * existing target is neither a link nor a directory: .local/share/fonts/RobotoMono-Medium.ttf
  * existing target is neither a link nor a directory: .local/share/fonts/RobotoMono-MediumItalic.ttf
  * existing target is neither a link nor a directory: .local/share/fonts/RobotoMono-Regular.ttf
  * existing target is neither a link nor a directory: .local/share/fonts/RobotoMono-Thin.ttf
  * existing target is neither a link nor a directory: .local/share/fonts/RobotoMono-ThinItalic.ttf
All operations aborted.
fonts stowed.
WARNING! unstowing git would cause conflicts:
  * existing target is neither a link nor a directory: .gitconfig
WARNING! stowing git would cause conflicts:
  * existing target is neither a link nor a directory: .gitconfig
All operations aborted.
git stowed.
gtk stowed.
i3 stowed.
WARNING! unstowing ncmpcpp would cause conflicts:
  * existing target is neither a link nor a directory: .config/ncmpcpp/config
WARNING! stowing ncmpcpp would cause conflicts:
  * existing target is neither a link nor a directory: .config/ncmpcpp/config
All operations aborted.
ncmpcpp stowed.
WARNING! unstowing polybar would cause conflicts:
  * existing target is neither a link nor a directory: .config/polybar/config
  * existing target is neither a link nor a directory: .config/polybar/launch.sh
  * existing target is neither a link nor a directory: .config/polybar/updates.sh
  * existing target is neither a link nor a directory: .config/polybar/weather.py
WARNING! stowing polybar would cause conflicts:
  * existing target is neither a link nor a directory: .config/polybar/config
  * existing target is neither a link nor a directory: .config/polybar/launch.sh
  * existing target is neither a link nor a directory: .config/polybar/updates.sh
  * existing target is neither a link nor a directory: .config/polybar/weather.py
All operations aborted.
polybar stowed.
ranger stowed.
sound stowed.
WARNING! unstowing termite would cause conflicts:
  * existing target is neither a link nor a directory: .config/termite/config
WARNING! stowing termite would cause conflicts:
  * existing target is neither a link nor a directory: .config/termite/config
All operations aborted.
termite stowed.
WARNING! unstowing tmux would cause conflicts:
  * existing target is neither a link nor a directory: .tmux.conf
WARNING! stowing tmux would cause conflicts:
  * existing target is neither a link nor a directory: .tmux.conf
All operations aborted.
tmux stowed.
WARNING! unstowing vim would cause conflicts:
  * existing target is neither a link nor a directory: .tern-config
  * existing target is neither a link nor a directory: .vimrc
WARNING! stowing vim would cause conflicts:
  * existing target is neither a link nor a directory: .tern-config
  * existing target is neither a link nor a directory: .vimrc
All operations aborted.
vim stowed.
WARNING! unstowing xorg would cause conflicts:
  * existing target is neither a link nor a directory: .Xresources
  * existing target is neither a link nor a directory: .xinitrc
WARNING! stowing xorg would cause conflicts:
  * existing target is neither a link nor a directory: .Xresources
  * existing target is neither a link nor a directory: .xinitrc
All operations aborted.
xorg stowed.
All stowed

【问题讨论】:

  • 如果我的回答帮助你解决了你的问题,不妨考虑接受我的回答

标签: linux bash config symlink dotfiles


【解决方案1】:

您从哪个目录调用此脚本?您的 cd 似乎不起作用,因为您正试图更改为 .dotfilesfor 循环正在使用名为 dotfiles 的目录(没有前导 .)。


警告的含义

您的主目录中似乎有 plain 文件,您希望由stow 管理(链接)。

来自stow 手册页:

--adopt
    Warning!  This behaviour is specifically intended to alter the contents of your stow directory.  If you do not want that,
    this option is not for you.

    When stowing, if a target is encountered which already exists but is a plain file (and hence not owned by any existing stow
    package), then normally Stow will register this as a conflict and refuse to proceed.  This option changes that behaviour so
    that the file is moved to the same relative place within the package's installation image within the stow directory, and
    then stowing proceeds as before.  So effectively, the file becomes adopted by the stow package, without its contents
    changing.

请注意:

在存放时,如果遇到一个已经存在但为普通文件的目标(因此不属于任何现有的存放包),则通常 Stow 会将其注册为冲突并拒绝继续。


TLDR

  • 修复您的脚本以将 cdfor 循环指向同一目录:~/.dotfiles/~/dotfiles/

  • 如果您想将现有配置文件(在 ~ 中)移动到您的 dotfiles,请最初使用 stow --adopt(事先查看手册)

  • 如果要将配置文件从 dotfiles 移动到 ~,请从主文件夹中移动或删除(备份)现有文件

【讨论】:

    【解决方案2】:

    我尝试过和你一样的方法。这是我的尝试。

    cd 不适用于脚本。相反,您需要告诉 stow 从哪里存放而不是当前目录。

    我在自己的 dotfiles 中有一堆包,所以我遍历它们并收藏起来:

    for PACKAGE in shell emacs # ...
    do
      stow -R -v -d path/to/dotfiles -t ~ $PACKAGE
    done
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-04-01
      • 2018-10-29
      • 2020-11-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-26
      相关资源
      最近更新 更多