I successfully and not successfully created a symlink:

I have two directories




Now I have a file called baz.txt in this path ~/foo. My aim was to create a symlink in ~/bar so I can access it from there as well.

With following command which I executed in the directory ~/foo:

ln -s baz.txt ~/bar

I got a bad symlink which got marked red. The results weren't as I expected and I tried something else which eventually worked out for me.

I executed this command in the path ~/bar

ln -s ~/foo/baz.txt .

I wonder now why my first approach didn't work since I referenced my file to the directory where I want to have my symlink. It looks to me like I did the same thing in both commands just in different directories.


The issue here is absolute versus relative paths. An absolute path is /bar/foo/file.txt and a relative path is ./file.txt. When you run ln -s foo.txt /home/ubuntu/central_addcontent/program/, that is equivalent to running

ln -s ./foo.txt /home/ubuntu/central_addcontent/program/

This will create a file called foo.txt that is pointing to ./foo.txt in the target directory. This is simpler to illustrate with an example giving a different link name:

$ ln -s foo.txt /home/terdon/central_addcontent/program/bar.txt
$ ls -l /home/terdon/central_addcontent/program/
total 0
lrwxrwxrwx 1 terdon terdon 7 Nov 25 11:25 bar.txt -> foo.txt

As you can see above, bar.txt is pointing to /home/terdon/central_addcontent/program/foo.txt and that file does not exist. To do this correctly you can either use the full path of foo.txt or use the relative path but with respect to the target directory. Both of these will work:

$ ln -s ~/central_app/program/foo.txt ~/central_addcontent/program/
$ l /home/terdon/central_addcontent/program/
total 0
lrwxrwxrwx 1 terdon terdon 40 Nov 25 11:28 foo.txt -> /home/terdon/central_app/program/foo.txt


$ ln -s ../../central_app/program/foo.txt ~/central_addcontent/program/
$ ls -l /home/terdon/central_addcontent/program/
total 0
lrwxrwxrwx 1 terdon terdon 33 Nov 25 11:29 foo.txt -> ../../central_app/program/foo.txt

In the second example, I've used a relative path but with respect to the link's target.

  • So what I did in my first example was a reference to the same file over and over again ? – BoJack Horseman Nov 25 '14 at 10:39
  • 1
    @IbrahimApachi exactly. It was a link linking to itself. On my system, I can create the link but when I try to read it I get "Error: too many levels of symbolic links". It basically goes into an endless loop. – terdon Nov 25 '14 at 10:59

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.