use-gnu-coreutils
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseUse GNU Coreutils with 'g' Prefix
使用带'g'前缀的GNU Coreutils
Rule (CRITICAL)
规则(严重级别)
ALWAYS use GNU coreutils commands with prefix instead of Mac default BSD commands.
gNEVER use Mac standard commands directly.
必须始终使用带前缀的GNU coreutils命令,替代Mac默认的BSD命令。
g绝对不要直接使用Mac标准命令。
Command Mapping
命令映射
| GNU Command (Use) | Mac BSD Command (Prohibited) | Description |
|---|---|---|
| | List directory contents |
| | Find files |
| | Stream editor |
| | Pattern scanning and processing |
| | Search patterns |
| | Concatenate files |
| | Copy files |
| | Move files |
| | Remove files |
| | Create directories |
| | Change file permissions |
| | Change file ownership |
| | Display/set date |
| | Change file timestamps |
| | Display first lines |
| | Display last lines |
| | Sort lines |
| | Remove duplicate lines |
| | Word count |
| | Cut fields |
| | Translate characters |
| | Build and execute commands |
| | Resolve absolute paths |
| | Display file status |
| | Read symbolic links |
| | Create links |
| | Shuffle lines |
| | Split files |
| | Base64 encode/decode |
| | MD5 checksum |
| | SHA256 checksum |
| 推荐使用的GNU命令 | 禁止使用的Mac BSD命令 | 描述 |
|---|---|---|
| | 列出目录内容 |
| | 查找文件 |
| | 流编辑器 |
| | 模式扫描与处理 |
| | 模式搜索 |
| | 合并文件内容 |
| | 复制文件 |
| | 移动文件 |
| | 删除文件 |
| | 创建目录 |
| | 修改文件权限 |
| | 修改文件所有权 |
| | 显示/设置日期 |
| | 修改文件时间戳 |
| | 显示文件前几行 |
| | 显示文件最后几行 |
| | 排序行内容 |
| | 移除重复行内容 |
| | 字数统计 |
| | 提取字段内容 |
| | 字符转换 |
| | 构建并执行命令 |
| | 解析绝对路径 |
| | 显示文件状态 |
| | 读取符号链接 |
| | 创建链接 |
| | 随机打乱行内容 |
| | 拆分文件 |
| | Base64编码/解码 |
| | MD5校验和 |
| | SHA256校验和 |
Examples
示例
Good: Using GNU Commands
正确用法:使用GNU命令
bash
undefinedbash
undefinedList files with GNU ls
使用GNU ls列出文件
gls -la
gls -la
Find files with GNU find
使用GNU find查找文件
gfind . -name "*.ts" -type f
gfind . -name "*.ts" -type f
Text processing with GNU sed
使用GNU sed处理文本
gsed -i 's/old/new/g' file.txt
gsed -i 's/old/new/g' file.txt
Pattern matching with GNU grep
使用GNU grep匹配模式
ggrep -r "pattern" .
ggrep -r "pattern" .
Path operations with GNU realpath
使用GNU realpath处理路径
grealpath --relative-to=/base /target
grealpath --relative-to=/base /target
File operations
文件操作
gcp source.txt dest.txt
gmv old.txt new.txt
grm -rf directory/
undefinedgcp source.txt dest.txt
gmv old.txt new.txt
grm -rf directory/
undefinedBad: Using Mac BSD Commands
错误用法:使用Mac BSD命令
bash
undefinedbash
undefinedDO NOT USE Mac standard commands
请勿使用Mac标准命令
ls -la
find . -name "*.ts"
sed -i '' 's/old/new/g' file.txt
grep -r "pattern" .
realpath file.txt
cp source.txt dest.txt
undefinedls -la
find . -name "*.ts"
sed -i '' 's/old/new/g' file.txt
grep -r "pattern" .
realpath file.txt
cp source.txt dest.txt
undefinedCommon Patterns
常见使用场景
File Operations
文件操作
bash
undefinedbash
undefinedCopy with GNU cp
使用GNU cp复制
gcp -r source/ dest/
gcp -r source/ dest/
Move with GNU mv
使用GNU mv移动
gmv file.txt newdir/
gmv file.txt newdir/
Remove with GNU rm
使用GNU rm删除
grm -rf directory/
undefinedgrm -rf directory/
undefinedText Processing
文本处理
bash
undefinedbash
undefinedSearch and replace with GNU sed
使用GNU sed搜索替换
gsed -i 's/pattern/replacement/g' file.txt
gsed -i 's/pattern/replacement/g' file.txt
Pattern matching with GNU grep
使用GNU grep匹配多模式
ggrep -E "pattern1|pattern2" file.txt
ggrep -E "pattern1|pattern2" file.txt
Process with GNU awk
使用GNU awk处理内容
gawk '{print $1, $3}' file.txt
undefinedgawk '{print $1, $3}' file.txt
undefinedFile Finding
文件查找
bash
undefinedbash
undefinedFind files with GNU find
使用GNU find查找并执行命令
gfind . -type f -name "*.ts" -exec ggrep -l "pattern" {} ;
gfind . -type f -name "*.ts" -exec ggrep -l "pattern" {} ;
Find and process
查找并删除过期文件
gfind . -name "*.log" -mtime +30 -delete
undefinedgfind . -name "*.log" -mtime +30 -delete
undefinedPath Operations
路径操作
bash
undefinedbash
undefinedGet absolute path
获取文件绝对路径
grealpath ./file.txt
grealpath ./file.txt
Get relative path
获取相对路径
grealpath --relative-to=/base /target
undefinedgrealpath --relative-to=/base /target
undefinedInstallation Note
安装说明
GNU coreutils can be installed via Homebrew:
bash
brew install coreutilsAfter installation, commands are available with prefix.
g可通过Homebrew安装GNU coreutils:
bash
brew install coreutils安装完成后,即可使用带前缀的命令。
gRationale
设计理由
- GNU coreutils provide consistent behavior across platforms
- Better compatibility with Linux systems
- More features and options than BSD versions
- Consistent behavior in scripts across different environments
- GNU coreutils在不同平台上提供一致的行为
- 与Linux系统兼容性更好
- 相比BSD版本具备更多功能与选项
- 确保脚本在不同环境下行为一致