本文转自【Linux 命令:realpath 命令_skydust1979的博客-CSDN博客_realpath】,有删改和扩充。
CONTENTS
命令简介
realpath 用于获取指定目录或文件的绝对路径。编写 Shell 脚本中,通常会使用相对路径来指明文件,但有时候,我们需要用到绝对路径,此时可以使用 realpath 来获取。
命令格式
realpath [OPTIONS] FILES
选项说明
-e, --canonicalize-existing 文件 FILE 的所有组成部件必须都存在 -m, --canonicalize-missing 文件 FILE 的组成部件可以不存在 -L, --logical 在软链接之前解析父目录 .. -P, --physical 解析软链接,默认动作 -q, --quiet 静默模式输出,禁止显示大多数错误消息 --relative-to=DIR 相对于目录 DIR 的路径 --relative-base=DIR 如果文件在基目录 DIR下,打印结果会省去基目录,否则打印绝对路径 -s, --strip, --no-symlinks 不扩展软链接 -z, --zero 不分隔输出,即所有的输出均在一行而不是单独每行 --help 显示帮助信息 --version 显示版本信息
常用示例
打印指定文件的绝对路径。
root@7b84fe28f86f:/mnt/share/unit_test/test# pwd /mnt/share/unit_test/test root@7b84fe28f86f:/mnt/share/unit_test/test# realpath hello.bin /mnt/share/unit_test/test/hello.bin
计算子目录相对父目录的相对路径。
root@7b84fe28f86f:/mnt/share/unit_test/test# ls hello.bin root@7b84fe28f86f:/mnt/share/unit_test/test# realpath --relative-to=hello.bin /mnt/share ../../..
计算父目录相对子目录路径。
root@7b84fe28f86f:/mnt/share/unit_test/test# ls hello.bin root@7b84fe28f86f:/mnt/share/unit_test/test# realpath --relative-base=/mnt/share hello.bin unit_test/test/hello.bin
发表评论