Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
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 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
share|improve this answer
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.

share|improve this answer

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
share|improve this answer
Hm, for me it seems there is no such option -r. Using Mac though. – ivkremer May 4 at 15:29

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
share|improve this answer
thanks. symlinks available on github. Maintained by J. Brandt Buckley. v1.4.3. – Evgeny Vereshchagin Aug 8 '15 at 12:20

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
share|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. 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
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.