readlink
Show the target of a symbolic link
TLDR
Get the actual file to which the symlink points
Get the absolute path to a file
SYNOPSIS
readlink [OPTION]... FILE...
PARAMETERS
-f, --canonicalize-existing
Canonicalize by following every component of the path, checking that each component exists and is a directory (except for the last one). All symbolic links are resolved, and the resulting path is absolute.
-e, --canonicalize-existing
This option is equivalent to -f. It canonicalizes the path by following all symbolic links and requiring all components to exist.
-m, --canonicalize-missing
Canonicalize by following every component of the path, but do not require them to exist. This can be useful for constructing paths that may not yet exist but need to be resolved against existing components.
-n, --no-newline
Do not output the trailing newline character. Useful for assigning the output directly to a shell variable without extra whitespace.
-q, --quiet, --silent
Suppress most (or all) error messages. When used, readlink will still return a non-zero exit status on errors, but will not print diagnostic messages to standard error.
-v, --verbose
Report messages about non-existent source or errors. This is usually the default behavior without -q.
--help
Display a help message and exit.
--version
Output version information and exit.
DESCRIPTION
The readlink command is used to display the value of a symbolic link or to canonicalize a path by resolving all its components.
When given a symbolic link, readlink by default simply prints the string that the symbolic link points to, without checking if the target actually exists. For example, if 'mylink' points to '../targetfile', readlink mylink will output '../targetfile'.
Its more powerful modes, enabled by options like -f, -e, or -m, allow it to follow all symbolic links in a path and resolve it to its canonical, absolute form. This means it will traverse directories and links until it reaches a path with no symbolic link components left, typically an absolute path. This is useful for scripts that need to work with real file paths, regardless of how they were referenced.
readlink is a lightweight tool, part of the GNU core utilities, often used in shell scripts for robust path handling.
CAVEATS
Differences exist between GNU readlink (common on Linux) and BSD readlink (common on macOS/FreeBSD). GNU versions typically offer more robust options like -f, -e, and -m for path canonicalization, which may not be present in BSD variants.
When using -m, be aware that the resulting path might contain components that do not exist, and its meaning can be ambiguous if used without careful consideration.
EXIT STATUS
readlink exits with a status of 0 upon successful operation. A non-zero exit status indicates an error, such as a file not found, permission denied, or an invalid option. Scripts should always check the exit status for reliable error handling.
SYMLINK VALUE VS. CANONICAL PATH
It's crucial to distinguish between merely reading the value of a symbolic link (the path string it contains, e.g., using readlink mylink) and resolving a canonical path (following all links and removing `.` or `..` components to get an absolute, true path, e.g., using readlink -f mylink). The former gives you the link's raw content, the latter gives you the ultimate, absolute destination.
HISTORY
The concept of resolving symbolic links is fundamental to Unix-like operating systems, implemented via the readlink(2) system call. The readlink utility itself is a standard tool provided by the GNU Core Utilities on Linux systems, translating the system call's functionality into a convenient command-line interface. Its development has focused on providing robust path canonicalization features, making it an indispensable tool for shell scripting and system administration.