linux - 用于存放 Linux 配置文件的 Bash 脚本不适用于主目录或现有文件夹根目录中的文件

标签 linux bash config symlink dotfiles

基本上,我正在尝试使用 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

最佳答案

你从哪个目录调用这个脚本?您的 cd 似乎无法正常工作,因为您正在尝试更改为 .dotfilesfor 循环正在使用名为 的目录点文件(无前导.)。


警告的含义

您的主目录中似乎有 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.

请注意:

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.


长见识

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

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

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

关于linux - 用于存放 Linux 配置文件的 Bash 脚本不适用于主目录或现有文件夹根目录中的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45783937/

相关文章:

linux - 英特尔处理器如何访问 Branch Trace Store 缓冲区?

linux - 包含特定字符串的 grep 行(一行最多可以写 3 行)

linux - 'find -exec' Linux 中的一个 shell 函数

bash - 带有 Bash 的 Windows 10 上的 Jenkins

rust - 为什么我使用 config-rs 时收到 "missing field"错误?

visual-studio - 如何让 Visual Studio 编译器忽略文件?

linux - VirtualBox VM 中的 VirtualBox VM

linux - 应用程序 'appname' 无法在打开的 shift Node 应用程序上启动(端口 8080 不可用)

bash - 在 Bash 中循环两个关联数组

path - 我如何找出 rubocop 使用的是哪个配置文件?