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

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <img> <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <blockquote> <h1> <h2> <h3> <h4> <h5> <h6> <p> <br>
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.
  • Image links with 'rel="lightbox"' in the <a> tag will appear in a Lightbox when clicked on.

More information about formatting options

Type the characters you see in this picture. (verify using audio)
Type the characters you see in the picture above; if you can't read them, submit the form and a new image will be generated. Not case sensitive.