Search code examples
terminaldirectorypathsymlinkrelative-path

How do I get the relative path based on two absolute paths?


I need the relative path pointing to a certain directory. I have this:

/a/b/ef
/a/b/g/h/i/

Now I want to create a relative symbolic link from the second folder to the first one, so it should look like this: ../../../ef

How do I extract the relative path based on two absolute paths in Terminal?

I’m on Mac OS X.


Solution

  • I found a solution using Python and this recipe.

    1. Download and save the above as relpath.py, stop at --- 8< --- snip --- 8< --- snip --- 8< --- snip --- 8< ---
    2. cd into the directory
    3. Run python relpath.py install in Terminal
    4. Then you can use the module from above

    Here is my script, which uses the above module to create relative symlinks based on two directories: relsymlink.py Gist