How to - Use the Readlink command to find the canonical filename of a symbolic link
Sometimes you may encounter a chain of files linked together using symbolic links. Readlink is a tool that can follow the chain and help you find out what is at the other end of it, the absolute file. Lets take an example of the Debian Alternate system. The file /usr/bin/editor is symlinked to /etc/alternatives/editor which in turn is linked to the actual editor binary - /bin/nano.
/usr/bin/editor ---> /etc/alternatives/editor ---> /bin/nano
Lets use readlink to find it out.
[shredder12]$ readlink /usr/bin/editor
/etc/alternatives/editor[shredder12]$ readlink -e /usr/bin/editor
/bin/nano
The -e option activates the canonicalize mode. It brings down the supplied link (/usr/bin/editor) to the simplest form - absolute file, by recursively following every link. If any component is missing or unavailable, readlink exits without any output.
We can also use the -f option. It also activates canonicalize mode, but if any other than the last component is missing, readlink exits without any output. Since our example is pretty straightforward, both of them will give the same answer.



























Post new comment