116

I'm trying to create a symbolic link in my home directory that points to a directory on my external HDD.

It works fine when I specify it like this:

cd ~
ln -s /run/media/name/exhdd/Data/ Data

However it creates a faulty link when I try this:

cd /run/media/name/exhdd
ln -s Data/ ~/Data

This creates a link that I cannot cd into.

When I try, bash complains:

bash: cd: Data: Too many levels of symbolic links

The Data symbolic link in my home is also colored in red when ls is set to display colored output.

Why is this happening? How can I create a link in that manner? (I want to create a symlink to a directory in my working directory in another directory.)


Edit: according to this StackOverflow answer, if the second argument (in my case that'd be ~/Data) already exists and is a directory, ln will create a symlink to the target inside that directory.

However, I'm experiencing the same issue with:

ln -s Data/ ~/
4
  • 9
    Just a random tip: cd ~ is usually the same as cd.
    – user26112
    Jul 23, 2013 at 22:50
  • 3
    ls -l ~/Data would have helped you see what was wrong with the "red" link.
    – msw
    Jul 23, 2013 at 22:58
  • Hm, didn't know that I could do that, thanks. I remember trying to cat it, but I forgot what was the result... (I'm not home at them moment.)
    – jcora
    Jul 24, 2013 at 7:02
  • Side note, suppose you want to only create a symlink of all files, dir and subdir inside a particular folder, say, all items inside /run/media/name/exhdd/Data/ to Data then use the following ln -s /run/media/name/exhdd/Data/* Data Mar 9, 2016 at 16:42

4 Answers 4

120

Here's what's happening. If you make a symlink with a relative path, the symlink will be relative. Symlinks just store the paths that you give them. They never resolve paths to full paths. Running

$ pwd
/usr/bin
$ ln -s ls /usr/bin/ls2

creates a symlink named ls2 in /usr/bin to ls(viz. /usr/bin/ls) relative to the directory that the symlink is in (/usr/bin). The above command would create a functional symlink from any directory.

$ pwd
/home/me
$ ln -s ls /usr/bin/ls2

If you moved the symlink to a different directory, it would cease to point to the file at /usr/bin/ls.

You are making a symlink that points to Data, and naming it Data. It is pointing to itself. You have to make a symlink with the absolute path of the directory.

ln -s "$(realpath Data)" ~/Data
0
20

I was having the same problem. Google led to this answer but the simplest solution is not documented here:

ln -sT 

-T does the trick

man ln:

-T, --no-target-directory
    treat LINK_NAME as a normal file always

Just adding this here so anyone with the same question may find this :)

17

ln's behavior with relative paths is unintuitive. To restore sanity, use the -r flag.

cd /run/media/name/exhdd
ln -sr Data/ ~/Data

Explanation:

   -r, --relative
          create symbolic links relative to link location

What it means is that ln will do what you expect. It will take into account what directory you are in, what directory the target is in, and construct a path relative to the directory the link will be in. The default behavior (without -r) is to interpret the first parameter (target) literally, in which case you have to construct the path yourself so that it is valid at the link's directory.

Alternatively, use an absolute path, as mentioned by @SmithJohn

ln -s "$(realpath Data)" ~/Data #bash shell

or

ln -s "(realpath Data)" ~/Data #fish shell
-1

Very simply you can create link for a folder. Look my command

ln -s /home/targatedfolder /var/www/html/link_name

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .