Shell – Convert Relative Path to Absolute Path Without Symbolic Link

directoryfilenamesshellsymlink

Is there a Unix command to get the absolute (and canonicalized) path from a relative path which may contain symbolic links?

Best Answer

You can use the readlink utility, with the -f option:

-f, --canonicalize

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

Some distributions, for example those that use GNU coreutils and FreeBSD, also come with a realpath(1) utility that basically just calls realpath(3) and does pretty much the same thing.

Related Question