comm -23:对有序文件做集合运算
用 comm 的列抑制选项在 Linux 上计算两个已排序文件的差集与交集。
comm -23 A B 输出仅在 A 中的行,comm -13 输出仅在 B 中的行,comm -12 输出两者共有的行。
01命令
命令
$ comm -23 <(sort old.list) <(sort new.list)02作用说明
comm 输出三列——文件 1 独有、文件 2 独有、两者共有——而编号选项就是隐藏对应列:-12 得到交集,-23 得到“在 A 但不在 B”(真正的差集)。它要求两个输入都已排序,所以通常用进程替换 <(sort 文件) 包一层。这是回答“昨天的列表和今天相比少了哪些条目”最可靠的方法。
03风险
!注意: 两个文件必须用同样的排序规则排序——两边都加 LC_ALL=C sort,否则结果会悄悄出错。
04撤销
✓无需撤销——该命令不会改变持久状态。
05来源
- comm(1) — Arch Linux manual依据: -1/-2/-3 suppress column 1 (file1-only), 2 (file2-only), 3 (common); inputs must be sorted
- comm(1P) — POSIX manual page依据: POSIX defines the three-column output and the requirement that files be sorted with LC_ALL consistency
最后验证: 2026-09-17 · 分类: 文件与文件夹