Linux Command Reference ยท Viewing and Searching Files
Linux comm command
Reference for the Linux comm command: compare two sorted files and print lines unique to each or common to both, suppress columns and handle unsorted input.
comm compares two files that are already sorted and prints three columns: lines only in the first file, lines only in the second file, and lines in both. It is a set-comparison tool: think of the two files as lists of items and comm as the way to compute "only in A", "only in B" and "in both".
Because it reads both files once, in step, it is very fast and uses almost no memory, even for huge lists. The price is the requirement that both inputs are sorted in the same order.
comm [OPTION]... FILE1 FILE2
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Syntax
comm [-123] [--check-order | --nocheck-order] [--output-delimiter=STR] [-i] [-z] FILE1 FILE2
Either file name can be - for standard input (not both).
| Option | Description |
|---|---|
-1 |
Suppress column 1 (lines unique to FILE1). |
-2 |
Suppress column 2 (lines unique to FILE2). |
-3 |
Suppress column 3 (lines common to both). |
--check-order |
Fail if input is not sorted, even when every line has a match. |
--nocheck-order |
Never check the order. |
--output-delimiter=STR |
Use STR between columns instead of a tab. |
-i |
Ignore case when comparing (the files must be sorted case-insensitively). |
-z |
Lines end with NUL instead of newline. |
--total |
Print a summary line with the three counts. |
Output layout: column 1 has no indentation, column 2 is indented by one tab, column 3 by two tabs. Combine the -1 -2 -3 flags to keep the one set you want: -12 means "show only column 3".
Returns: 0 on success, 1 on error (including a detected sort-order problem).
Usage notes
- Both files must be sorted with the same rules
commuses. Sort them with the same locale for both:LC_ALL=C sort. A mismatch gives wrong results, with the warningfile 2 is not in sorted orderprinted on standard error. - Set operations:
comm -12is the intersection,comm -23is the difference A minus B, andcomm -3is the symmetric difference (with the tab for column 2). - Duplicate lines are matched pairwise: if
ahasxtwice andbhas it once, one copy is common and one copy is unique toa. Usesort -ufirst for true sets. - Whole lines only. Comparing by a single field needs
joinorawk. - Trailing space matters:
appleandappleare different lines. - Typical uses: which packages are installed on one machine and not on another, which users appear in one list only, which files were added or removed between two directory listings.
- Difference to
diff:diffcares about the order of lines and reports edits;commignores everything except membership.
Related
diff,cmp,join,paste- Lessons: diff, comm and patch, sort, uniq and cut