ln -s with a path relative to pwd
Solution 1
The easiest way to link to the current directory as an absolute path, without typing the whole path string would be
ln -s "$(pwd)/foo" ~/bin/foo_link
The target
(first) argument for the ln -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:
cd some_directory
ln -s foo foo_link
and then move that link around
mv foo_link ../some_other_directory
ls -l ../some_other_directory
you will see that foo_link
tries to point to foo
in the directory it is residing in. This also works with symbolic links pointing to relative paths. If you do the following:
ln -s ../foo yet_another_link
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
ln -s foo ~/bin/foo_link
foo_link
just holds a link to foo
, 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.
Solution 2
Using the -r
(--relative
) flag will make this work:
ln -sr foo ~/bin/foo_link
Solution 3
To save some typing, you can do
ln -s "$PWD"/foo ~/bin/foo_link
Related videos on Youtube
Niels B.
I am a junior Ruby on Rails developer from Copenhagen, Denmark. Feel free to checkout my public repositories at GitHub: https://github.com/nielsbuus So far you'll find: A JavaScript utility class for reading and setting query parameters A JavaScript utility function for conveniently turning HTML strings into DOM objects.
Updated on September 18, 2022Comments
-
Niels B. almost 2 years
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.
-
Niels B. about 10 yearsNope, doesn't do the trick.
-
Wildcard over 8 years"...helps to imagine that the created symlink simply holds text...." Isn't this the literal truth?
-
jlliagre over 8 yearsBeware that
-r
is a GNUism, i.e. non POSIX so won't work in the OP case as the standard OS Xln
command is BSD based. -
Achilleas over 8 yearsIt is. Perhaps I could change imagine to "know" or "understand".
-
cristoper about 6 yearsIn linux (gnu ln) the man page calls the first argument
target
and the secondlink
(man7.org/linux/man-pages/man1/ln.1.html). But in BSD (including OS X) the first is calledsource
and the secondtarget
(freebsd.org/cgi/man.cgi?ln). Quite confusing. -
Stéphane Chazelas almost 5 years@Freddy, it depends on the shell. In fish or zsh, it would be OK. In Bourne-like shells including bash and with the default value of $IFS, SPC would indeed be a problem, but also TAB, NL and all globbing characters (at least
*
,?
and[...]
). -
Freddy almost 5 years@StéphaneChazelas Yes, you're right and the question is tagged with
bash
, so it's a generally a good idea to quote it. My bad, I should have been more specific.