use-grealpath
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseUse grealpath for Path Operations
使用grealpath进行路径操作
Rule (CRITICAL)
规则(严重级别)
ALWAYS use command for:
grealpath- Calculating relative paths between files/directories
- Converting relative paths to absolute paths
- Resolving symbolic links to absolute paths
务必使用命令完成以下操作:
grealpath- 计算文件/目录之间的相对路径
- 将相对路径转换为绝对路径
- 解析符号链接为绝对路径
Relative Path Calculation
相对路径计算
Use to calculate relative paths:
grealpath --relative-to=<base>bash
undefined使用来计算相对路径:
grealpath --relative-to=<base>bash
undefinedCalculate relative path from base directory to target
计算从基准目录到目标路径的相对路径
grealpath --relative-to=/home/user /home/user/test
grealpath --relative-to=/home/user /home/user/test
Output: test
输出: test
Calculate relative path from current directory
计算从当前目录出发的相对路径
grealpath --relative-to=. ./subdir/file.txt
grealpath --relative-to=. ./subdir/file.txt
Output: subdir/file.txt
输出: subdir/file.txt
Calculate relative path between two specific paths
计算两个指定路径之间的相对路径
grealpath --relative-to=/path/to/base /path/to/base/subdir/file.txt
grealpath --relative-to=/path/to/base /path/to/base/subdir/file.txt
Output: subdir/file.txt
输出: subdir/file.txt
undefinedundefinedAbsolute Path Conversion
绝对路径转换
Use to convert relative paths to absolute paths:
grealpathbash
undefined使用将相对路径转换为绝对路径:
grealpathbash
undefinedConvert relative path to absolute path
将相对路径转换为绝对路径
grealpath ./subdir/file.txt
grealpath ./subdir/file.txt
Output: /home/user/project/subdir/file.txt
输出: /home/user/project/subdir/file.txt
Convert with current directory as base
以当前目录为基准转换为绝对路径
grealpath file.txt
grealpath file.txt
Output: /home/user/project/file.txt
输出: /home/user/project/file.txt
Resolve symbolic links to absolute paths
解析符号链接为绝对路径
grealpath symlink
grealpath symlink
Output: /home/user/project/actual/path
输出: /home/user/project/actual/path
undefinedundefinedCommon Use Cases
常见使用场景
Getting Relative Path Between Two Files
获取两个文件之间的相对路径
bash
undefinedbash
undefinedFrom file A to file B
从文件A到文件B的相对路径
grealpath --relative-to=/path/to/fileA /path/to/fileB
undefinedgrealpath --relative-to=/path/to/fileA /path/to/fileB
undefinedConverting Relative to Absolute
将相对路径转换为绝对路径
bash
undefinedbash
undefinedSimple conversion
简单转换
grealpath relative/path/to/file
grealpath relative/path/to/file
With base directory
指定基准目录
grealpath --relative-to=/base/dir relative/path
undefinedgrealpath --relative-to=/base/dir relative/path
undefinedResolving Paths with Symbolic Links
解析包含符号链接的路径
bash
undefinedbash
undefinedResolve all symbolic links
解析所有符号链接
grealpath -s symlink
grealpath -s symlink
Or without -s flag (default behavior resolves symlinks)
或者不加-s参数(默认行为会解析符号链接)
grealpath symlink
undefinedgrealpath symlink
undefinedExamples
示例
Good: Using grealpath
正确用法:使用grealpath
bash
undefinedbash
undefinedCalculate relative path
计算相对路径
grealpath --relative-to=/home/user /home/user/project/src/file.ts
grealpath --relative-to=/home/user /home/user/project/src/file.ts
Output: project/src/file.ts
输出: project/src/file.ts
Convert to absolute path
转换为绝对路径
grealpath ./src/file.ts
grealpath ./src/file.ts
Output: /home/user/project/src/file.ts
输出: /home/user/project/src/file.ts
Resolve symbolic link
解析符号链接
grealpath ./link-to-file
grealpath ./link-to-file
Output: /home/user/project/actual/file.ts
输出: /home/user/project/actual/file.ts
undefinedundefinedBad: Manual Path Manipulation
错误用法:手动处理路径
bash
undefinedbash
undefinedDO NOT manually calculate relative paths
请勿手动计算相对路径
DO NOT use cd and pwd combinations
请勿使用cd和pwd的组合
DO NOT use string manipulation for paths
请勿使用字符串操作处理路径
undefinedundefinedNotes
注意事项
- is the GNU version of
grealpath(Mac uses BSD version by default)realpath - Always use instead of
grealpathfor consistent behaviorrealpath - The option requires GNU coreutils
--relative-to - Paths are normalized (removes and
.components)..
- 是
grealpath的GNU版本(Mac默认使用BSD版本的realpath)realpath - 为了行为一致性,请始终使用而非
grealpathrealpath - 选项需要GNU coreutils支持
--relative-to - 路径会被规范化(移除和
.组件)..