I'm trying to create a bunch of symbolic links, but I can't figure out why this is working
ln -s /Users/niels/something/foo ~/bin/foo_link
while this
cd /Users/niels/something
ln -s foo ~/bin/foo_link
is not.
I believe it has something to do with foo_link
linking to foo in /Users/niels/bin
instead of /Users/niels/something
So the question is, how do I create a symbolic link that points to an absolute path, without actually typing it?
For reference, I am using Mac OS X 10.9 and Zsh.
Best Answer
The easiest way to link to the current directory as an absolute path, without typing the whole path string would be
The
target
(first) argument for theln -s
command works relative to the symbolic link's location, not your current directory. It helps to know that, essentially, the created symlink (the second argument) simply holds the text you provide for the first argument.Therefore, if you do the following:
and then move that link around
you will see that
foo_link
tries to point tofoo
in the directory it is residing in. This also works with symbolic links pointing to relative paths. If you do the following:and then move
yet_another_link
to another directory and check where it points to, you'll see that it always points to../foo
. This is the intended behaviour, since many times symbolic links might be part of a directory structure that can reside in various absolute paths.In your case, when you create the link by typing
foo_link
just holds a link tofoo
, relative to its location. Putting$(pwd)
in front of the target argument's name simply adds the current working directory's absolute path, so that the link is created with an absolute target.