comm Command
Intermediate Text Processing man(1)Compare two sorted files line by line
👁 10 views
📅 Updated: Mar 15, 2026
SYNTAX
comm [OPTION]... FILE1 FILE2
What Does comm Do?
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).
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).
Options & Flags
| Option | Description | Example |
|---|---|---|
| -1 | Suppress column 1 (lines unique to file 1) | comm -1 file1.txt file2.txt |
| -2 | Suppress column 2 (lines unique to file 2) | comm -2 file1.txt file2.txt |
| -3 | Suppress column 3 (lines common to both) | comm -3 file1.txt file2.txt |
| -12 | Show only common lines (intersection) | comm -12 sorted1.txt sorted2.txt |
| -23 | Show lines only in file 1 (difference) | comm -23 sorted1.txt sorted2.txt |
| -13 | Show lines only in file 2 (difference) | comm -13 sorted1.txt sorted2.txt |
Practical Examples
#1 Compare two sorted lists
Shows three columns: old-only, new-only, and common items.
$ comm list_old.txt list_new.txt#2 Find common lines
Shows only lines present in both files (set intersection).
$ comm -12 users_server1.txt users_server2.txt#3 Find lines only in file 1
Shows packages that were in old but removed in new (set difference).
$ comm -23 old_packages.txt new_packages.txt#4 Compare unsorted files
Uses process substitution to sort files before comparing.
$ comm -12 <(sort ips_today.txt) <(sort ips_yesterday.txt)#5 Find new entries
Shows lines in current.txt that are not in baseline.txt — useful for finding new items.
$ comm -13 <(sort baseline.txt) <(sort current.txt)#6 Count common items
Counts how many items appear in both lists.
$ comm -12 list1.txt list2.txt | wc -l
Output:
42
Tips & Best Practices
Files must be sorted: comm produces incorrect results on unsorted input. Always sort first: comm <(sort file1) <(sort file2).
Set operations: Think of comm flags as set operations: -12 = intersection, -23 = A minus B, -13 = B minus A, -3 = symmetric difference.
comm vs diff: 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.
Frequently Asked Questions
How do I find lines common to two files?
Sort both files, then use comm -12 file1.txt file2.txt. The -12 flags suppress unique-to-file columns, showing only common lines.
Why does comm show wrong results?
comm requires sorted input. Sort your files first: comm <(sort file1) <(sort file2). Also check locale: use LC_ALL=C for consistent sorting.
How do I find lines in file1 but not in file2?
Use comm -23 <(sort file1) <(sort file2). The -23 flags suppress file2-only and common lines, showing only file1-unique lines.
Related Commands
More Text Processing Commands
Master Linux with Professional eBooks
Curated IT eBooks covering Linux, DevOps, Cloud, and more
Browse Books →