跳转至内容

核心工具

来自 ArchWiki
(重定向自 Diff)

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

大多数命令行界面都记录在 man pages 中,GNU 项目的工具主要记录在 Info manuals 中,一些 shell 为 shell 内建命令提供了 help 命令。此外,大多数工具在运行 `--help` 标志时会打印其用法。

必需工具

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

软件包 (Package) 工具 描述 文档 替代方案
shell 内建命令 cd 更改目录 cd(1p) #cd 替代品
GNU coreutils ls 列出目录 ls(1), info tree, #ls 替代品
cat 将文件连接到 stdout 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 选项可能很危险,因为你会习惯它们,导致在你使用没有这些选项的另一个系统或用户时发生潜在的数据丢失。防止数据丢失的最佳方法是创建 备份

非必需工具

此表列出了经常有用的核心工具。

软件包 (Package) 工具 描述 文档 替代方案
shell 内建命令 alias 定义或显示别名 alias(1p)
type 打印命令的类型 type(1p) command(1p), whereis(1), which(1)
time 计算命令的执行时间 time(1p)
GNU coreutils tee 从 stdin 读取并写入 stdout 和文件 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), vimxxd(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 从 stdin 组合或模板参数以调用外部命令 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 包提供了 GNU coreutils 中缺失的一些有用工具,例如 sponge(1)

替代方案

替代的核心工具由以下包提供

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

cat 替代品

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

cd 替代品

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

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

date 替代品

本文档或部分内容可能需要移动到 List of applications/Utilities

注意:这些并非替代品,页面上甚至没有提到 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。
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
  • plocate — 一个快得多的 locate。
https://plocate.sesse.net/ || plocate
  • rawhide — 使用漂亮的 C 表达式查找文件。
https://raf.org/rawhide/ || rawhideAUR
  • uutils-findutils — findutils 的 Rust 重写版
https://github.com/uutils/findutils || uutils-findutilsAUR

有关图形化文件搜索器,请参阅 List of applications/Utilities#File searching

diff 替代项

  • uutils-diffutils — diffutils 的 Rust 重写版。
https://github.com/uutils/diffutils || uutils-diffutilsAUR

虽然 diffutils 不提供按单词的 diff,但其他一些程序可以。

  • cwdiff — 一个 GNU wdiff 的包装器,可将输出着色。
https://github.com/junghans/cwdiff || cwdiffAUR
  • dwdiff — diff 程序的按单词 diff 前端;支持颜色。
https://os.ghalkes.nl/dwdiff.html || dwdiffAUR
  • git diff 可以使用 ` --color-words ` 进行按单词 diff,使用 ` --no-index ` 还可以用于 Git 工作树之外的文件。
  • git-delta — 一个用于 git、diff 和 grep 输出的语法高亮分页器。
https://dandavison.github.io/delta/ || git-delta
  • icdiff — 一个用 Python 编写的彩色 diff 工具。“改进的彩色 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/ || ackAUR
  • pcre2grep — 兼容 Perl 的 grep,但它使用 PCRE2 正则表达式库。
https://github.com/PCRE2Project/pcre2 || pcre2
  • 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

交互式过滤器

  • fnf — 一个终端的交互式模糊查找器。
https://github.com/leo-arch/fnf || fnfAUR
  • 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 实现

本小节列出了其接口和默认行为在很大程度上符合 POSIX dd(1p) 规范的 dd 实现。

  • 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 的分支

GNU 实现的 dd,在 coreutils 中可找到,也符合 POSIX。本小节列出了其分支。

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

现代化的 dd 类似物

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

  • 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
  • dua — 快速的 **d**isk **u**sage **a**nalyzer(磁盘使用分析器),支持删除文件,用 Rust 编写。
https://github.com/Byron/dua-cli || dua-cli
  • dust — du 的一个更直观的版本,用 Rust 编写。
https://github.com/bootandy/dust || dust
  • gdu — 带有控制台界面的磁盘使用分析器,用 Go 编写。
https://github.com/Dundee/gdu || gdu
  • ncdu — 一个极其轻量级且简单的基于 ncurses 的磁盘使用分析器,用 Zig 编写。
https://dev.yorhel.nl/ncdu || ncdu

另见 List of applications/Utilities#Disk usage display

POSIX shell 工具

许多常用软件包已经将最流行的POSIX 工具作为依赖项安装,但可以安装 posix 元软件包以确保它们始终存在。

除了强制性工具外,还有一些可选类别的元软件包

注意 并非给定类别中的所有可选工具都一定存在于相应的元软件包中。

技巧与提示

覆盖或添加缺失的 coreutils

某些命令(例如 `arch`, `kill` 等)在 coreutils 中缺失或来自其他软件包。为了兼容性而补全它们,请安装 uutils-coreutils 并执行以下操作

# ln -sf /usr/bin/uu-coreutils /usr/local/bin/arch
# echo -e "#compdef arch=uu-arch\n_uu-arch" > /usr/local/share/zsh/site-functions/_arch
# echo "complete -c arch -w uu-arch" > /usr/local/share/fish/vendor_completions.d/arch.fish

参见