comm

comm compares two sorted files and tells you which lines are unique to each and which are common to both.

comm [options] file1 file2

By default it prints three columns:

$ comm list-a.txt list-b.txt
            apple
banana
                        cherry
date
  • Column 1 — lines only in file1.
  • Column 2 — lines only in file2.
  • Column 3 — lines in both.

Each column is indented past the one before, so a line's column position tells you where it was found.

The input must be sorted #

comm steps through both files together, so both must be sorted. On unsorted input the comparison is meaningless. Sort the files first with sort.

comm checks the ordering and reports a file that is out of order. --check-order forces the check; --nocheck-order disables it.

Showing only the columns you want #

Each column can be switched off. The remaining columns close up to fill the gap.

OptionEffect
-1Suppress column 1 — hide lines unique to file1.
-2Suppress column 2 — hide lines unique to file2.
-3Suppress column 3 — hide lines common to both.

These combine to answer specific questions. comm -12 shows only the common lines — the intersection of the two files. comm -23 shows lines in file1 that are not in file2 — the difference.

Other options #

OptionEffect
--output-delimiter=STRSeparate the columns with STR instead of spaces.
--totalPrint a final summary line with the count in each column.
-z, --zero-terminatedTreat the NUL character as the line delimiter.

Exit status #

CodeMeaning
0Success.
1A file could not be read, or was not sorted.

Edit this page