Beginners Guide for Readlink Command on Linux

Linux TLDR
Last Updated:
Reading time: 3 minutes

If you have been following us for a long time, then you might already be aware of symlinks (or soft links), but for a quick reminder, they are a kind of shortcut to another file in Linux.

Now, you might already be interacting with soft links without knowing the path of the original file to which they point.

For that, you have tools like the ls command that can tell you where a symbolic link points.

$ ls -l /path/to/softlink

For example, I’ve created a soft link named β€œfile2.txt” that points to β€œfile1.txt” and both reside in the same directory.

$ ls -l /home/linuxtldr/file2.txt

Output:

Finding the file to which the symbolic link points

Great, we got the path to the original file, but what if I told you that β€œfile1.txt” is another soft link?

Confused? But it’s true, and it’s also quite possible that the next file will be another soft link, so tools like β€œls” are not that effective in this type of situation where you want the exact path of the original file from the deeply nested soft link.

For that, you can take the help of specialized tools like readlink (as its name suggests) to find the exact path of the original file that is deeply nested in multiple layers.

Tutorial Details

DescriptionReadlink (Following the Symlinks)
Difficulty LevelLow
Root or Sudo PrivilegesNo
OS CompatibilityUbuntu, Manjaro, Fedora, etc.
Prerequisitesreadlink
Internet RequiredNo

What is the Readlink Command in Linux?

The readlink command is a specialized tool that can find deep-nested symbolic links or canonical file names.

I think that’s enough for the introduction; now look at the following image.

Soft link file structure for readlink usage

While showing the usage of the readlink command, I will use the above mentioned files, mostly β€œfile2.txtβ€œ, to show you how to resolve multiple layered soft links.

Displaying the File Name to Which Soft Link Points

The simplest way to use this command is to directly specify the soft link path without any options.

$ readlink /home/linuxtldr/file2.txt

Output:

Finding the file to which the soft link points

As you can see from the above picture, β€œfile1.txt” is the file to which β€œfile2.txt” points, but β€œfile1.txt” is another soft link that points to another file.

However, you can directly jump to multiple layers down to get the exact original file to which all the soft links are pointing by following the next section.

Recursively Follow All of the Soft Links

The β€œ-f” flag will recursively follow each of the soft links to the bottom of the tree to give you the exact original file.

For example, the following command will skip each soft link in the tree until the exact file that originated the first soft link is found.

$ readlink -f /home/linuxtldr/file2.txt

Output:

Return the original file to which all soft links point

The above command works to find the deeply nested original file, but there is one problem: it will still return the output even if your file (in this case, β€œfile.txtβ€œ) is deleted.

Getting output even after the original file is removed

However, this can be avoided by instructing the readlink command to print the output only if the file exists, as shown in the next section.

Only Output When the Original File Exists

As you saw in the previous example, even if the original file doesn’t exist, the readlink command still outputs it in the result, which can be avoided by using the β€œ-e” flag.

The difference between the β€œ-f” and β€œ-e” flags is that the β€œ-e” flag will only return the output if the original file exists and is not deleted.

For example, the following command returns the output because the original file exists.

$ ls -l /home/linuxtldr/file.txt
$ readlink -e /home/linuxtldr/file2.txt

Output:

Returning the existing file

However, if the original file to which all the soft links point is deleted, then no output will be returned, as shown.

$ rm /home/linuxtldr/file.txt
$ readlink -e /home/linuxtldr/file2.txt

Output:

Returning the non existing file

Avoid the Trailing Delimiter

The β€œ-n” flag will not output the trailing delimiter to make output more compact.

$ readlink -n /home/linuxtldr/file2.txt

Output:

Skipping a new line in the output

Return the Output in Quiet Mode

The β€œ-q” flag will suppress any type of error by printing the output quietly.

$ readlink -q /home/linuxtldr/file2.txt

Output:

Print the output quietly

Suppress the Error in the Output

The β€œ-s” flag will suppress most of the error from the output, behaving similarly to the β€œ-q” flag.

$ readlink -s /home/linuxtldr/file2.txt

Output:

Suppressing the errors

Printing All the Error Messages

Unlike the previous two examples, using the β€œ-v” flag will enable the verbose mode, in which all the errors that occur will be printed on the output.

This flag is most effective if you use it with another flag, like the β€œ-e” flag, to display errors if the original file does not exist.

$ rm /home/linuxtldr/file.txt
$ readlink -v -e /home/linuxtldr/file2.txt

Output:

Verbose mode in readlink command

That was the last example.

There is not much left to say about this tool, although if you have any questions or queries, feel free to ask them in the comment section.

Join The Conversation

Users are always welcome to leave comments about the articles, whether they are questions, comments, constructive criticism, old information, or notices of typos. Please keep in mind that all comments are moderated according to our comment policy.