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.
readlink [OPTION]... FILE...
Try it
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.
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
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
readlinkis shallow. It shows only the first hop and does not make the path absolute. Ifa -> bandb -> c,readlink aisb. - A relative target is relative to the link's directory, not to where you run
readlink. Use-fto get an absolute path. -fis a GNU extension. On macOS and BSD,readlinkhas different options (-farrived only in recent versions); portable scripts usecd "$(dirname "$0")" && pwd -Pinstead.- Dangling links: plain
readlinkand-fsucceed on a link whose target is missing;-efails. - 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. -nmatters for comparisons: command substitution already strips trailing newlines, so-nis 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 -lnameorstat -c %Nonce.
Related
realpath,ln,dirname,pwd- Lessons: Hard and symbolic links, Navigating the filesystem