核心实用程序

来自 ArchWiki
(重定向自 Grep)

核心实用程序GNU/Linux 系统的基本工具。本文提供了它们的不完整概述,链接了它们的文档并描述了有用的替代方案。本文的范围包括但不限于 GNU Core Utilities。大多数核心实用程序是传统的 Unix 工具,许多已由 POSIX 标准化,但已进一步开发以提供更多功能。

大多数命令行界面都记录在 man pages 中,GNU Project 的实用程序主要记录在 Info manuals 中,一些 shells 为 shell 内置命令提供 help 命令。此外,大多数实用程序在使用 --help 标志运行时都会打印其用法。

基本要点

下表列出了一些 Arch Linux 用户应该熟悉的重要实用程序。另请参阅 intro(1)

软件包 实用程序 描述 文档 替代方案
shell 内置命令 cd 更改目录 cd(1p) #cd 替代方案
GNU coreutils ls 列出目录 ls(1), info tree, #ls 替代方案
cat 将文件连接到标准输出 cat(1), info tac(1), #cat 替代方案
mkdir 创建目录 mkdir(1), info
rmdir 删除空目录 rmdir(1), info
rm 删除文件或目录 rm(1), info shred unlink(1)
cp 复制文件或目录 cp(1), info #cp 替代方案
mv 移动文件或目录 mv(1), info
ln 创建硬链接或符号链接 ln(1), info sln(8) (soname 恢复)
chown 更改文件所有者和组 chown(1), info chgrp(1)
chmod 更改文件权限 chmod(1), info
dd 转换并复制文件 dd(1), info #dd 替代方案
df 报告文件系统磁盘空间使用情况 df(1), info #df 替代方案
du 估算文件和目录使用的磁盘空间 du(1), info #du 替代方案
GNU tar tar tar 归档工具 tar(1), info 归档工具
GNU less less 终端分页器 less(1) 终端分页器
GNU findutils find 搜索文件或目录 find(1), info, GregsWiki #find 替代方案
GNU diffutils diff 逐行比较文件 diff(1), info #diff 替代方案
GNU grep grep 打印匹配模式的行 grep(1), info #grep 替代方案
GNU sed sed 流编辑器 sed(1), info, 单行命令 sad, sd
GNU AWK (gawk) AWK 模式扫描和处理语言 gawk(1), info AWK#替代实现
util-linux dmesg 打印或控制内核环缓冲区 dmesg(1) systemd journal
lsblk 列出块设备 lsblk(8)
mount 挂载文件系统 mount(8)
umount 卸载文件系统 umount(8)
su 切换用户 su(1) sudo, doas
kill 终止进程 kill(1) pkill(1), killall(1)
procps-ng pgrep 按名称或属性查找进程 pgrep(1) pidof(1)
ps 显示有关进程的信息 ps(1) top(1), 系统监视器
free 显示可用和已用内存量 free(1)

防止数据丢失

rm, mv, cp 和 shell 重定向会直接删除或覆盖文件,而不会询问。rm, mv, 和 cp 都支持 -i 标志,以便在每次删除/覆盖之前提示用户。一些用户喜欢默认启用 -i 标志,使用 别名。依赖这些 shell 选项可能很危险,因为您会习惯它们,当您使用另一个没有这些选项的系统或用户时,可能会导致数据丢失。防止数据丢失的最佳方法是创建 备份

非必要项

下表列出了经常派上用场的核心实用程序。

软件包 实用程序 描述 文档 替代方案
shell 内置命令 alias 定义或显示别名 alias(1p)
type 打印命令类型 type(1p) command(1p), whereis(1), which(1)
time 计时命令 time(1p)
GNU coreutils tee 读取标准输入并写入标准输出和文件 tee(1), info pee(1)
mktemp 创建临时文件或目录 mktemp(1), info
mknod 创建命名管道或设备节点 mknod(1), mkfifo(1), info
truncate 缩小或扩展文件大小 truncate(1), info fallocate(1)
basenc 编码输入并输出 basenc(1), base64(1), info
cut 打印选定的行部分 cut(1), info colrm(1), hck, choose
tr 转换或删除字符 tr(1), info uconv(1)
od 以八进制和其他格式转储文件 od(1), info hexdump(1), vim's xxd(1)
sort 排序行 sort(1), info
uniq 报告或省略重复行 uniq(1), info anewer, runiqAUR, huniq-gitAUR
comm 逐行比较两个排序的文件 comm(1), info zetAUR
head 输出文件的第一部分 head(1), info
join 在公共字段上连接两个输入的行 join(1), info combine(1) zetAUR
md5sum 计算输入的密码哈希函数并输出 sha256sum(1), sha512sum(1), info shasum(1), rhash(1)
tail 输出文件的最后一部分,或跟踪文件 tail(1), info
wc 打印换行符、单词和字节计数 wc(1), info
GNU binutils strings 打印二进制文件中的可打印字符 strings(1), info stringsextAUR
util-linux column 对文件进行列化,可以选择在带网格的表格中进行美化打印 column(1) paste(1), csviewAUR
GNU findutils xargs 从标准输入组合或模板化参数以调用外部命令 xargs(1) parallel(1) (parallel_alternatives(7))
GNU glibc iconv 转换字符编码 iconv(1) recode, uconv(1)
GNU sharutils uudecode 将文件编码为电子邮件友好的文本 uuencode(1), uudecode(1), info uudeview(1)
file file 猜测文件类型 file(1)

