Say I have a variable with a path release/linux/x86, and want the relative path from a different directory (i.e. ../../.. for current working directory), how would I get that in a shell command (or possibly GNU Make)?

Soft link support not required.

This question has been heavily modified based on the accepted answer for improved terminology.


Absolutely not clear the purpose of it, but this will do exactly what was asked, using GNU realpath:

realpath -m --relative-to=release/linux/x86 .
realpath -m --relative-to=release///./linux/./x86// .
  • The problem here is that realpath is a nonstandard extension and not part of the UNIX/POSIX specs. – schily Mar 17 '20 at 14:12
  • @schily and not all realpath(1) implementations support the --relative-to option. – mosvy Mar 17 '20 at 15:00
  • If this ever becomes part of POSIX, it would first need to decide on a short option for that feature... – schily Mar 17 '20 at 15:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.