首页·终端命令

realpath

文件与目录

用法

realpath [path]

把任意路径解成一条绝对路径,符号链接和点号都展开;macOS 上是 BSD 版,没有 --relative-to 这类 GNU 选项。

常用选项

选项含义
-qBSD and macOS: stay quiet when a path cannot be resolved
-eGNU: fail unless every component exists
-mGNU: never fail, even if nothing exists
-sGNU: do not expand symlinks
--relative-toGNU: print the answer relative to a directory

示例

realpath ../src/./index.ts

Prints one absolute path with dots resolved.

realpath --relative-to=/var /var/log/nginx

Prints log/nginx (GNU only).

移动、删除和查找。有些命令不会再问一遍,所以顺手加 -i 是在救自己。

怎么看

  • 方括号 [ ] 表示这部分可以不写。
  • 省略号 … 表示可以写多个。
  • 选项区分大小写——有些命令里 -r 和 -R 不是一回事。

常见问题

Q. realpath 是做什么的?

把任意路径解成一条绝对路径,符号链接和点号都展开;macOS 上是 BSD 版,没有 --relative-to 这类 GNU 选项。

Q. 怎么写?

realpath [path] —— 方括号表示可以省略的部分。

Q. 值得记的选项有几个?

这里列了 5 个,完整列表在 man realpath。这条命令属于文件与目录。

相关命令

man 手册: man realpath