Skip to content
Tutorials Center
My learning

Topics

Linux Command Reference ยท Files and Directories

Linux readlink command

Reference for the Linux readlink command: print where a symlink points, use -f to resolve a full canonical path, and the differences between -f, -e and -m.

readlink answers the question "where does this symbolic link point?". With no options it prints the stored target of a symlink exactly as it was written, whether relative or absolute, and exits with an error if the argument is not a symlink.

With -f it becomes a path resolver: it follows every symlink in every component and prints the full canonical absolute path. That is why readlink -f "$0" is a common idiom for finding the real location of a script. The newer realpath does the same job with a clearer name.

Terminal
readlink [OPTION]... FILE...

Try it

Bash One level versus fully resolved

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Bash -f, -e and -m with missing files

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Syntax

Terminal
readlink [-f | -e | -m] [-n] [-q] [-s] [-v] [-z] FILE...
Option Description
(none) Print the value of the symlink; fail silently if FILE is not a symlink.
-f, --canonicalize Resolve all symlinks; all components except the last must exist.
-e, --canonicalize-existing Resolve all symlinks; every component must exist.
-m, --canonicalize-missing Resolve what exists; no component needs to exist.
-n Do not print the trailing newline.
-q, -s, --silent Suppress most error messages.
-v Report errors (the default for canonicalizing modes).
-z End each output line with a NUL byte instead of a newline.

Returns: 0 on success; 1 if the file is not a symlink (plain mode) or cannot be resolved (canonical modes).

Usage notes

  • Plain readlink is shallow. It shows only the first hop and does not make the path absolute. If a -> b and b -> c, readlink a is b.
  • A relative target is relative to the link's directory, not to where you run readlink. Use -f to get an absolute path.
  • -f is a GNU extension. On macOS and BSD, readlink has different options (-f arrived only in recent versions); portable scripts use cd "$(dirname "$0")" && pwd -P instead.
  • Dangling links: plain readlink and -f succeed on a link whose target is missing; -e fails.
  • Script location: script_dir=$(dirname "$(readlink -f "${BASH_SOURCE[0]}")") finds the real directory of a script even when it is started through a symlink.
  • Quote the output: target=$(readlink -f -- "$file"). Use -- for names that begin with a dash.
  • -n matters for comparisons: command substitution already strips trailing newlines, so -n is mostly useful when writing to a file.
  • Compare two paths by resolving both: [[ $(readlink -f a) == $(readlink -f b) ]], or use [[ a -ef b ]] for the same-file test.
  • Speed: each call is a process start. In a loop over thousands of files, use find -lname or stat -c %N once.