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
Use abspath() to Get the Absolute Path in Python abspath() with the given path to get the absolute path. The output of the abspath() function will return a string value of the absolute path relative to the current working directory.
An absolute path is a path that describes the location of a file or folder regardless of the current working directory; in fact, it is relative to the root directory.
In this case, first, we need the current script's path, and from it, we use dirname to get the directory path of the script file. Once we have that, we cd into the folder and print the working directory. To get the full or absolute path, we attach the basename of the script file to the directory path or $DIR_PATH.
File getAbsolutePath() method in Java with Examples The getAbsolutePath() method is a part of File class. This function returns the absolute pathname of the given file object. If the pathname of the file object is absolute then it simply returns the path of the current file object.
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.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With