Linux Command: comm
Compare two sorted files line by line
The comm command compares two sorted files line by line and produces three columns of output: lines unique to file 1, lines unique to file 2, and lines common to both files. comm is the Unix equivalent of set operations: the three columns represent set difference (A-B), set difference (B-A), and set intersection (AโฉB). This makes it perfect for comparing lists like user accounts, IP addresses, installed packages, or any sorted data. Both input files must be sorted for comm to work correctly. If your files are not sorted, pipe them through sort first: comm <(sort file1) <(sort file2).
Syntax
comm [OPTION]... FILE1 FILE2Key Options
-1โ Suppress column 1 (lines unique to file 1)-2โ Suppress column 2 (lines unique to file 2)-3โ Suppress column 3 (lines common to both)-12โ Show only common lines (intersection)-23โ Show lines only in file 1 (difference)-13โ Show lines only in file 2 (difference)
Examples
Compare two sorted lists
comm list_old.txt list_new.txtFind common lines
comm -12 users_server1.txt users_server2.txtFind lines only in file 1
comm -23 old_packages.txt new_packages.txtPro Tips
- comm produces incorrect results on unsorted input. Always sort first: comm <(sort file1) <(sort file2).
- Think of comm flags as set operations: -12 = intersection, -23 = A minus B, -13 = B minus A, -3 = symmetric difference.
- comm shows set-based differences (what is in A, B, or both). diff shows how to transform one file into another. Use comm for list comparison.
Learn more: Full comm reference โ