Say I have two paths: <source_path>
and <target_path>
. I would like my shell (zsh) to automatically find out if there is a way to represent <target_path>
from <source_path>
as a relative path.
E.g. Let's assume
<source_path>
is/foo/bar/something
<target_path>
is/foo/hello/world
The result would be ../../hello/world
Why I need this:
I need like to create a symbolic link from <source_path>
to <target_path>
using a relative symbolic link whenever possible, since otherwise our samba server does not show the file properly when I access these files on the network from Windows (I am not the sys admin, and don't have control over this setting)
Assuming that <target_path>
and <source_path>
are absolute paths, the following creates a symbolic link pointing to an absolute path.
ln -s <target_path> <source_path>
so it does not work for my needs. I need to do this for hundreds of files, so I can't just manually fix it.
Any shell built-ins that take care of this?
Best Answer
You could use the
symlinks
command to convert absolute paths to relative:We've got absolute links, let's convert them to relative:
References