0

Say I have a folder ~/dot containing some files and directories, such as zshrc, Xresources, and emacs.d. How do I create symlinks to all of those in ~, but such that the symlinks begin with a dot (.zshrc &c.)?

And how would I remove all already existing symlinks that would have been created by the answer to the first question? (I.e. how would I uninstall my dotfiles.)

2

creating the symlinks

cd ~/dot
for file in *; do
    ln -sf dot/"$file" ~/."$file"
done

deleting the symlinks

cd
for dotfile in .*; do
    test -L || continue
    target="$(readlink "$dotfile")"
    [[ $target =~ ^dot/ ]] && echo rm "$dotfile"
done
  • wouldn't it be simpler to do as in the first, but test if ~/."$file is a symlink before unlinking? – Toothrot Dec 3 '17 at 23:02
  • 1
    @Toothrot That would be an option if you can be sure that files in dot are neither deleted nor renamed. – Hauke Laging Dec 3 '17 at 23:06
0

Since you're mentioning zsh:

autoload zmv # best in ~/.zshrc:
(cd && zmv -sL -v 'dot/(*)' '.$1')

To remove them:

(cd && files=(dot/*(:t)) && rm -vf -- .$^files(N@))

(remove the -v for verbose if your rm implementation doesn't support it)

For each dot/foo file, that removes .foo if it's a symlink (whether it points to to dot/foo or not). Alternatively, you could do:

rm .*(@e'<[[ $REPLY -ef dot/${REPLY#.} ]]>')

That is remove the .foo symlinks if it's the same file (after symlink resolution) as old/foo.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.