Skip to content
Tutorials Center
My learning

Topics

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.

Terminal
comm [OPTION]... FILE1 FILE2

Try it

Bash Three columns

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.

Bash Unsorted input and process substitution

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

Terminal
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 comm uses. Sort them with the same locale for both: LC_ALL=C sort. A mismatch gives wrong results, with the warning file 2 is not in sorted order printed on standard error.
  • Set operations: comm -12 is the intersection, comm -23 is the difference A minus B, and comm -3 is the symmetric difference (with the tab for column 2).
  • Duplicate lines are matched pairwise: if a has x twice and b has it once, one copy is common and one copy is unique to a. Use sort -u first for true sets.
  • Whole lines only. Comparing by a single field needs join or awk.
  • Trailing space matters: apple and apple are 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: diff cares about the order of lines and reports edits; comm ignores everything except membership.