1. Introduction

When dealing with symbolic links, we often encounter situations where we may need to determine the actual file or directory that a particular symbolic link points to. For example, when troubleshooting issues or tracking down dependencies, it’s essential to know the underlying file structure. Similarly, when working with scripts, we may need to resolve the symbolic links to ensure the code targets the right location.

This is where the readlink command comes in handy, as we can use it to understand the symbolic links.

In this tutorial, we’ll begin by exploring the basic usage of the readlink command. From there, we’ll move on to explore various options available to understand the behavior of the readlink command in more detail.

2. Basic Usage

Let’s start with the basic syntax of the readlink command:

$ readlink [OPTION] FILE

Here, FILE represents the symbolic link we want to inspect.

Now, let’s suppose we’ve got a symbolic link named my_link. We use the readlink command to display its target:

$ readlink my_link
myfile.txt

Here, the output indicates that the symbolic link my_link points to a file named myfile.txt.

Similarly, the readlink command is used to resolve symbolic links that point to directories:

$ readlink dir_link
original_dir

The output reveals that the symbolic link dir_link references the original_dir directory.

The previous examples dealt with relative paths. However, we can also use the readlink command to display the symbolic link that points to an absolute path:

$ readlink absolute_link
/home/user/data

The output clearly shows that absolute_link references /home/user/data directory.

3. Additional Options

While the basic usage of the readlink command is fairly straightforward, several options can enhance its functionality.

The -f option is particularly useful when working with a complex directory structure that contains multiple layers of symbolic links.

In particular, if we use the -f flag, we can quickly find the exact location of a file or directory through several redirections:

$ readlink -f my_link
/home/user/myfile.txt

This command displays the absolute path of myfile.txt that my_link points to, resolving any intermediate symbolic links.

3.2. Displaying Canonical Path

The -e option shows the canonical path of the target file or directory. It resolves symbolic links and removes any redundant separators or references. This ensures that the path shown is fully resolved and valid within the filesystem:

$ readlink -e my_link

The command eliminates any unnecessary elements in the path and provides the full, absolute path to the target.

3.3. Resolving Canonicalized Path

We can use the -m option to simplify a path by following all symbolic links throughout its components. Furthermore, it ensures that the path fully resolves without requiring every part of the path to exist beforehand:

$ readlink -m my_link

In this example, the -m option ensures that the path my_link is simplified to its canonical form, resolving any symbolic links or references.

3.4. Omitting Trailing Newlines

The -n option prevents the output from ending with a newline character. This is useful when using the output in a script where a trailing newline is undesirable:

$ readlink -n my_link
myfile.txt

Here, the -n option ensures that the output myfile.txt displays without a trailing newline character, thereby making it potentially easier to process.

3.5. Suppressing Error Messages

When we want to suppress error messages for nonexistent files or directories, we can use the -q option:

$ readlink -q my_link

In the output, we won’t see any error message if my_link doesn’t exist or there is an error.

However, the command still prints the resolved path in case of no errors.

While the readlink command is a powerful tool for working with symbolic links, two alternative commands may provide similar results in certain cases:

The realpath command offers similar functionality to readlink, but with fewer options and less granular control.

On the other hand, we can use the ls command to display information about symbolic links using the -l option:

$ ls -l my_link
lrwxr-xr-x 1 user user 10 Mar 12 14:30 my_link -> myfile.txt

Here, the output shows that my_link is a symbolic link that points to myfile.txt. In addition, the ls command gives limited details about symbolic links as its primary focus is listing file and directory objects.

5. Conclusion

In this article, we explored the usage and options of the readlink command, a handy tool to resolve symbolic links.

In general, the basic function of the readlink command is to display the target of symbolic links. The -f flag resolves all symbolic links in a path, while -e shows the canonical path of the target file or directory. The -m option simplifies a path by following all symbolic links and -n suppresses trailing newlines in the output. Additionally, -q suppresses error messages for nonexistent files or directories.

Along with the readlink command, we also discussed the realpath and ls commands as alternatives for working with symbolic links in certain situations. However, they offer fewer options and less granular control.