Creating a symbolic link to a directory

22,888

Solution 1

Thanks everyone for your suggestions. I found it easiest to understand how to do it (though I still don't understand the logic behind it) with:

cd ~/workspace_b4a
cd android-sdk-linux
cd platform-tools
ln -sv ../build-tools/20.0.0/lib/ .

Solution 2

Assuming you are in directory /path/to/common/root:

$ pwd
/path/to/common/root

which has a subdirectory build-tools/20.0.0/lib:

$ readlink -e build-tools/20.0.0/lib
/path/to/common/root/build-tools/20.0.0/lib

and subdirectory platform-tools, but no file or directory platform-tools/lib:

$ ls -d platform-tools
platform-tools
$ ls platform-tools/lib
ls: cannot access platform-tools/lib: No such file or directory

and you want to make platform-tools/lib a symlink to build-tools/20.0.0/lib, then you can either make it an absolute link:

$ ln -s /path/to/common/root/build-tools/20.0.0/lib platform-tools/lib
$ ls -l platform-tools/lib
lrwxrwxrwx 1 user user 72 Sep 16 12:57 platform-tools/lib -> /path/to/common/root/build-tools/20.0.0/lib
$ readlink -e platform-tools/lib
/path/to/common/root/build-tools/20.0.0/lib

or you can make it a relative link:

$ ln -s ../build-tools/20.0.0/lib platform-tools/lib
$ ls -l platform-tools/lib
lrwxrwxrwx 1 user user 25 Sep 16 12:58 platform-tools/lib -> ../build-tools/20.0.0/lib
$ readlink -e platform-tools/lib
/path/to/common/root/build-tools/20.0.0/lib

while noting that the relative link must be relative to the symlink file itself, not to the location where you run the ln -s command. To make sure you don't get that wrong (and because you get convenient tab completion with it), there are two options:

Either go to the symlink's directory first, and create the symlink there:

$ cd platform-tools  
$ ln -s ../build-tools/20.0.0/lib lib  # The last lib is optional

Or use the -r option to make ln figure out the relative path while you specify paths relative to your current directory:

$ ln -sr build-tools/20.0.0/lib platform-tools/lib

Solution 3

You can link to a non existing PATH (either file or directory)

abox $ ls
abox $ ln -s foo bar
abox $ ls
bar  (<- in red)
abox $ ls -l
total 0
lrwxrwxrwx 1 archemar archemar 3 sept. 16 10:47 bar -> foo  ## both foo and bar in red

Why someone would do that ?

Latter, i can create foo file/dir, and link will work.

while directory build-tools/20.0.0/lib/ does not exsists, your link will be broken.

Share:
22,888

Related videos on Youtube

John Rose
Author by

John Rose

Just an old fogey.

Updated on September 18, 2022

Comments

  • John Rose
    John Rose over 1 year

    I have a directory to which I want to create a symbolic link. The commands I used were:

    cd workspace_b4a
    cd android-sdk-linux
    ln -sv build-tools/20.0.0/lib/ platform-tools/lib
    

    The output was:

    ‘platform-tools/lib’ -> ‘build-tools/20.0.0/lib/’
    

    However, Nautilus says that platform-tools/lib is a broken link and ls -l platform-tools/lib shows the both build-tools/20.0.0/lib/ and platform-tools/lib in red. What am I doing wrong?

    • αғsнιη
      αғsнιη over 9 years
      what is the output of ls -la platform-tools/lib | grep "^l"?
    • Mitch
      Mitch over 9 years
      Take a look at this.
  • Archemar
    Archemar over 9 years
    you can link with relative pathname.
  • RishbhSharma
    RishbhSharma over 9 years
    My apologies for misguidance.