Sign up ×
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:


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 May 13 '10 at 4:30

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

This should not have been migrated. – Roger Pate May 13 '10 at 4:52
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')"

Tested successfully on linux (Kubuntu 14.04) an on Mac OSX, needs Python 2.6.

share|improve this answer

Your Answer


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