use-grealpath

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Use grealpath for Path Operations

使用grealpath进行路径操作

Rule (CRITICAL)

规则(严重级别)

ALWAYS use
grealpath
command for:
  • Calculating relative paths between files/directories
  • Converting relative paths to absolute paths
  • Resolving symbolic links to absolute paths
务必使用
grealpath
命令完成以下操作:
  • 计算文件/目录之间的相对路径
  • 将相对路径转换为绝对路径
  • 解析符号链接为绝对路径

Relative Path Calculation

相对路径计算

Use
grealpath --relative-to=<base>
to calculate relative paths:
bash
undefined
使用
grealpath --relative-to=<base>
来计算相对路径:
bash
undefined

Calculate 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

undefined
undefined

Absolute Path Conversion

绝对路径转换

Use
grealpath
to convert relative paths to absolute paths:
bash
undefined
使用
grealpath
将相对路径转换为绝对路径:
bash
undefined

Convert 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

undefined
undefined

Common Use Cases

常见使用场景

Getting Relative Path Between Two Files

获取两个文件之间的相对路径

bash
undefined
bash
undefined

From file A to file B

从文件A到文件B的相对路径

grealpath --relative-to=/path/to/fileA /path/to/fileB
undefined
grealpath --relative-to=/path/to/fileA /path/to/fileB
undefined

Converting Relative to Absolute

将相对路径转换为绝对路径

bash
undefined
bash
undefined

Simple conversion

简单转换

grealpath relative/path/to/file
grealpath relative/path/to/file

With base directory

指定基准目录

grealpath --relative-to=/base/dir relative/path
undefined
grealpath --relative-to=/base/dir relative/path
undefined

Resolving Paths with Symbolic Links

解析包含符号链接的路径

bash
undefined
bash
undefined

Resolve all symbolic links

解析所有符号链接

grealpath -s symlink
grealpath -s symlink

Or without -s flag (default behavior resolves symlinks)

或者不加-s参数(默认行为会解析符号链接)

grealpath symlink
undefined
grealpath symlink
undefined

Examples

示例

Good: Using grealpath

正确用法:使用grealpath

bash
undefined
bash
undefined

Calculate 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

undefined
undefined

Bad: Manual Path Manipulation

错误用法:手动处理路径

bash
undefined
bash
undefined

DO NOT manually calculate relative paths

请勿手动计算相对路径

DO NOT use cd and pwd combinations

请勿使用cd和pwd的组合

DO NOT use string manipulation for paths

请勿使用字符串操作处理路径

undefined
undefined

Notes

注意事项

  • grealpath
    is the GNU version of
    realpath
    (Mac uses BSD version by default)
  • Always use
    grealpath
    instead of
    realpath
    for consistent behavior
  • The
    --relative-to
    option requires GNU coreutils
  • Paths are normalized (removes
    .
    and
    ..
    components)
  • grealpath
    realpath
    的GNU版本(Mac默认使用BSD版本的realpath)
  • 为了行为一致性,请始终使用
    grealpath
    而非
    realpath
  • --relative-to
    选项需要GNU coreutils支持
  • 路径会被规范化(移除
    .
    ..
    组件)