readlink flags & options

Basic usage

Print the target of a symbolic link.

readlink /usr/bin/python

-f, --canonicalize

Resolve all symlinks and relative components to get the absolute canonical path.

readlink -f ./relative/path/../file.txt
readlink -f /usr/bin/python3

-e, --canonicalize-existing

Like -f, but every component must exist.

readlink -e /usr/bin/python3

-m, --canonicalize-missing

Like -f, but components don't need to exist.

readlink -m /future/path/to/file

-n, --no-newline

Don't print a trailing newline. Useful in scripts.

DIR=$(readlink -fn /usr/bin/python3)

-z, --zero

Output NUL-terminated instead of newline-terminated.

readlink -zf /usr/bin/python3

Scripting pattern

Common pattern: resolve the real directory of a script.

SCRIPT_DIR=$(dirname "$(readlink -f "$0")")