Resolve link target using readlink
In this step, you will learn how to use the readlink
command to find the target of a symbolic link.
While ls -l
is great for a visual overview, the readlink
command is specifically designed to resolve symbolic links and print their target path. This is particularly useful in scripts or when you need just the target path without other file information.
Make sure you are in the ~/project
directory.
Use the readlink
command on the symbolic link my_symlink
that you created in the previous steps:
readlink my_symlink
The output will be the path that the symbolic link points to:
original_file.txt
This confirms that my_symlink
is indeed pointing to original_file.txt
.
The readlink
command has a useful option, -f
, which recursively follows all symbolic links and resolves them to the absolute path of the final target. This is helpful if you have a chain of symbolic links.
Let's create another symbolic link that points to my_symlink
:
ln -s my_symlink another_link
Now, if you use readlink
on another_link
without the -f
option, it will show the immediate target:
readlink another_link
Output:
my_symlink
But if you use readlink -f
on another_link
, it will follow the chain (another_link
-> my_symlink
-> original_file.txt
) and give you the absolute path of the final target:
readlink -f another_link
Output:
/home/labex/project/original_file.txt
This shows the full path to the original file. The readlink -f
command is very powerful for finding the ultimate destination of a link, even if it's nested within other links.
You have now learned three ways to work with symbolic links: using test -L
to check if a file is a link, using ls -l
to see link details and targets, and using readlink
to resolve the target path.