Linux command
realpath 命令
文件
复制后可按需替换文件名、目录或参数。
常用示例
Get absolute path
realpath [file.txt]
Resolve symlinks
realpath [symlink]
Get path without resolving symlinks
realpath -s [symlink]
Get path relative
realpath --relative-to=[/base/path] [/full/path/file]
Get relative base path
realpath --relative-base=[/base] [/base/subdir/file]
Get absolute paths
realpath [file1] [file2] [file3]
Require all path components to exist
realpath -e [path/to/file]
Quiet mode
realpath -q [file]
说明
realpath prints the resolved absolute pathname of files. It expands all symbolic links, resolves references to /./, /../, and removes extra slashes to produce a canonical path. The tool is useful in shell scripts when you need the absolute path of a file regardless of how it was specified (relative path, symlink, etc.). It can also compute relative paths between two locations. realpath is part of GNU coreutils and provides similar functionality to the readlink command with -f flag.
参数
- -e, --canonicalize-existing
- All path components must exist.
- -m, --canonicalize-missing
- No path component needs to exist.
- -L, --logical
- Resolve .. before symlinks.
- -P, --physical
- Resolve symlinks first (default).
- -s, --strip, --no-symlinks
- Don't resolve symlinks.
- --relative-to= _dir_
- Print path relative to directory.
- --relative-base= _dir_
- Print relative path if below base.
- -q, --quiet
- Suppress error messages.
- -z, --zero
- End each output line with NUL.
FAQ
What is the realpath command used for?
realpath prints the resolved absolute pathname of files. It expands all symbolic links, resolves references to /./, /../, and removes extra slashes to produce a canonical path. The tool is useful in shell scripts when you need the absolute path of a file regardless of how it was specified (relative path, symlink, etc.). It can also compute relative paths between two locations. realpath is part of GNU coreutils and provides similar functionality to the readlink command with -f flag.
How do I run a basic realpath example?
Run `realpath [file.txt]` in a terminal, then adjust file names, paths, flags, or remote targets for your system.
What does -e, --canonicalize-existing do in realpath?
All path components must exist.