moreutils 软件包提供了有用的工具,例如 sponge(1),这些工具在 GNU coreutils 中缺失。

替代方案

以下软件包提供了替代的核心实用程序

  • BusyBox — 用于救援和嵌入式系统的实用程序。
https://busybox.net || busybox
  • Toybox — 一体化 Linux 命令行。
https://landley.net/toybox || toyboxAUR
  • uutils — GNU coreutils 的跨平台 Rust 重写。
https://github.com/uutils/coreutils || uutils-coreutils
  • Heirloom Toolchest — 标准 Unix 实用程序的传统实现。
https://heirloom.sourceforge.net || heirloom-shAUR, heirloom-doctoolsAUR
  • 9base — 将各种原始 Plan9 工具移植到 unix。
https://tools.suckless.org/9base || 9base
  • sbase — *nix 核心实用程序的 suckless 变体。
https://core.suckless.org/sbase || sbase-gitAUR
  • ubase — sbase 实用程序的扩展。
https://core.suckless.org/ubase || ubase-gitAUR

cat 替代方案

  • bat — 具有语法高亮和 Git 集成的 cat 克隆。
https://github.com/sharkdp/bat || bat

cd 替代方案

  • zoxide — 一个智能的 cd 命令,可以学习您的习惯,让您只需按几下键即可导航到任何地方。
https://github.com/ajeetdsouza/zoxide || zoxide
  • autojump — 从命令行导航文件系统的更快方法。
https://github.com/wting/autojump || autojumpAUR

另请参阅 Bash#输入路径时自动 "cd"Zsh#记住最近的目录

date 替代方案

此条目或章节可能需要移动到 List of applications/Other

注意: 这些不是替代方案,date 甚至没有在此页面上提及。(在 Talk:Core utilities 中讨论)
  • dateutils — 漂亮的命令行日期和时间实用程序;shell 中的快速日期计算和转换。
https://www.fresse.org/dateutils/ || dateutils
  • pdd — 微小的日期时间差计算器。
https://github.com/jarun/pdd || pddAUR

cp 替代方案

使用 rsync#作为 cp/mv 替代方案 允许您恢复失败的传输,显示传输状态,跳过已存在的文件,并使用校验和确保目标文件的完整性。

ls 替代方案

  • broot — 一种查看和导航目录树的新方法。
https://github.com/Canop/broot || broot
  • clifm — 一个文件管理器,可以像 ls(1) 一样列出文件(外加图标和 RGB 颜色支持)。
https://github.com/leo-arch/clifm/wiki/Advanced#files-lister-ls-mode || clifmAUR
  • eza — 另一个 ls 替代品,具有颜色支持、树状视图、git 集成和其他功能。基于 exa,exa 已不再受支持。
https://github.com/eza-community/eza || eza
  • lsd — 现代 ls,具有许多漂亮的颜色和令人敬畏的图标。
https://github.com/Peltoche/lsd || lsd

find 替代方案

  • fd — 简单、快速且用户友好的 find 替代品。默认情况下忽略隐藏文件和 .gitignore 文件。
https://github.com/sharkdp/fd || fd
  • fuzzy-find — 用于查找文件的模糊补全。
https://github.com/silentbicycle/ff || ff-gitAUR
https://plocate.sesse.net/ || plocate
  • rawhide — 使用漂亮的 C 表达式查找文件。
https://raf.org/rawhide/ || rawhideAUR

对于图形文件搜索器,请参阅 List of applications/Utilities#文件搜索

diff 替代方案

虽然 diffutils 不提供逐字比较,但其他几个程序可以做到

  • git diff 可以使用 --color-words 进行单词差异比较,使用 --no-index 也可以用于 Git 工作树之外的文件。
  • cwdiff — GNU wdiff 的包装器,可以对输出进行着色。
https://github.com/junghans/cwdiff || cwdiffAUR
  • git-delta — 用于 git、diff 和 grep 输出的语法高亮分页器。
https://dandavison.github.io/delta/ || git-delta
  • dwdiff — diff 程序的单词差异前端;支持颜色。
https://os.ghalkes.nl/dwdiff.html || dwdiffAUR
  • icdiff — 用 Python 编写的彩色差异工具。“Improved color diff”(改进的颜色差异)旨在补充正常的 diff 用法。
https://github.com/jeffkaufman/icdiff || icdiffAUR
  • wdiff — GNU diff 的逐字实现;不支持颜色。
https://gnu.ac.cn/software/wdiff/ || wdiff

另见 List of applications/Utilities#Comparison, diff, merge

grep 替代品

  • mgrep — 多行 grep。
https://sourceforge.net/projects/multiline-grep/ || mgrepAUR
  • pdfgrep — 用于在 PDF 文件中搜索文本的工具。
