Linux Comm Realpath
[ Linux Command Encyclopaedia](#)\n\n* * *\n\nrealpath 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 the `.` (current directory) and `..` (parent directory) references in the path.\n\n* * *\n\n## Why Use realpath\n\nIn daily work, we often encounter the following situations:\n\n* Need to get the absolute path of a script or configuration file\n* When handling paths containing symbolic links, want to know the actual location they point to\n* Normalize user input paths to eliminate relative path references\n* Ensure path accuracy in script programming\n\nThe realpath command is designed precisely to solve these problems.\n\n* * *\n\n## Basic Syntax\n\nrealpath file or directory...\n\n* * *\n\n## Common Options Explained\n\n| Option | Description |\n| --- | --- |\n| `-e` or `--canonicalize-existing` | Only resolve paths that actually exist (default behavior) |\n| `-m` or `--canonicalize-missing` | Resolve paths even if they don't exist |\n| `-L` or `--logical` | When resolving symbolic links, do not follow symbolic links |\n| `-P` or `--physical` | When resolving symbolic links, follow all symbolic links (default behavior) |\n| `-q` or `--quiet` | Quiet mode, do not display error messages |\n| `--relative-to=DIR` | Return the path relative to the specified directory |\n| `--relative-base=DIR` | Only normalize the path portion under the specified directory |\n| `-s` or `--strip` or `--no-symlinks` | Do not expand symbolic links |\n| `-z` or `--zero` | Use NUL character instead of newline to separate output |\n\n* * *\n\n## Usage Examples\n\n### Example 1: Get the absolute path of a file\n\n## Example\n\n$ realpath example.txt\n\n/home/user/documents/example.txt\n\n### Example 2: Resolve symbolic links\n\nAssume `/usr/bin/python` is a symbolic link pointing to `/usr/bin/python3.8`:\n\n## Example\n\n$ realpath /usr/bin/python\n\n/usr/bin/python3.8\n\n### Example 3: Handle non-existent paths\n\n## Example\n\n$ realpath -m/nonexistent/path\n\n/nonexistent/path\n\n### Example 4: Get relative path\n\n## Example\n\n$ realpath --relative-to=/home/user /home/user/documents/file.txt\n\n documents/file.txt\n\n### Example 5: Batch process multiple files\n\n## Example\n\n$ realpath file1.txt file2.txt dir/\n\n/home/user/file1.txt\n\n/home/user/file2.txt\n\n/home/user/dir\n\n* * *\n\n## Practical Application Scenarios\n\n### Scenario 1: Get the absolute path of the current script in a script\n\n## Example\n\n#!/bin/bash\n\nSCRIPT_PATH=$(realpath "$0")\n\nSCRIPT_DIR=$(dirname"$SCRIPT_PATH")\n\necho"Script directory: $SCRIPT_DIR"\n\n### Scenario 2: Compare whether two paths point to the same location\n\n## Example\n\npath1=$(realpath "/path/to/link")\n\npath2=$(realpath "/actual/path")\n\nif["$path1" = "$path2"]; then\n\necho"Both paths point to the same location"\n\nfi\n\n### Scenario 3: Normalize user input paths\n\n## Example\n\nread-p"Please enter file path: " user_path\n\nnormalized_path=$(realpath -m"$user_path")\n\necho"Normalized path: $normalized_path"\n\n* * *\n\n## Frequently Asked Questions\n\n### Q1: What is the difference between realpath and readlink?\n\n`readlink` is mainly used to read the target that a symbolic link points to, while `realpath` provides more comprehensive path resolution functions, including handling relative path references and normalizing path formats.\n\n### Q2: Why doesn't my system have the realpath command?\n\nrealpath is part of GNU coreutils, but some lightweight Linux distributions may not have it pre-installed. You can obtain it by installing the coreutils package:\n\n## Example\n\n# Debian/Ubuntu\n\nsudo apt install coreutils\n\n# RHEL/CentOS\n\nsudo yum install coreutils\n\n### Q3: How to implement similar functionality on systems that don't support realpath?\n\nYou can use combined commands:\n\n## Example\n\n# Get absolute path\n\nreadlink-f/path/to/file\n\n# Or\n\n(cd/path/to/file&& pwd -P)\n\n* * *\n\n## Best Practice Recommendations\n\n1. **Always use absolute paths in scripts**: Avoid path issues caused by working directory changes\n2. **Normalize paths when processing user input**: Prevent path injection attacks\n3. **Check if paths exist**: Use the `-e` option to ensure paths are valid\n4. **Consider the impact of symbolic links**: Clarify whether you need to resolve symbolic links\n5. **Handle paths with special characters like spaces**: Always quote path variables\n\n* * *\n\n## Summary\n\nrealpath 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:\n\n* The basic functions and syntax of realpath\n* The specific uses of various options\n* Practical application scenarios and example code\n* Solutions to common problems\n* Best practices for use\n\nNow, you can start applying the realpath command in your projects to make path processing more reliable and secure!\n\n* * Linux Command Encyclopaedia](#)
YouTip