83

I can do this:

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

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

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

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

4
  • 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. 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
95

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
/home/beau
$ ln -s foo/bar.txt bar.txt
$ readlink -f /home/beau/bar.txt
/home/beau/foo/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 /).

3
  • 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
  • 3
    "that string is understood as a relative path to the parent directory of the symlink" - this explains everything! Sep 26 '19 at 15:25
40

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
4
  • 1
    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) 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
  • This should be the accepted answer. Dec 30 '21 at 21:33
14

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.

9

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
2
  • thanks. symlinks available on github. Maintained by J. Brandt Buckley. v1.4.3. Aug 8 '15 at 12:20
  • This tool is of limited utility. It only repairs absolute symlinks that are valid, relative to the absolute root. For instance, say I have a file system tree tree/ which contains tree/bin/foo -> /bin/bar. There is no way to tell symlinks that tree is the "sysroot", such that /bin/bar is supposed to refer to tree/bin/bar (from where I'm sitting) and therefore the correct way to relativize it is to rewrite it to just bar. To the symlinks utility, this looks like a dangling link; all it can do is delete it.
    – Kaz
    Apr 19 '21 at 19:36
4

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
1

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
1
  • 1
    Underrated answer.
    – dr jerry
    Jun 24 '20 at 12:52
0

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.

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.