My goals are to:
- Do it automatically (without adding a line to my script every time I add or change the location of a config file)
- The files in the git repo folder should not be hidden, but the symlinks in my home directory should
- Existing files of the same name should be overwritten, but not directories. For example if I have
config/conf.txt
in my dotfiles repo, it shouldn't overwrite the entire~/.config
folder, but simply add a link toconf.txt
inside~/.config
.
My current solution does these, but it could probably be done better:
DOTFILES_DIR="$HOME/dotfiles"
echo -n Creating symlinks...
shopt -s extglob nocaseglob
# Create hidden symlinks, hard link them to $HOME, rm old symlinks
for dotfile in $DOTFILES_DIR/!(readme*|${0##*/}); do
hidden="$DOTFILES_DIR/.${dotfile##*/}"
cp -asf "$dotfile" "$hidden"
cp -alf "$hidden" "$HOME"
rm -r "$hidden"
done
echo done
I think I should be able to copy the files directly to the home directory, but I had some trouble getting that to work right if a destination folder already existed.
Edit: I was able to eliminate the second for loop. Much better. There may still be a better way, though.
cp
is for copying, not symlinking (look atln
) \$\endgroup\$cp -s
creates symlinks. I usecp
because the-a
option preserves the directory structure (point 3 above) \$\endgroup\$