Is there a command to retrieve the absolute path given a relative path?
For example I want $line to contain the absolute path of each file in dir ./etc/
find ./ -type f | while read line; do
echo $line
done
In simple words, an absolute path refers to the same location in a file system relative to the root directory, whereas a relative path points to a specific location in a file system relative to the current directory you are working on.
An absolute path is defined as specifying the location of a file or directory from the root directory(/). In other words,we can say that an absolute path is a complete path from start of actual file system from / directory. Relative path is defined as the path related to the present working directly(pwd).
relpath() method in Python is used to get a relative filepath to the given path either from the current working directory or from the given directory.
To obtain the full path of a file, we use the readlink command. readlink prints the absolute path of a symbolic link, but as a side-effect, it also prints the absolute path for a relative path. In the case of the first command, readlink resolves the relative path of foo/ to the absolute path of /home/example/foo/.
Try realpath
.
~ $ sudo apt-get install realpath # may already be installed
~ $ realpath .bashrc
/home/username/.bashrc
To avoid expanding symlinks, use realpath -s
.
The answer comes from "bash/fish command to print absolute path to a file".
If you have the coreutils package installed you can generally use readlink -f relative_file_name
in order to retrieve the absolute one (with all symlinks resolved)
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