Bash Command to Get Absolute Path for a File

If you are inside a directory location in Terminal and you want to get the absolute path of a particular file then you can make use of the readlink command with -f or --canonicalize flag.


readlink - print resolved symbolic links or canonical file names

man page: https://man7.org/linux/man-pages/man1/readlink.1.html


Example:

bash-3.2$ readlink -f cert.pem 

/Users/c2ctech/cert.pem
How to Get Absolute Path of a file using bash

As you can see, the output is the absolute file path with the file name.

If you are on macOS then you can also make use of the realpath command.

Comments & Discussion

Facing issues? Have questions? Post them here! We're happy to help!