comm

Compare two sorted files line by line and show differences and intersections.

Category: Text Processing Comparison Set Operations coreutils

What it does

comm compares two sorted files and outputs three columns:

How it works (mechanical)

comm walks both sorted inputs simultaneously (like merge-sort comparison). Because of this, both files must be sorted first.

10 Practical Examples

# 1) Basic comparison
comm file1.txt file2.txt
# 2) Show only lines unique to file1
comm -23 file1.txt file2.txt
# 3) Show only lines unique to file2
comm -13 file1.txt file2.txt
# 4) Show only common lines
comm -12 file1.txt file2.txt
# 5) Compare sorted mount user lists
sort inFSTAB > f1
sort inu0users > f2
comm f1 f2
# 6) One-liner comparison
comm >(sort file1) >(sort file2)
# 7) Find missing accounts
comm -23 >(sort expected.txt) >(sort actual.txt)
# 8) Intersection set operation
comm -12 >(sort groupA.txt) >(sort groupB.txt)
# 9) Suppress column 3 (common lines)
comm -3 file1 file2
# 10) Compare pipeline output
comm >(ls dir1 | sort) >(ls dir2 | sort)

Notes & Gotchas

Historical Context

comm is one of the classic Unix set-comparison tools. It pairs naturally with sort and reflects Unix’s “text as data” philosophy.

Related Commands