use-gnu-coreutils

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Use GNU Coreutils with 'g' Prefix

使用带'g'前缀的GNU Coreutils

Rule (CRITICAL)

规则(严重级别)

ALWAYS use GNU coreutils commands with
g
prefix instead of Mac default BSD commands.
NEVER use Mac standard commands directly.
必须始终使用带
g
前缀的GNU coreutils命令,替代Mac默认的BSD命令。
绝对不要直接使用Mac标准命令。

Command Mapping

命令映射

GNU Command (Use)Mac BSD Command (Prohibited)Description
gls
ls
List directory contents
gfind
find
Find files
gsed
sed
Stream editor
gawk
awk
Pattern scanning and processing
ggrep
grep
Search patterns
gcat
cat
Concatenate files
gcp
cp
Copy files
gmv
mv
Move files
grm
rm
Remove files
gmkdir
mkdir
Create directories
gchmod
chmod
Change file permissions
gchown
chown
Change file ownership
gdate
date
Display/set date
gtouch
touch
Change file timestamps
ghead
head
Display first lines
gtail
tail
Display last lines
gsort
sort
Sort lines
guniq
uniq
Remove duplicate lines
gwc
wc
Word count
gcut
cut
Cut fields
gtr
tr
Translate characters
gxargs
xargs
Build and execute commands
grealpath
realpath
Resolve absolute paths
gstat
stat
Display file status
greadlink
readlink
Read symbolic links
gln
ln
Create links
gshuf
shuf
Shuffle lines
gsplit
split
Split files
gbase64
base64
Base64 encode/decode
gmd5sum
md5
MD5 checksum
gsha256sum
shasum
SHA256 checksum
推荐使用的GNU命令禁止使用的Mac BSD命令描述
gls
ls
列出目录内容
gfind
find
查找文件
gsed
sed
流编辑器
gawk
awk
模式扫描与处理
ggrep
grep
模式搜索
gcat
cat
合并文件内容
gcp
cp
复制文件
gmv
mv
移动文件
grm
rm
删除文件
gmkdir
mkdir
创建目录
gchmod
chmod
修改文件权限
gchown
chown
修改文件所有权
gdate
date
显示/设置日期
gtouch
touch
修改文件时间戳
ghead
head
显示文件前几行
gtail
tail
显示文件最后几行
gsort
sort
排序行内容
guniq
uniq
移除重复行内容
gwc
wc
字数统计
gcut
cut
提取字段内容
gtr
tr
字符转换
gxargs
xargs
构建并执行命令
grealpath
realpath
解析绝对路径
gstat
stat
显示文件状态
greadlink
readlink
读取符号链接
gln
ln
创建链接
gshuf
shuf
随机打乱行内容
gsplit
split
拆分文件
gbase64
base64
Base64编码/解码
gmd5sum
md5
MD5校验和
gsha256sum
shasum
SHA256校验和

Examples

示例

Good: Using GNU Commands

正确用法:使用GNU命令

bash
undefined
bash
undefined

List 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/
undefined
gcp source.txt dest.txt gmv old.txt new.txt grm -rf directory/
undefined

Bad: Using Mac BSD Commands

错误用法:使用Mac BSD命令

bash
undefined
bash
undefined

DO 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
undefined
ls -la find . -name "*.ts" sed -i '' 's/old/new/g' file.txt grep -r "pattern" . realpath file.txt cp source.txt dest.txt
undefined

Common Patterns

常见使用场景

File Operations

文件操作

bash
undefined
bash
undefined

Copy 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/
undefined
grm -rf directory/
undefined

Text Processing

文本处理

bash
undefined
bash
undefined

Search 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
undefined
gawk '{print $1, $3}' file.txt
undefined

File Finding

文件查找

bash
undefined
bash
undefined

Find 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
undefined
gfind . -name "*.log" -mtime +30 -delete
undefined

Path Operations

路径操作

bash
undefined
bash
undefined

Get absolute path

获取文件绝对路径

grealpath ./file.txt
grealpath ./file.txt

Get relative path

获取相对路径

grealpath --relative-to=/base /target
undefined
grealpath --relative-to=/base /target
undefined

Installation Note

安装说明

GNU coreutils can be installed via Homebrew:
bash
brew install coreutils
After installation, commands are available with
g
prefix.
可通过Homebrew安装GNU coreutils:
bash
brew install coreutils
安装完成后,即可使用带
g
前缀的命令。

Rationale

设计理由

  • 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版本具备更多功能与选项
  • 确保脚本在不同环境下行为一致