Given two absolute paths, e.g.
/a/path/to/a
/a/path/to/somewhere/else
How can I get a relative path from one
Find the longest common path (in this case, /a/path/to
) and delete it from both absolute paths. That would give:
/a
/somewhere/else
Now, replace each path component in the starting path with ../
and prepend the result to the destination path. If you want to go from directory else
to directory a
, that would give you:
../../a
If you want to go the other way, you'd instead have:
../somewhere/else