I'm not sure if these paths are duplicates. Given the relative path, how do I determine absolute path using a shell script?
Example:
relative path: /x/y/../../a/b/z/../c/d
absolute path: /a/b/c/d
The most reliable method I've come across in unix is readlink -f
:
$ readlink -f /x/y/../../a/b/z/../c/d
/a/b/c/d
A couple caveats:
readlink
will give a blank result if you reference a non-existant directory. If you want to support non-existant paths, use readlink -m
instead. Unfortunately this option doesn't exist on versions of readlink released before ~2005.