Create a symbolic link relative to the current directory

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/ ~/
Asked By: jcora


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
$ 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
$ 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
Answered By: user26112

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 🙂

Answered By: tiegs

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

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

Answered By: Bhupendra Sahu

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

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


   -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


ln -s "(realpath Data)" ~/Data #fish shell
Answered By: Aleksandr Dubinsky
Categories: Answers Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.