Beginners Guide for Readlink Command in Linux

Linux TLDR
Last Updated:
Reading time: 4 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
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.

Also Read: 5 Ways to Follow Symbolic Links in Linux

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
Host System and ArchitectureUbuntu 22.10 (x64)
OS Compatibility Ubuntu, Manjaro, Fedora, etc.
Prerequisitesreadlink
Internet RequiredNo
Discussed Tools in this Article

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
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.

Also Read: Beginners Guide for Realpath Command in Linux

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
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
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
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
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
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
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
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
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
Verbose mode in the 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.