# Relative Symbolic Link



## kainjow (Jan 18, 2006)

Is it possible to create a symlink that uses a relative path instead of an absolute one?


----------



## lurk (Jan 18, 2006)

yep just use .. like "../../mygrandpa/uncle/cuosin.txt"


----------



## kainjow (Jan 18, 2006)

Sweet it works, thanks.

I could have swore I tried that though..  oh well


----------



## kainjow (Jan 18, 2006)

Annoyingly Panther doesn't have the "readlink" command like Tiger, but "file" can read symlinks though


----------



## leonpmu (Jan 23, 2006)

did you try "ln -l"  which makes a "soft" symlink...


----------



## not-just-yeti (Aug 27, 2012)

If you symlink to an *existing* file, the symlink is changed to an absolute path.
However, if you symlink to a *non*existing file (or, a to file whose pathname contains a symlink), then you get a relative link:

cd
mkdir Tmp1
mkdir Tmp2
touch Tmp1/file1
cd Tmp2
ln -s ../Tmp1/file1 f1
ln -s ../Tmp1/file2 f2
ls -l
# f1 @ -> /Users/yeti/Tmp1/file1
# f2 @ -> ../Tmp1/file2

You can then create Tmp1/file2, and Tmp2/f2 will be a relative link to it.


[Hmm, my characterization isn't *quite* complete, since `cd ; ln -s Tmp1/file1 f1` also creates a relative symlink, even though `Tmp/f1` exists.]


I don't know why `ln` doesn't just have a switch to force an absolute-path, with relative paths being the default.

[This is all confirmed on 10.8 Mountain Lion, but I suspect it's been like this for a while.]


----------



## not-just-yeti (Aug 27, 2012)

If you symlink to an *existing* file, the symlink is changed to an absolute path.
However, if you symlink to a *non*existing file (or, a to file whose pathname contains a symlink), then you get a relative link:

cd
mkdir Tmp1
mkdir Tmp2
touch Tmp1/file1
cd Tmp2
ln -s ../Tmp1/file1 f1
# linking to a file that doesn't yet exist:
ln -s ../Tmp1/file2 f2
ls -l
# f1 @ -> /Users/yeti/Tmp1/file1
# f2 @ -> ../Tmp1/file2

You can then create Tmp1/file2, and Tmp2/f2 will (still) be a relative link to it.


[Hmm, my characterization isn't *quite* complete, since `cd ; ln -s Tmp1/file1 f1` also creates a relative symlink, even though `Tmp/f1` exists.]


I don't know why `ln` doesn't just have a switch to force an absolute-path, with relative paths being the default.

[This is all confirmed on 10.8 Mountain Lion, but I suspect it's been like this for a while.]


----------

