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.

-

Facing issues? Have Questions? Post them here! I am happy to answer!


Author: Rakesh
Author Info:

Rakesh is a seasoned developer with over 10 years of experience in web and app development, and a deep knowledge of operating systems. Author of insightful How-To articles for Code2care.

Follow him on: X

Copyright © Code2care 2023 | Privacy Policy | About Us | Contact Us | Sitemap