Linux realpath Command

Linux 命令大全Linux Command Reference


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

$ realpath example.txt
/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

$ realpath /usr/bin/python
/usr/bin/python3.8

Example 3: Handle a non-existent path

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 in a script

Example

#!/bin/bash
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

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 user-input paths

Example

read -p "Please enter a file path: " user_path
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

# Debian/Ubuntu
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

# Get the absolute path
readlink -f /path/to/file

# Or
(cd /path/to/file && pwd -P)

Best Practice Recommendations

  1. Always use absolute paths in scripts: to avoid path issues caused by changes in the working directory
  2. Normalize paths first when handling user input: to prevent path injection attacks
  3. Check whether the path exists: use the-eoption to ensure the path is valid
  4. Consider the impact of symbolic links: clearly determine whether symbolic links need to be resolved
  5. 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!


Linux 命令大全Linux Command Reference

Other Extensions