Bash script to symlink configuration files from git repo into home directory

My goals are to:

1. Do it automatically (without adding a line to my script every time I add or change the location of a config file)
2. The files in the git repo folder should not be hidden, but the symlinks in my home directory should
3. 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 to conf.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.

• Do not that cp is for copying, not symlinking (look at ln) – D. Ben Knoble Oct 11 '17 at 4:02
• cp -s creates symlinks. I use cp because the -a option preserves the directory structure (point 3 above) – pauloue Oct 11 '17 at 4:32
• Interesting... need to go read my man pages again! – D. Ben Knoble Oct 11 '17 at 4:48