comm: Compare Two Sorted Files Line by Line
comm reads two sorted files and prints three columns: lines only in the first, lines only in the second, and lines in both.
comm is the set-comparison tool. Given two sorted lists it answers "what is only here" and "what is common", which is perfect for comparing lists of files, packages, or IDs in CI.
What it does
comm expects both inputs to be sorted. It emits three tab-separated columns: column 1 = lines unique to file1, column 2 = lines unique to file2, column 3 = lines in both. The -1/-2/-3 flags suppress columns, letting you compute set difference or intersection.
Common usage
# lines only in a.txt (set difference a - b)
comm -23 <(sort a.txt) <(sort b.txt)
# lines in both (intersection)
comm -12 <(sort a.txt) <(sort b.txt)
# lines unique to either side
comm -3 <(sort a.txt) <(sort b.txt)Options
| Flag | What it does |
|---|---|
| -1 | Suppress column 1 (lines only in file1) |
| -2 | Suppress column 2 (lines only in file2) |
| -3 | Suppress column 3 (lines in both) |
| --check-order | Fail if the input is not sorted |
| --nocheck-order | Do not verify sort order |
In CI
Use comm to diff two lists: expected vs actual dependencies, allowed vs installed packages, or committed vs generated file listings. comm -13 expected actual shows unexpected extras; pipe to grep . and test for output to fail the job when the sets diverge.
Common errors in CI
"comm: file 1 is not in sorted order" (or file 2) means you forgot to sort an input; comm requires sorted data and will otherwise report wrong results. Locale matters: sort and comm must use the same collation, so set LC_ALL=C on both to avoid mismatches. Tab vs space confusion in the three-column output is why -1/-2/-3 are used to isolate the column you want.