5 Ways to Follow (or Find All) Symbolic Links in Linux

Linux TLDR
Last Updated:
Reading time: 3 minutes

Symbolic links (also referred to as β€œsoft links” or β€œsymlinks”) are a kind of shortcut to another file used in the Linux operating system mostly for shared libraries.

If you’ve been using Linux as a regular desktop, you might already have encountered one or will in the future. But today’s focus will be on how you can find the original file to which the symbolic link points.

Note that we already wrote a separate article for all the tools that are mentioned in this article, so this is kind of a summarized version of all the tools.

However, if you directly jump to this article and are only interested in knowing a tool that can help you find the original file to which the symbolic link points, then you can directly read our readlink command article.

So, let’s keep all this aside and jump to the main topic: how to follow symbolic links in Linux.

Tutorial Details

DescriptionFollowing the Symbolic (or Soft) Links
Difficulty LevelLow
Root or Sudo PrivilegesNo
OS CompatibilityUbuntu, Manjaro, Fedora, etc.
Prerequisitesls, readlink, realpath, stat, file
Internet RequiredNo

How to Follow Symbolic Links in Linux

As I’ve told you earlier, symbolic links are nothing but a kind of shortcut to another file created using the ln command. Once you create the soft link, there are multiple ways to find the original file to which it points.

In Linux, you have multiple commands to do this job, followed by:

So, we will go through each of the above mentioned commands to find the original file to which the soft link points. But before that, check the following image:

Soft link structure in my Linux

In the above image, there are nested soft links, followed by β€œfile.txt (original file) <- file1.txt (soft link – layer 1) <- file2.txt (soft link – layer 2)” in my Linux system.

I will use β€œfile2.txtβ€œ, which is on top of the soft link tree, to show you all the examples in this article, so let’s get started with our first command.

Using the ls command

The ls command is not a specialized tool designed for the purpose of printing the original file to which the soft link points.

However, if you use the β€œ-l” flag, then it will give you the path of the file to which the specified soft link points.

$ ls -l file2.txt

Output:

Using the ls command, determine the path to the file to which a soft link points

As you can see, it returns β€œfile1.txt” in the output, which is another soft link, so this command is not the perfect solution if the specified soft link involves multiple layers, like one link pointing to another.

However, you can check the next command that can dig the soft link in multiple layers to give you the original file.

Using the readlink command

Readlink is a specialized command-line utility that allows you to follow soft links in a single or multiple layers in the tree.

For example, the following command with the β€œ-f” flag will directly return the original file to which all the soft links point.

$ readlink -f file2.txt

Output:

Return the original file to which all soft links point using the readlink command

Using the realpath command

As its name suggests, it returns the absolute path of the referenced file, directory, or soft link; however, it is quite different from the previous command, which can be stated in this way: this command without any flag is equivalent to the β€œreadlink -f” command.

For example, the following command, without any flag, will directly return the original file to which all the nested soft links point.

$ realpath file2.txt

Output:

Returns the original file to which all soft links point using realpath command

Using the stat command

The stat command is usually used to get detailed information about a referenced file or filesystem, like file size, blocks, IO blocks, device type, etc.

You can directly specify the soft link path as an argument to this command, but you should note that it will give you the next mapped link or file path.

$ stat file2.txt

Output:

Using the stat command, return the file to which the soft link points

If you just want to know the file to which the soft links point and want to ignore other unnecessary information in the output, then use the following command:

$ stat -c%N file2.txt

Output:

Trimming stat command output

Using the file command

The file command is mostly used to find the type (or property) of a referenced file, directory, or soft link. In the case of a soft link, it will also print the next mapped link or file path.

$ file file2.txt

Output:

Return the file to which soft link point using file command

Final Tips!

If you are only interested in knowing the next mapped or original file to which all the soft links point, then I would suggest using the readlink and realpath commands (you can also read our article about them to learn more).

So, that’s it for today; if you have any questions or queries related to this topic, then 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.