Convert a relative path to absolute path in BASH

0

There are a few ways to take a relative path and convert it to an absolute path which can be accessed regardless of the current working directory. The easiest method is to use the readlink utility which comes bundled with about every distribution.

Here my working directory is my home directory. It is printed as an absolute path.
$ pwd/home/ryan

Using readlink we can convert this same directory as a relative path to an absolute path.
$ readlink -f ./../ryan/Desktop/../../../home/ryan/home/ryan

If you are using a Debian based distribution like Ubuntu you can use the realpath utility instead.
$ realpath ./../ryan/Desktop/../../../home/ryan/home/ryan

Share.