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!

migrated from stackoverflow.com Feb 5 '12 at 8:20

This question came from our site for professional and enthusiast programmers.


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

I tend to use

ln -s $(pwd)/local_file /path/to/link

when I need to link a local file.


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.

  • Sufficiently 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. – Croad Langshan Feb 5 '12 at 15:02
  • @CroadLangshan, thanks. I wasn't aware of that readlink option. – Matthew Flaschen Feb 6 '12 at 1:59

No, there is no such requirement:

aix@aix:/tmp$ cd /tmp
aix@aix:/tmp$ mkdir test_path
aix@aix:/tmp$ echo test > test_path/file.txt
aix@aix:/tmp$ ln -s /tmp/test_path/file.txt /tmp/test_path/link.txt
aix@aix:/tmp$ cat /tmp/test_path/link.txt

Make sure you have't mistyped any of the paths, and that you're using absolute paths everywhere (as your example indicates).

  • It's a bad example, but I'm sure in the real one /path/to/file is relative. – Matthew Flaschen Feb 5 '12 at 8:21
  • Yes, the real path was relative. Sorry for the confusion. I just updated the OP to clarify this. – evanrmurphy Feb 5 '12 at 23:54

It's not really about which directory you're in, in itself. It's about what symlink you make: symlinks are just files that contain a path that points to a file. You can make symlinks that contain paths that start with the / character, or symlinks that don't start with that character. You can create symlinks that contain .. path components, and symlinks that don't. When programs resolve symlinks to find the target file, they interpret the path as being relative to the directory that contains the symlink. So you just need to make sure the symlink has the path that you want.

Here's one way that's similar to Matthew Flaschen's answer, but here making a relative symlink:

/tmp$ mkdir -p spam/eggs/ham
/tmp$ mkdir foo
/tmp$ python -c "import os, sys; print os.path.relpath(sys.argv[1], os.path.abspath(sys.argv[2])), sys.argv[2]" $(readlink -e foo) spam/eggs/ham  | xargs ln -s 
/tmp$ ls -l spam/eggs/ham/foo
lrwxrwxrwx 1 user user 12 2012-02-05 15:12 spam/eggs/ham/foo -> ../../../foo

No doubt that could be done in pure Bourne / bash shell too, but it's easy to write (and read) this correctly in python.

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.