国际访客建议访问 Primers 编程伙伴 国际版站点 > Bash 教程 > realpath 以获得更好的体验。

# Bash 的 realpath 命令

realpath [OPTION]... FILE...

!subtitle:功能

解析路径。

!subtitle:类型

可执行文件(/usr/bin/realpath),属于 coreutils

!subtitle:参数

  • OPTION 选项:

    • -e, --canonicalize-existing - 路径的所有组成部分都必须存在

    • -m, --canonicalize-missing - 路径的所有组成部分都可以不存在

    • -L, --logical - 逻辑路径;在跟踪符号链接之前解析 ..

    • -P, --physical - 物理路径;在跟踪符号链接之后解析 ..(默认)

    • -q, --quiet - 忽略大部分错误信息

    • --relative-to=DIR - 打印相对于 DIR 的相对路径

    • --relative-base=DIR - 如果路径在 DIR 内,则相对路径,否则打印绝对路径

    • -s, --strip, --no-symlinks - 不展开符号链接

    • -z, --zero - 以空字符(\0)作为行的结尾,而不是换行符(\n

    • --help - 显示帮助

    • --version - 显示版本

  • FILE - 文件路径

# 示例

!subtitle:绝对路径

$ pwd                                                   # 查看当前路径
/home/user/primers
$ realpath -m ./file.txt
/home/user/primers/file.txt
$ realpath -m ../data/../config/./settings.yaml
/home/user/config/settings.yaml

!subtitle:相对路径

$ pwd                                                   # 查看当前路径
/home/user/primers
$ realpath -m --relative-to=/home/user ./file.txt       # 打印相对于 /home/user 的路径
primers/file.txt
$ realpath -m --relative-to=/home/user2 ./file.txt      # 打印相对于 /home/user2 的路径
../user/primers/file.txt
$ realpath -m --relative-base=/home/user ./file.txt     # 在 /home/user 内,打印相对于 /home/user 的路径
primers/file.txt
$ realpath -m --relative-base=/home/user2 ./file.txt    # 不在 /home/user 内,打印绝对路径
/home/user/primers/file.txt

!subtitle:符号链接

$ pwd                                                   # 查看当前路径
/home/user/primers
$ ln -s /usr/lib lib                                    # 创建符号链接
$ ls -l lib
lrwxrwxrwx 1 planc planc 8 Oct 31 14:06 lib -> /usr/lib
$ realpath ./lib                                        # 跟踪符号链接(默认)
/usr/lib
$ realpath -s ./lib                                     # 不跟踪符号链接
/home/user/primers/lib
$ realpath ./lib/..                                     # 在跟踪符号链接之后解析 ..(默认)
/usr
$ realpath -L ./lib/..                                  # 在跟踪符号链接之前解析 ..
/home/user/primers

# 相关命令

命令 说明
basename 从文件路径中去除目录和后缀
dirname 从文件路径中提取目录

# 推荐阅读

# 手册

REALPATH(1)                      User Commands                     REALPATH(1)

NAME
       realpath - print the resolved path

SYNOPSIS
       realpath [OPTION]... FILE...

DESCRIPTION
       Print  the resolved absolute file name; all but the last component must
       exist

       -e, --canonicalize-existing
              all components of the path must exist

       -m, --canonicalize-missing
              no path components need exist or be a directory

       -L, --logical
              resolve '..' components before symlinks

       -P, --physical
              resolve symlinks as encountered (default)

       -q, --quiet
              suppress most error messages

       --relative-to=DIR
              print the resolved path relative to DIR

       --relative-base=DIR
              print absolute paths unless paths below DIR

       -s, --strip, --no-symlinks
              don't expand symlinks

       -z, --zero
              end each output line with NUL, not newline

       --help display this help and exit

       --version
              output version information and exit

AUTHOR
       Written by Padraig Brady.

REPORTING BUGS
       GNU coreutils online help: <https://www.gnu.org/software/coreutils/>
       Report any translation bugs to <https://translationproject.org/team/>

COPYRIGHT
       Copyright © 2023 Free Software Foundation, Inc.   License  GPLv3+:  GNU
       GPL version 3 or later <https://gnu.org/licenses/gpl.html>.
       This  is  free  software:  you  are free to change and redistribute it.
       There is NO WARRANTY, to the extent permitted by law.

SEE ALSO
       readlink(1), readlink(2), realpath(3)

       Full documentation <https://www.gnu.org/software/coreutils/realpath>
       or available locally via: info '(coreutils) realpath invocation'

GNU coreutils 9.4                 April 2024                       REALPATH(1)
本文 更新于: 2025-11-27 09:38:15 创建于: 2025-11-27 09:38:15