explain the options of readlink command

I think it's quite self-explanatory, so I don't really know the part which sounds ambiguous for you... Let's see with an example:

--canonicalize

$ mkdir /tmp/realdir
$ mkdir /tmp/subdir
$ ln -s /tmp/realdir /tmp/subdir/link
$ cd /tmp

$ readlink -f ./subdir/link/nonexistentdir/
/tmp/realdir/nonexistentdir

$ readlink -f ./subdir/link/nonexistentfile.txt
/tmp/realdir/nonexistentfile.txt

Whatever the options are, readlink will: - translate the relative path to absolute path - translate the symlink name to the real path

And as you can see above, with -f, readlink doesn't care if the last part of this path (here nonexistentfile.txt) exists or not.

If another part of this path does not exist, readlink will output nothing and will have a return code different than 0 (which means an error occured). See:

$ readlink -f /tmp/fakedir/foo.txt
$ echo $?
1

--canonicalize-existing

If you try the same with -e:

$ readlink -e ./subdir/link
/tmp/realdir

$ readlink -e ./subdir/link/nonexistentfile.txt
$ echo $?
1

With -e, in case any of the path component doesn't exist, readlink will output nothing and will have a return code different than 0.

--canonicalize-missing

-m option is the opposite of -e. No test will be made to check if the components of path exist:

$ readlink -m ./subdir/link/fakedir/fakefile
/tmp/realdir/fakedir/fakefile

$ ln -s /nonexistent /tmp/subdir/brokenlink

$ readlink -m ./subdir/brokenlink/foobar
/nonexistent/foobar

Tags:

Readlink