readlink

readlink prints where a symbolic link points.

readlink [options] file...

A symbolic link is a file whose contents are another path. readlink reads that path and prints it:

$ readlink /home/jack/work
/data/projects/jack

By default readlink resolves exactly one level: it prints the link's immediate target, whether or not that target is itself a link, and whether or not it exists. If the named file is not a symbolic link, readlink prints nothing and reports failure.

Canonicalising a whole path #

The canonicalise options change readlink from "read this one link" into "resolve this entire path to its real, absolute form" — following every symbolic link in every component, collapsing . and ... The three differ only in how strict they are about components existing:

OptionResolvesExistence requirement
-f, --canonicalizeEvery link in the path.Every component except the last must exist.
-e, --canonicalize-existingEvery link in the path.Every component must exist.
-m, --canonicalize-missingEvery link in the path.No component need exist.

With any of these, readlink succeeds on an ordinary (non-link) file too — it simply returns the canonical path. realpath is the dedicated command for this canonicalising job and has more options for it; the canonicalise flags here exist so readlink can do it without a second tool.

Output and error control #

OptionEffect
-n, --no-newlineDo not print the trailing newline. Ignored, with a warning, when more than one file is given.
-z, --zeroEnd each output line with a NUL character instead of a newline.
-q, --quiet / -s, --silentSuppress most error messages. This is the default.
-v, --verboseReport error messages that the quiet default would suppress.

Exit status #

CodeMeaning
0Every named file was resolved and printed.
1A file was not a symbolic link, or a path could not be resolved under the chosen strictness.

Edit this page