How to use the command readlink (with examples)
- Osx
- November 5, 2023
The readlink
command is used to follow symbolic links and get information about them. It is a part of the GNU Core Utilities package and is commonly available on Linux systems. The command takes a path to a symbolic link file as an argument and prints the absolute path that the symlink points to.
Use case 1: Print the absolute path that a symlink points to
Code:
readlink path/to/symlink_file
Motivation: This use case is helpful when you want to determine the actual target of a symbolic link. Symlinks are often used to create shortcuts or aliases for files or directories, and it can be useful to know the path they lead to.
Explanation:
readlink
: The command itself.path/to/symlink_file
: Replace this with the actual path to the symbolic link file you want to inspect.
Example output:
Let’s say we have a symbolic link file called shortcut
in the directory /home/user
and it points to the file document.txt
located in /var/documents
. Running the following command:
readlink /home/user/shortcut
The output would be:
/var/documents/document.txt
This tells us that the symlink /home/user/shortcut
points to the file /var/documents/document.txt
.
Conclusion:
The readlink
command is a useful tool for inspecting symbolic links and getting information about their targets. With it, you can easily determine the path that a symlink points to, allowing you to understand the underlying file or directory that it represents.