๐ŸŽ New User? Get 20% off your first purchase with code NEWUSER20 ยท โšก Instant download ยท ๐Ÿ”’ Secure checkout Register Now โ†’
Menu

Categories

๐Ÿ’ก Text Processing September 21, 2026 5

Linux Command: comm

Compare two sorted files line by line

Terminal โ€” Text Processing
Command
$ comm list_old.txt list_new.txt

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 FILE2

Key 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.txt

Find common lines

comm -12 users_server1.txt users_server2.txt

Find lines only in file 1

comm -23 old_packages.txt new_packages.txt

Pro 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 โ†’

Share this tip