# Make a symbolic link to a relative pathname

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? - 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 ln -s$(pwd)/bar.txt ~/ or include an install script. –  forcefsck Mar 31 '11 at 19:15
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 relative path to a symbolic link, 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  - Nicely said. You answered this while I was typing my own answer. :) – Shadur Mar 31 '11 at 12:30 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. - 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  - 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  - As the most common shells expands paths, to create a relative symlink you should quote the "source path". Also, the -s parameter means --symbolic. ls create hard links by default. $ ln -s '../../relative_path' path
\$ ls -la

lrwxrwxrwx 1 empjustine empjustine 19 Aug  6 01:38 path -> ../../relative/path

-