readlink command in Linux with Examples
The 'readlink' command in Linux is a valuable tool used to print resolved symbolic links or canonical file names. In simpler terms, when dealing with symbolic links and you need to know the actual path they represent, the 'readlink' command reveals the path of the symbolic link. This command is particularly useful for system administrators, developers, and users who work with complex directory structures and need to trace symbolic links effectively.
What is the 'readlink' Command?
Symbolic links in Linux are pointers to another file or directory. The 'readlink' command helps you determine the exact path that these links point to. It's an essential command for managing symbolic links and understanding their relationships within the file system.
Syntax:
readlink [OPTION]... FILE...
'readlink' command Example in Linux
It will print the print resolved symbolic links or canonical file names of the symbolic link passed with the command as shown below.

Explanation: As you can see in the above example we have a symbolic link with the name of the 'desk' with the help of 'readlink' command we will be able to identify its actual path.
Common Options with 'readlink' Command
1. readlink -f
This option canonicalize by following every symlink in every component of the given name recursively; all but the last component must exist.
Example:
readlink -f desk1

This command will display the resolved path by recursively following all symbolic links up to the final component, even if the final component does not exist.
2. readlink -e
This option will canonicalize by following every symlink in every component of the given name recursively, all components must exist.
Example:
readlink -e desk

3. readlink -m
This option canonicalize by following every symlink in every component of the given name recursively, without requirements on components existence.
Example:
readlink -m desk3

This option is useful when you want to resolve a path even if some of the components in the path do not exist.
4. readlink -n
This option will do not output the trailing delimiter.
Example:
readlink -n desk4

This can be useful when you need to combine the output of 'readlink' with other commands or scripts without additional line breaks.
5. readlink -q
This option will execute in quiet mode.
Example:
readlink -q desk

6. readlink -s
This option will suppress most error messages.
Example:
readlink -s desk5

7. readlink -v
This option will report error messages if any.
Example:
readlink -v desk6

This option is useful when troubleshooting symbolic links, as it provides a complete output with any error messages.
8. readlink -z
This option will end each output line with NUL, not newline.
Example:
readlink -z desk2

This is particularly useful when processing output in environments where newline characters might interfere with data processing.
9. readlink --help
This option will display this help and exit.
Example:
readlink --help

10. readlink --version
This option will show the version information and exit.
Example:
readlink --version
