Main Options
`readlink` performs a simple function of reading the path of a symbolic link, so there are not many options. It is primarily used to find the actual path of the link.
1. Link Information
Generated command:
Try combining the commands.
Description:
`readlink` Executes the command.
Combine the above options to virtually execute commands with AI.
Commonly Used Examples
`readlink` is primarily used to check the actual target of a symbolic link or to obtain the absolute path of a file in scripts. Learn how to use it through the examples below.
Check Symbolic Link Target
echo 'Hello World' > original_file.txt
ln -s original_file.txt link_to_file.txt
readlink link_to_file.txt
First, create a symbolic link named `link_to_file.txt` using the `ln -s` command, then use `readlink` to check the original file path.
Find Actual Absolute Path of File
readlink -f link_to_file.txt
Outputs the absolute path of the actual file pointed to by the symbolic link in the current directory. The `-f` option follows the link to reach the actual file.
Use Absolute Path in Script
SCRIPT_PATH=$(readlink -f "$0")
SCRIPT_DIR=$(dirname "$SCRIPT_PATH")
echo "Absolute path of the script: $SCRIPT_PATH"
echo "Directory of the script: $SCRIPT_DIR"
In scripts, you can combine `readlink -f` and `dirname` to reference other files based on the absolute path of the current script.
Difference Between readlink and ls -l
`ls -l` also shows the target of a symbolic link, but `readlink` is specialized for cleanly outputting the original path string for automated processing in scripts.
- ls -l: Outputs the target along with detailed information about the link. (e.g., `link_to_file.txt -> original_file.txt`)
- readlink: Outputs only the pure target path string. (e.g., `original_file.txt`)