0

I'm trying to manage my dot files inside a sub directory of $HOME and write a shell script that sets up symbolic links inside $HOME.

#!/bin/sh

for i in .cvsignore .gitconfig .npmrc .tmux.conf .zprofile .zshrc; do
    ln -s $i ~/$i
done

for some reason this script keeps producing broken symbolic links.

file {~/*,~/.*} | grep "broken symbolic link" | cut -d: -f1

/home/user/.cvsignore
/home/user/.gitconfig
/home/user/.npmrc
/home/user/.tmux.conf
/home/user/.zprofile
/home/user/.zshrc
1

The symbolic links will be broken because they will look like

.cvsignore -> .cvsignore

etc.

When you create a symbolic link with

ln -s source target

then the target will be a symbolic link that points to source exactly the way you wrote it.

So, what's missing is the path to the source (here I'm using the $PWD variable which holds the absolute path to the current directory):

ln -s "$PWD/$i" "$HOME/$i"

Most of the time, one is located in the directory of the target when creating the link:

ln -s some/dir/where/the/file/is mylink

The link (the path that the link points to) should be absolute, or be relative to the location of the link.

  • Could you explain to me why ln can't figure out the target from the relative path name? – Philippe Oct 2 '17 at 8:09
  • @Philippe The source path (the first argument to ln), if it's a relative path, needs to be relative to the target (the second argument). – Kusalananda Oct 2 '17 at 8:11
  • Ok, I still don't understand why I can't use a relative target path and an absolute link name path – Philippe Oct 2 '17 at 8:12
  • @Philippe You mean like in ln -s "$PWD/file" ../../file? That should not be a problem... – Kusalananda Oct 2 '17 at 8:14
  • No, I meant the other way round. – Philippe Oct 2 '17 at 8:14
0

Are you standing in the same directory (ie. home of that user)? If yes, it would be linking to itself...

If you are in different directory, you'd need to specify full path to the target.

  • He's not. He is in a subdirectory (see first sentence). – Kusalananda Oct 2 '17 at 7:25

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.