What is the use of readlink command

Can you please let me know the use of readlink command?

1 Like

readlink prints resolved symbolic links or canonical file names

Let’s understand it. Say we want to find the actual location of java program on CloudxLab.

Login to CloudxLab web console. Type below command and see the output

Command -

ls -lh /usr/bin/java

Output -

It shows that /usr/bin/java is symbolic link to /etc/alternatives/java

Let’s see if /etc/alternatives/java is a symbolic link to any other file or directory.

Type below command and see the output

Command -

ls -lh /etc/alternatives/java

Output -

It shows that /etc/alternatives/java is a symbolic link to /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.91-0.b14.el7_2.x86_64/jre/bin/java

Let’s see if /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.91-0.b14.el7_2.x86_64/jre/bin/java is a symbolic link

Type below command and see the output

Command -

ls -lh /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.91-0.b14.el7_2.x86_64/jre/bin/java

Output -

As you can see that /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.91-0.b14.el7_2.x86_64/jre/bin/java is not symbolic link to any file. This concludes that actual location of Java program on CloudxLab is /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.91-0.b14.el7_2.x86_64/jre/bin/java

We could achieve the same with readlink -f

Hope this helps.

2 Likes

Thanks for the useful post. Just wanted to share the below information:

Readlink -f filename --> provides the original link
readlink filename ( i.e without -f) --> provides the immediate link and not the original link

In our case :
readlink /usr/bin/java --> provides --> /etc/alternatives/java
readlink -f /usr/bin/jav --> provides --> /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.91-0.b14.el7_2.x86_64/jre/bin/java

Hence readlink command without usage of -f , does the same job of ls -lh or simply ls -l

4 Likes

Thanks @balajikvr for sharing this :slight_smile: