How to Follow Symbolic Links
A symbolic link (also known as soft link) is a kind of shortcut to another file. It's heavily used in Linux for shared libraries.
But how do you know to which original file the link points to?
You can use the ls command for this purpose. Surprised? Don't be. The long listing ls -l
displays where a symbolic link points:
ls -l /path/to/file
For example, I've created a soft link named MyTorrents
that targets another disk so my command will be:
ls -l /home/sagar/Symbolics/MyTorrents
However, this is not a foolproof way to follow the symbolic link to the original file because if it's a multilayer link (a link pointing to another link that points to a file), the ls command won't display the source file.
How to Find Target File of Symbolic Link in Linux
It's a no-brainer that with enough skills, you do have multiple ways of accomplishing the same thing, especially if we consider Linux.
So I'll be utilizing the following command line utilities to follow symbolic links:
- readlink
- realpath
- stat
- file
You can use the ln command to create links and practice while you follow this tutorial.
1. Using readlink command
A specific utility that is just made to accomplish our goal. Yes, that's readlink.
It is quite easy to use and available by default on every Linux distro. So just give a path of symbolic link with readlink
command and that's it.
readlink /path/to/symbolic/link
My symbolic link is placed at /home/sagar/Symbolics/MyTorrents
so my command would be:
readlink /home/sagar/Symbolics/MyTorrents
But what if your symbolic link involves multiple layers such as one link indicted to another? Well, in that case, you'd have to use -f
option.
For this example, I've created a new symbolic link located at /home/sagar/Documents/NewLink
and maps to the other link to have a better idea of how to deal with such scenarios:
readlink -f /home/sagar/Documents/NewLink
2. Using realpath command
As its name suggests, the realpath utility is used to get the path of files and directories but the interesting thing is when used without any option, it can get us to the source of the symbolic link.
Using realpath even without any options is equivalent to using readlink -f
so don't worry about being mapped to another symbolic link.
The syntax of realpath to follow symbolic link to the source file is:
realpath /path/to/symbolic/link
And after specifying the path, end result should look like this:
3. Using stat command
The stat utility is used to get the status of files and can also be utilized to find the original source of the symbolic link.
Just give a path of the symbolic link to the stat
command and that's it.
stat /path/to/symbolic/link
And if you find the other details unnecessary, you can use the -c%N
option to filter them out. Not the easiest option to remember and hence use the man or help command to recall it.
stat -c%N /path/to/symbolic/link
4. Using file command
Well, using the file command is quite easy and you're required to follow the same syntax that you saw earlier with other examples.
A file command with a path to a symbolic link. That's all you'd need!
file /path/to/symbolic/link
Final Words
If you're dealing with multilayer soft link layers, I recommend using the first two ways of following symbolic links.
These utilities are quite basic and do not require any complex syntax but if you're still confused, let me know in the comments.