Skip to content

Commit 99b6035

Browse files
authored
Add realpath command (#678)
Add documentation for the realpath command, including usage, options, and examples.
1 parent dd107af commit 99b6035

File tree

1 file changed

+64
-0
lines changed

1 file changed

+64
-0
lines changed

command/realpath.md

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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+
```

0 commit comments

Comments
 (0)