File tree Expand file tree Collapse file tree 1 file changed +64
-0
lines changed
Expand file tree Collapse file tree 1 file changed +64
-0
lines changed Original file line number Diff line number Diff line change 1+ realpath
2+ ===
3+
4+ 解析并规范化文件路径,返回绝对路径。
5+
6+ ## 补充说明
7+
8+ ** realpath** 命令用于解析给定路径中的符号链接、相对路径(如 ` . ` 、` .. ` ),并输出其对应的** 绝对路径** 。
9+ 它常用于脚本中获取文件或目录的真实位置,避免因符号链接或相对路径导致的路径歧义。
10+
11+ 与直接使用 ` pwd ` 或字符串拼接不同,` realpath ` 能保证输出路径是唯一、真实、可访问的物理路径。
12+
13+ ### 语法
14+
15+ ``` shell
16+ realpath [选项] 文件...
17+ ```
18+
19+ ### 选项
20+
21+ ``` shell
22+ -e, --canonicalize-existing 仅在路径中所有组件都存在时才输出结果
23+ -m, --canonicalize-missing 即使路径中部分组件不存在也输出规范化路径
24+ -L, --logical 按逻辑方式解析符号链接(默认)
25+ -P, --physical 按物理方式解析符号链接
26+ -q, --quiet 静默模式,不输出错误信息
27+ -s, --strip 删除路径末尾的斜杠
28+ --relative-to=DIR 输出相对于 DIR 的路径
29+ --relative-base=DIR 若可能,输出相对于 DIR 的路径
30+ --help 显示帮助信息
31+ --version 显示版本信息
32+ ```
33+
34+ ### 参数
35+
36+ ``` shell
37+ 文件 需要解析的文件或目录路径,可以是相对路径或符号链接
38+ ```
39+
40+ ## 实例
41+
42+ ### 获取文件的绝对路径
43+
44+ ``` shell
45+ realpath file.txt
46+ ```
47+
48+ ### 解析符号链接的真实路径
49+
50+ ``` shell
51+ realpath /usr/bin/python
52+ ```
53+
54+ ### 即使路径不存在也返回规范化结果
55+
56+ ``` shell
57+ realpath -m ./not/exist/path
58+ ```
59+
60+ ### 输出相对于指定目录的路径
61+
62+ ``` shell
63+ realpath --relative-to=/usr /usr/bin/env
64+ ```
You can’t perform that action at this time.
0 commit comments