Does ln -s require you to be in a certain directory?
Solution 1
If you are not in the same directory as the target you must make sure you use the absolute path.
ln -s foo/bar baz/quux # fails
ln -s ~/foo/bar baz/quux # succeeds
Solution 2
I tend to use
ln -s $(pwd)/local_file /path/to/link
when I need to link a local file.
Solution 3
This is always unintutive to me. I get in the habit of doing:
ln -s $(readlink -f /path/to/file) /path/to/link
which should work but at the cost of making it non-relative in the actual on-disk link.
Related videos on Youtube
evanrmurphy
Updated on September 18, 2022Comments
-
evanrmurphy almost 2 years
I want to create a simple symlink to a file.
It works perfectly if I run the command from inside the directory where I want the symlink to be created:
/path/to/link $ ln -s /path/to/file .
But if I'm in any other directory, it creates a broken link every time:
/any/other/path $ ln -s /path/to/file /path/to/link
Does
ln -s
require to you to be in a certain directory, or am I missing something? Running Ubuntu 10.04.Update: Sorry for the confusion about whether the paths were relative or absolute. They were relative, and as several mentioned in their answers this was the source of my problem. Thanks!
-
Matthew Flaschen over 12 yearsIt's a bad example, but I'm sure in the real one /path/to/file is relative.
-
Croad Langshan over 12 yearsSufficiently recent GNU coreutils has an -e flag to coreutils that errors if the target does not exist -- I use that in place of -f. I also find ln confusing, and tend to use the -T flag also.
-
Matthew Flaschen over 12 years@CroadLangshan, thanks. I wasn't aware of that
readlink
option.