Create a symbolic link relative to the current directory

484,401

Solution 1

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 :)

Solution 2

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
Share:
484,401

Related videos on Youtube

jcora
Author by

jcora

flowing.systems

Updated on September 18, 2022

Comments

  • jcora
    jcora almost 2 years

    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/ ~/
    
    • Admin
      Admin almost 11 years
      Just a random tip: cd ~ is usually the same as cd.
    • Admin
      Admin almost 11 years
      ls -l ~/Data would have helped you see what was wrong with the "red" link.
    • Admin
      Admin almost 11 years
      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.)
    • Admin
      Admin over 8 years
      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