How to retrieve absolute path given relative
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".
#! /bin/shecho "$(cd "$(dirname "$1")"; pwd)/$(basename "$1")"
UPD Some explanations
- This script get relative path as argument
"$1"
- Then we get dirname part of that path (you can pass either dir or file to this script):
dirname "$1"
- Then we
cd "$(dirname "$1")
into this relative dir and get absolute path for it by runningpwd
shell command - After that we append basename to absolute path:
$(basename "$1")
- As final step we
echo
it