I can do this:

$ pwd
$ ln -s /home/beau/foo/bar.txt /home/beau/bar.txt
$ readlink -f bar.txt

But I'd like to be able to do this:

$ pwd
$ cd foo
$ ln -s bar.txt /home/beau/bar.txt
$ readlink -f /home/beau/bar.txt

Is this possible? Can I somehow resolve the relative pathname and pass it to ln?

  • But what is the problem that it would be solved with the described behavior? – forcefsck Mar 31 '11 at 14:46
  • Well, say you offer a compressed folder for download, and would like to give copy-pastable instructions on how to symlink some of the folder's files. – Humphrey Bogart Mar 31 '11 at 18:14
  • 2
    ln -s $(pwd)/bar.txt ~/ or include an install script. – forcefsck Mar 31 '11 at 19:15
  • 2
    the link stores actually the name you use on command line. The resolution of that name to a file is done relative to the link location. You have to use ln -s foo/bar.txt /home/beau/bar.txt – pqnet Aug 25 '14 at 15:41

If you create a symbolic link to a relative path, it will store it as a relative symbolic link, not absolute like your example shows. This is generally a good thing. Absolute symbolic links don't work when the filesystem is mounted elsewhere.

The reason your example doesn't work is that it's relative to the parent directory of the symbolic link and not where ln is run.

You can do:

$ pwd
$ ln -s foo/bar.txt bar.txt
$ readlink -f /home/beau/bar.txt

Or for that matters:

$ cd foo
$ ln -s foo/bar.txt ../bar.txt

It's important to realise that the first argument after ln -s is stored as the target of the symlink. It can be any arbitrary string (with the only restrictions that it can't be empty and can't contain the NUL character), but at the time the symlink is being used and resolved, that string is understood as a relative path to the parent directory of the symlink (when it doesn't start with /).

| improve this answer | |
  • 2
    Nicely said. You answered this while I was typing my own answer. :) – Shadur Mar 31 '11 at 12:30
  • I just had a slightly different question - and this answers it perfectly. I wanted to know how to reference the home directory in symlink so it would work when I transferred it from one user to another. I wasn't sure if something like ~ would work. – Joe Feb 25 '17 at 7:05
  • 1
    "that string is understood as a relative path to the parent directory of the symlink" - this explains everything! – Bojan Komazec Sep 26 '19 at 15:25

If you use newer coreutils (I'm using coreutils-8.22-11), it has an option to do that:

ln --help | grep relative
can hold arbitrary text; if later resolved, a relative link is
  -r, --relative              create symbolic links relative to link location

For example:

$ mkdir /tmp/test
$ touch /tmp/test/to
$ ln -rs /tmp/test/to /tmp/test/from
$ ls -l /tmp/test/from
    lrwxrwxrwx 1 ptr ptr 4 aug   25 17.02 /tmp/test/to -> from
| improve this answer | |
  • Hm, for me it seems there is no such option -r. Using Mac though. – ivkremer May 4 '16 at 15:29
  • 1
    @ivkremer it seems this option is available since coreutils >= 8.16 (source: superuser.com/a/837935/403979) – DJCrashdummy Jun 1 '17 at 7:08
  • Using the relative flag seemed to clear up the issues of "too many levels of symbolic links" I was experiencing. – Vix Jun 25 '18 at 13:11

Sorry, no. Symbolic links are relative to the location the link is in, not the location you were when you created the link. There are several good reasons for this behavior, most of which involve mounting remote filesystems.

| improve this answer | |

There's a convenient little utility called symlinks (originally by Mark Lords, now maintained by J. Brandt Buckley), present in many Linux distributions.

To convert all the absolute symbolic links in a directory to be relative:

symlinks -c /path/to/directory

Unfortunately there's no option to convert a single link, you'd have to move it to its own directory first.

mkdir ../tmp
mv link-to-relativize ../tmp
symlinks ../tmp
mv ../tmp/* .
rmdir ../tmp
| improve this answer | |

You could try:

ln -s `pwd`/bar.txt /home/beau/bar.txt

But, it makes a symbolic link to the absolute pathname. Your textual question asks mentions relative pathname...

Normally, a relative pathname is what you want, and what ln -s gives you. I think what you want is:

cd /home/beau
ln -s foo/bar.txt bar.txt
| improve this answer | |

As the most common shells expands paths, to create a relative symlink you should quote the "source path".

Also, the -s parameter means --symbolic. ln creates hard links by default.

$ ln -s '../../relative_path' path
$ ls -al path

lrwxrwxrwx 1 empjustine empjustine 19 Aug  6 01:38 path -> ../../relative/path
| improve this answer | |
  • Underrated answer. – dr jerry Jun 24 at 12:52

If you are certain there is no chance of your mount points changing you can achieve what you are trying to do in a bash shell like this:

ln -s `pwd`/bar.txt ~/bar.txt

This assumes that you are in the target directory when making the symbolic link.

| improve this answer | |

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.