LinuxCommandLibrary

readlink

Show the target of a symbolic link

TLDR

Get the actual file to which the symlink points

$ readlink [path/to/file]
copy

Get the absolute path to a file
$ readlink [[-f|--canonicalize]] [path/to/file]
copy

SYNOPSIS

readlink [OPTION]... FILE...

PARAMETERS

-f, --canonicalize
    Canonicalize by following every symbolic link in every component of the given name recursively; all but the last component must exist.

-e, --canonicalize-existing
    Canonicalize by following every symbolic link in every component of the given name recursively, all components must exist.

-m, --canonicalize-missing
    Canonicalize by following every symbolic link in every component of the given name recursively, without requirements on components existence.

-n, --no-newline
    Do not output the trailing newline.

-v, --verbose
    Report error if destination cannot be determined

-q, --quiet
    Suppress most error messages

-z, --zero
    End each output line with NUL, not newline

--help
    Display help message and exit.

--version
    Output version information and exit.

DESCRIPTION

The readlink command in Linux is used to display the target of a symbolic link. A symbolic link, often referred to as a symlink, is a file system object that points to another file or directory. readlink retrieves and prints the path to which the symlink is linked.

By default, readlink prints the target as a string. It's a crucial tool for understanding file system structure and resolving symbolic links in scripts and programs. It can be used to determine the real location of a file or directory when the path encountered is actually a symlink. readlink can also be used to canonicalize paths, resolving symbolic links to produce an absolute path to the target file.

CAVEATS

readlink only resolves one level of symbolic links by default. Using the -f option will recursively resolve all symlinks in the path. If the specified file is not a symbolic link, readlink will not output anything unless the -v option is used, and it will produce an error.

EXIT STATUS

readlink exits with a status of 0 if successful. It exits with a non-zero status if an error occurs, such as the specified file not existing or not being a symbolic link.

EXAMPLES

To display the target of a symlink named 'my_link':
readlink my_link

To get the absolute path of the target of a symlink named 'my_link':
readlink -f my_link

HISTORY

The readlink command has been a standard part of Unix-like operating systems, including Linux, for a considerable amount of time. Its purpose has remained consistent throughout its history: to provide a way to determine the target of a symbolic link. The specific implementations and available options might vary slightly across different Unix variants and versions of GNU coreutils. Over time, features like canonicalization (resolving to absolute paths) have been enhanced through options like -f.

SEE ALSO

ln(1), find(1), stat(1)

Copied to clipboard