Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

We have two paths. First one is directory, second either dir or file.

/a/b/c and /a/d/e.txt

Relative path from first path to second will be:

../../d/e.txt

How to calculate that in Linux terminal? For those who ask “What is usage case?” one could use this—or example—to create lots of relative symlinks.

share|improve this question

migrated from stackoverflow.com May 13 '10 at 4:30

This question came from our site for professional and enthusiast programmers.

1  
This should not have been migrated. –  Roger Pate May 13 '10 at 4:52
1  
It should have been closed as a duplicate. –  Dennis Williamson May 13 '10 at 10:19

2 Answers 2

Assuming GNU coreutils:

  • For symlinks, ln has recently learned the --relative option.

  • For everything else, realpath supports options --relative-to= and --relative-base=.

share|improve this answer

For me, this answer (which uses a python oneliner) works perfect.

$ python -c "import os.path; print os.path.relpath('/a/d/e.txt', '/a/b/c')"
../../d/e.txt

Tested successfully on linux (Kubuntu 14.04) an on Mac OSX.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.