Find Canonical Path with readlink

The readlink utility resolves symlinks and prints the canonical absolute path of a file. It’s the standard tool on Debian-based systems for determining what a symlink actually points to.

Core options

OptionBehavior
-f, --canonicalizeFollow every symlink recursively; all but the last component must exist
-e, --canonicalize-existingFollow every symlink recursively; all components must exist
-m, --canonicalize-missingFollow symlinks; no requirements on component existence

Examples

# Resolve a symlink chain to its final target
readlink -f /usr/bin/python
 
# Require every component to exist (fails on broken symlinks)
readlink -e ./some-link
 
# Resolve even if the final target doesn't exist
readlink -m ./some-link

Caveats

  • readlink follows symlinks, so it is not suitable when you need to know the link itself rather than its target.
  • For a pure “print the resolved path” interface, realpath (from coreutils) provides similar functionality with a slightly different flag layout. See realpath(1) for details.

Sources

Related: ssh, linux-reconnaissance-commands