readlink Command: Tutorial & Examples

Resolving symbolic links

The readlink command is a utility in Linux used to resolve symbolic links. In other words, it outputs the absolute path of a file or a directory. It is part of the GNU Coreutils package which is installed on almost all Linux systems.

Understanding this command is crucial because symbolic links are common in Linux. They are used to create shortcuts or references to files and directories. However, it can sometimes be difficult to track where these links are pointing to, especially when a link points to another link. This is where the readlink command comes into play.

Understanding the readlink command

The basic syntax of readlink command is simple:

readlink [option]... [file]...

The command resolves the value of a symbolic link and prints it to the standard output. It's typically used in scripts where you need to know the actual file or directory a symbolic link points to.

Common readlink command options

Here are some of the commonly used readlink command options:

  • -f : This option prints the canonical name by following every symlink in every component of the given name recursively. This is most resilient to partial or broken symlinks.

  • -e : This option ensures a similar behavior to -f, but it only prints an output if the file or directory exists.

  • -m : This option also prints the canonical name, but it does not require the file or directory to exist.

Examples of using the readlink command

Let's look at a few examples of using the readlink command.

  1. Display the value of a symbolic link:

    ln -s /etc/hosts test
    readlink test
    

    Here, test is a symbolic link to the /etc/hosts file. The readlink command will output /etc/hosts.

  2. Using -f option to resolve a chain of symbolic links:

    ln -s /etc/hosts test1
    ln -s test1 test2
    readlink -f test2
    

    Here, test2 is a symbolic link to test1, which is a symbolic link to /etc/hosts. The readlink -f command will output /etc/hosts.

  3. Using -e option to ensure the file exists:

    ln -s /etc/non_existent_file test
    readlink -e test
    

    Here, test is a symbolic link to a non-existing /etc/non_existent_file. The readlink -e command will not output anything because the file does not exist.

Troubleshooting with readlink

The readlink command is a powerful tool for troubleshooting and identifying issues related to symbolic links. It can be used to identify broken symbolic links that point to non-existing files or directories. It can also help in identifying loops in symbolic links where a link points to itself directly or indirectly, causing a circular reference.

For instance, you can use the -e option to identify broken symbolic links, as the command will not return anything for a broken link. This can be very useful in scripts to ensure that a symbolic link is valid before performing operations on the file or directory it points to.

Conclusion

Understanding and using the readlink command effectively can make managing and troubleshooting symbolic links much easier on your Linux system. Whether you're a system administrator managing a complex system with numerous links, or a developer working in a codebase with linked directories, mastering readlink can be a valuable addition to your Linux skill set.

The text above is licensed under CC BY-SA 4.0 CC BY SA