Linux realpath command
realpath is a practical command-line tool in Linux for resolving the absolute path of a file or directory (an absolute path is a complete path starting from the root directory /). It resolves all symbolic links and normalizes .. (current directory) and .... (parent directory) references.
Why do you need realpath
In daily work, we often encounter the following situations:
- You need to obtain the absolute path of a script or configuration file
- When working with paths containing symbolic links, you want to know where they actually point
- Normalize user-supplied paths, eliminating relative path references
- Ensure path accuracy in script programming
The realpath command is designed precisely to solve these problems.
Basic Syntax
realpath [选项] 文件或目录...
Details of common options
| Option | Description |
|---|---|
-e or --canonicalize-existing |
Resolve only paths that actually exist (default behavior) |
-m or --canonicalize-missing |
Resolve a path even if it does not exist |
-L or --logical |
When resolving symbolic links, do not follow them |
-P or --physical |
When resolving symbolic links, follow all of them (default behavior) |
-q or --quiet |
Silent mode, do not display error messages |
--relative-to=DIR |
Return the path relative to the specified directory |
--relative-base=DIR |
Normalize only the portion of the path under the specified directory |
-s or --strip or --no-symlinks |
Do not expand symbolic links |
-z or --zero |
Separate output with NUL characters instead of newlines |
Usage examples
Example 1: Get the absolute path of a file
Example
$ realpath example.txt
/
home
/
user
/
documents
/
example.txt
Example 2: Resolve a symbolic link
Assume /usr/bin/python is a symbolic link pointing to /usr/bin/python3.8 :
Example
$ realpath
/
usr
/
bin
/
python
/
usr
/
bin
/
python3.8
Example 3: Handle a path that does not exist
Example
$ realpath
-m
/
nonexistent
/
path
/
nonexistent
/
path
Example 4: Get a relative path
Example
$ realpath
--relative-to
=
/
home
/
user
/
home
/
user
/
documents
/
file.txt
documents
/
file.txt
Example 5: Process multiple files in batch
Example
$ realpath file1.txt file2.txt dir
/
/
home
/
user
/
file1.txt
/
home
/
user
/
file2.txt
/
home
/
user
/
dir
Practical Application Scenarios
Scenario 1: Get the absolute path of the current script
Example
#!/bin/bash
SCRIPT_PATH
=$
(
realpath
"$0"
)
SCRIPT_DIR
=$
(
dirname
"$SCRIPT_PATH"
)
echo
"Script directory: $SCRIPT_DIR"
Scenario 2: Compare whether two paths point to the same location
Example
path1
=$
(
realpath
"/path/to/link"
)
path2
=$
(
realpath
"/actual/path"
)
if
[
"$path1"
=
"$path2"
]
;
then
echo
"The two paths point to the same location"
fi
Scenario 3: Normalize a user-supplied path
Example
read
-p
"Please enter the file path: "
user_path
normalized_path
=$
(
realpath
-m
"$user_path"
)
echo
"Normalized path: $normalized_path"
Frequently Asked Questions
Q1: What is the difference between realpath and readlink?
readlink is mainly used to read the target that a symbolic link points to, while realpath provides more comprehensive path resolution, including handling relative path references and normalizing path format.
Q2: Why doesn't my system have the realpath command?
realpath is part of GNU coreutils, but some minimal Linux distributions may not have it preinstalled. You can obtain it by installing the coreutils package:
Example
Debian/Ubuntu
sudo
apt
install
coreutils
RHEL/CentOS
sudo
yum install
coreutils
Q3: How can I achieve similar functionality on a system that does not support realpath?
You can use a combination of commands:
Example
Get the absolute path
readlink
-f
/
path
/
to
/
file
or
(
cd
/
path
/
to
/
file
&
amp;
&
amp;
pwd
-P
)
Best Practice Recommendations
- Always use absolute paths in scripts: avoids path problems caused by changes to the working directory
- Normalize paths first when handling user input: prevents path injection attacks
- Check whether the path exists: When using
-eoption ensures the path is valid - Consider the impact of symbolic links: clarify 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 yet powerful path-handling tool, especially suited 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 each option
- practical application scenarios and sample code
- Solutions to common problems
- best practices for using it
Now you can start applying the realpath command in your projects to make path handling more reliable and secure!
Linux Command Reference