The difference is that relative symlinks are resolved at access time, not creation time.
In your first example, if the link's name is /somedir/LALA/apache2
and its target is FOO/apache2
, it will be resolved to /somedir/LALA/FOO/apache2
when accessing it.
link name: link target: absolute target: "/somedir/LALA/apache2" + "FOO/apache2" = "/somedir/LALA/FOO/apache2"
In the second example, the link's name is same – /somedir/LALA/apache2
– but its target, ../FOO/apache2
, is now resolved as:
link name: link target: absolute target: "/somedir/LALA/apache2" + "../FOO/apache2" = "/somedir/LALA/../FOO/apache2" → "/somedir/FOO/apache2"
This means that you must always give "../FOO/apache2
" as the target to ln -s
when creating the second link, regardless of what directory you are currently in.
Note: In the latest version of coreutils, the ln
command has a new option -r
/--relative
, which does the job for you. For example, ln -r -s FOO/apache2 LALA/apache2
would work correctly.
For older versions, you could hack something with realpath --strip --relative-to
...
I wrote sym
for my own use.