Q. How can I convert a relative path to absolute path for using them in shell script.

Some times it required to get absolute path from a given relative path. For example I am in /home/surendra and relative path to /etc/apache2 will be ../../etc/apache2 for this relative path I require absolute path so that the value is constant with the script irrespective of my directory changes. This can be acheived by using command readlink.

readlink -f relativepath

Example1: I am at /home/surendra I want to know what will be the absolute path to ../../etc/ from that directory.

surendra@linuxnix.com:~$ pwd

surendra@linuxnix.com:~$ readlink -f ../../etc

Example2: I am at /etc/samba and I have ../apache/mods-enabled which is relative to /etc/samba, I want to know the absolute path.

surendra@linuxnix.com:~$ cd /etc/samba/

surendra@linuxnix.com:/etc/samba$ readlink -f ../apache2/mods-enabled/

The following two tabs change content below.
Mr Surendra Anne is from Vijayawada, Andhra Pradesh, India. He is a Linux/Open source supporter who believes in Hard work, A down to earth person, Likes to share knowledge with others, Loves dogs, Likes photography. He works as Devops Engineer with Taggle systems, an IOT automatic water metering company, Sydney . You can contact him at surendra (@) linuxnix dot com.