8

Can someone please explain me following options of readlink command in simple language:

   -f, --canonicalize
      canonicalize  by  following  every symlink in every component of
      the given name recursively; all  but  the  last  component  must
      exist

   -e, --canonicalize-existing
      canonicalize  by  following  every symlink in every component of
      the given name recursively, all components must exist

   -m, --canonicalize-missing
      canonicalize by following every symlink in  every  component  of
      the  given  name recursively, without requirements on components
      existence
Alex Jones
  • 6,223
  • 17
  • 51
  • 83
  • 2
    Try the [info page](http://www.gnu.org/software/coreutils/manual/html_node/readlink-invocation.html#readlink-invocation). Also, they have some [test scripts](http://git.savannah.gnu.org/cgit/coreutils.git/tree/tests/readlink) (`can-f.sh`, `can-e.sh` and `can-m.sh`) that should give you an insight on how those options work. – don_crissti May 12 '15 at 12:12

2 Answers2

17

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
apaul
  • 3,338
  • 1
  • 15
  • 15
  • 3
    the thing that confused me was why would someone need `-e` and `-m` options? – Alex Jones May 12 '15 at 15:32
  • 5
    @edwardtorvalds for instance, you may need a path which depends on an external device which is not mounted yet, or a log file which is not yet created because the service is not up... – apaul May 12 '15 at 15:58
0

that confused me was why would someone need -e and -m options?

From the kernel source code ./scripts/kconfig/merge_config.sh (the script takes a list of config fragment values, and merges them one by one):

if [ -z "$KCONFIG_CONFIG" ]; then
    if [ "$OUTPUT" != . ]; then
        KCONFIG_CONFIG=$(readlink -m -- "$OUTPUT/.config")
    else
        KCONFIG_CONFIG=.config
    fi
fi

KCONFIG_CONFIG receive path to .config file which may not yet exist.

e42d3
  • 3
  • 2