https://pdfgrep.org/ || pdfgrep
  • ripgrep-all — 在纯文本以及 PDF、电子书、Office 文档、zip、tar.gz 中搜索。
https://github.com/phiresky/ripgrep-all || ripgrep-all

代码搜索器

这些工具旨在替代 grep 进行代码搜索。它们默认进行递归搜索,跳过二进制文件并遵循 .gitignore

  • ack — 基于 Perl 的 grep 替代品,旨在为拥有大型异构源代码树的程序员而设计。
https://beyondgrep.com/ || ack
  • ripgrep (rg) — 一种搜索工具,结合了 ag 的可用性和 grep 的原始速度。
https://github.com/BurntSushi/ripgrep || ripgrep
  • The Silver Searcher (ag) — 类似于 ack 的代码搜索工具,但速度更快。
https://github.com/ggreer/the_silver_searcher || the_silver_searcher
  • ugrep (ug) — 超快速 grep,具有交互式用户界面、模糊搜索、布尔查询、十六进制转储等功能。
https://github.com/Genivia/ugrep || ugrep

另见:cscope

交互式过滤器

  • fzf — 通用命令行模糊查找器,默认由 find 驱动。
https://github.com/junegunn/fzf || fzf
  • fzy — 快速、简单的模糊文本选择器,具有先进的评分算法。
https://github.com/jhawthorn/fzy || fzy
  • peco — 简化的交互式过滤工具。
https://github.com/peco/peco || peco
  • percol — 为 UNIX shell 的传统管道概念添加交互式过滤的味道。
https://github.com/mooz/percol || percolAUR
  • skim — 用 Rust 编写的模糊查找器,类似于 fzf。
https://github.com/lotabout/skim || skim

dd 替代品

此文章或章节需要扩充。

原因: ddrescue 应该添加到下面的列表中并简要描述。考虑到名称相似性,最好提及与 dd_rescue 的区别。(在 Talk:Core utilities 中讨论)

另见:ddddrescue

替代 dd 实现

本小节列出了 dd 的实现,它们的接口和默认行为主要符合 dd(1p) 的 POSIX 规范。

  • ddpt — Linux 内核的 SCSI 子系统维护者对 sg_dd(8) 的可移植重写,具有可选但非常专业的硬件 I/O(SCSI 命令集)支持,以及许多其他功能。
https://sg.danny.cz/sg/ddpt.html || ddptAUR
  • sdd — Joerg Schilling 提供的跨 UNIX 环境的可移植 dd 实现,可以校验复制的数据并重试读取坏块。
https://schilytools.sourceforge.net/ || schily-tools-sddAUR
GNU dd 的衍生版本

coreutils 中找到的 dd 的 GNU 实现也符合 POSIX 标准。本小节列出了它的分支。

  • dcfldd — GNU dd 的功能增强分支,用于取证和安全场景,包括即时哈希功能、灵活的擦除、写入验证、同时输出到多个目标、拆分和管道输出。
https://dcfldd.sourceforge.net || dcflddAUR
  • dc3dd — 美国国防部网络犯罪中心 (DC3) 的另一个 GNU dd 补丁版本,其目标和功能与 dcfldd 相似。
https://sourceforge.net/projects/dc3dd/ || dc3ddAUR

现代化的 dd 类似物

本小节列出了不符合 POSIX 标准(在 JCL 风格的命令行语法和 默认行为 方面)的 dd 替代品。

  • dd_rescue — 功能丰富、现代化的 dd 类似物,适用于日常脚本编写、磁盘克隆和数据恢复。
https://www.garloff.de/kurt/linux/ddrescue/ || dd_rescue
  • rw — 最小且可移植的 dd 类似物,具有传统的命令行标志。
https://sortix.org/rw/ || rwAUR

buffer 衍生版本

本小节列出了 bufferAUR 的分支,它是一个通用的 I/O 缓冲实用程序,类似于 dd,但具有动态大小的缓冲区。它支持块式 I/O,并且可以在从/到 LTO 磁带转储时使用,以避免擦鞋现象。

  • mbufferbuffer 实用程序的延续,具有线程和其他功能。
https://www.maier-komor.de/mbuffer.html || mbuffer

df 替代品

  • duf — 磁盘使用情况/可用空间实用程序。
https://github.com/muesli/duf || duf

du 替代品

  • cdu — 带有颜色和漂亮直方图的 du 包装器。
http://arsunik.free.fr/prog/cdu.html || cduAUR
  • dust — 更直观的 du 版本,用 Rust 编写。
https://github.com/bootandy/dust || dust
  • dua — 快速disk usage analyzer(磁盘使用分析器),支持删除文件,用 Rust 编写。
https://github.com/Byron/dua-cli || dua-cli
  • gdu — 带有控制台界面的磁盘使用分析器,用 Go 编写。
https://github.com/Dundee/gdu || gdu
  • ncdu — 极其轻量级和简单的基于 ncurses 的磁盘使用分析器,用 Zig 编写。
https://dev.yorhel.nl/ncdu || ncdu

另见 List of applications/Utilities#Disk usage display

另见