Linux realpath Command
realpath is a practical command-line tool in Linux systems, used to resolve the absolute path of a file or directory (an absolute path is the complete path starting from the root directory /). It resolves all symbolic links and normalizes references in the path to.(current directory) and..(parent directory).
Why do you need realpath
In daily work, we often encounter the following situations:
- Need to get the absolute path of a script or configuration file
- When dealing with paths containing symbolic links, want to know the actual location they point to
- Normalize user-input paths to eliminate relative path references
- Ensure path accuracy in script programming
The realpath command is designed specifically to solve these problems.
Basic Syntax
realpath [选项] 文件或目录...
Explanation of Common Options
| Option | Description |
|---|---|
-eor--canonicalize-existing |
Only resolve paths that actually exist (default behavior) |
-mor--canonicalize-missing |
Resolve the path even if it does not exist |
-Lor--logical |
When resolving symbolic links, do not follow symbolic links |
-Por--physical |
When resolving symbolic links, follow all symbolic links (default behavior) |
-qor--quiet |
Silent mode, do not display error messages |
--relative-to=DIR |
Return a path relative to the specified directory |
--relative-base=DIR |
Only normalize the path part under the specified directory |
-sor--stripor--no-symlinks |
Do not expand symbolic links |
-zor--zero |
Separate output with NUL characters instead of newline characters |
Usage Examples
Example 1: Get the absolute path of a file
Example
/home/user/documents/example.txt
Example 2: Resolve symbolic links
Suppose/usr/bin/pythonis a symbolic link pointing to/usr/bin/python3.8the symbolic link:
Example
/usr/bin/python3.8
Example 3: Handle a non-existent path
Example
/nonexistent/path
Example 4: Get a relative path
Example
documents/file.txt
Example 5: Process multiple files in batch
Example
/home/user/file1.txt
/home/user/file2.txt
/home/user/dir
Practical Application Scenarios
Scenario 1: Get the absolute path of the current script in a script
Example
SCRIPT_PATH=$(realpath "$0")
SCRIPT_DIR=$(dirname "$SCRIPT_PATH")
echo "The directory where the script is located:$SCRIPT_DIR"
Scenario 2: Compare whether two paths point to the same location
Example
path2=$(realpath "/actual/path")
if [ "$path1" = "$path2" ]; then
echo "The two paths point to the same location"
fi
Scenario 3: Normalize user-input paths
Example
normalized_path=$(realpath -m "$user_path")
echo "Normalized path:$normalized_path"
Frequently Asked Questions (FAQ)
Q1: What is the difference between realpath and readlink?
readlinkIt is mainly used to read the target pointed to by a symbolic link, whilerealpathprovides more comprehensive path resolution functionality, including handling relative path references and normalizing path formats.
Q2: Why does my system not have the realpath command?
realpath is part of GNU coreutils, but some stripped-down Linux distributions may not have it preinstalled. You can get it by installing the coreutils package:
Example
sudo apt install coreutils
# RHEL/CentOS
sudo yum install coreutils
Q3: How to implement similar functionality on systems that do not support realpath?
You can use a combination of commands:
Example
readlink -f /path/to/file
# Or
(cd /path/to/file && pwd -P)
Best Practice Recommendations
- Always use absolute paths in scripts: to avoid path issues caused by changes in the working directory
- Normalize paths first when handling user input: to prevent path injection attacks
- Check whether the path exists: use the
-eoption to ensure the path is valid - Consider the impact of symbolic links: clearly determine whether symbolic links need to be resolved
- Handle paths containing special characters such as spaces: always wrap path variables in quotes
Summary
realpath is a simple but powerful path processing tool, especially suitable for use in script programming and system administration. Through this article, you should have mastered:
- The basic functions and syntax of realpath
- The specific uses of various options
- Practical application scenarios and example code
- Solutions to common problems
- Best practices for usage
Now, you can start applying the realpath command in your projects to make path handling more reliable and secure!
Other Extensions
Linux Command Reference