Skip to main content

comm command

comm - compare two sorted files line by line

The comm command in Linux is a utility used to compare two sorted files and display their common and unique lines in three columns. It’s useful for finding overlaps or differences between lists, such as text files or data sets.

Note: comm requires input files to be sorted (e.g., using the sort command) for accurate results.

Usage: comm [OPTION]... FILE1 FILE2

  • OPTION: Flags which enhances the diff abilities.
  • FILE1: The sorted file to compare.
  • FILE2: The sorted file to compare.

When FILE1 or FILE2 (not both) is -, read standard input.

Examples

  • Basic Usage

    Run comm with two sorted files to see three columns of output:

    • Column 1: Lines unique to file1.
    • Column 2: Lines unique to file2.
    • Column 3: Lines common to both.
    # Create and sort files
    $ echo -e "apple\nbanana\ncherry" > file1.txt
    $ echo -e "banana\ncherry\ndate" > file2.txt
    $ sort file1.txt -o file1.txt
    $ sort file2.txt -o file2.txt

    $ comm file1.txt file2.txt
    • Output:
      apple
      banana
      cherry
      date
    • Explanation:
      • apple: Only in file1 (Column 1).
      • banana: In both (Column 3, indented twice).
      • cherry: In both (Column 3).
      • date: Only in file2 (Column 2, indented once).
  • Suppressing Columns

    Use -1, -2, or -3 to hide specific columns:

    • -1: Suppress lines unique to file1.
    • -2: Suppress lines unique to file2.
    • -3: Suppress lines common to both.

    Example (Common Lines Only):

    $ comm -1 -2 file1.txt file2.txt
    • Output:
      banana
      cherry
    • Shows only the lines present in both files.

    Example (Unique to file1 Only):

    $ comm -2 -3 file1.txt file2.txt
    • Output:
      apple
    • Shows only lines unique to file1.
  • Ignoring Case

    Use -i to perform a case-insensitive comparison.

    $ echo -e "Apple\nBanana" > file1.txt
    $ echo -e "apple\ncherry" > file2.txt
    $ sort file1.txt -o file1.txt
    $ sort file2.txt -o file2.txt

    $ comm -i file1.txt file2.txt
    • Output:
              apple
      Banana
      cherry
    • Apple and apple are treated as the same with -i.
  • Handling Unsorted Files

    If files aren’t sorted, results will be incorrect. Use sort first or pipe it.

    Example with Pipe:

    $ sort file1.txt | comm - file2.txt
    • Use - to read from standard input (sorted file1.txt) and compare with sorted file2.txt.
  • Custom Output Delimiter

    Use --output-delimiter=STR to change the tab separator between columns.

    $ comm --output-delimiter=" | " file1.txt file2.txt
    • Output:
      apple |       |
      | | banana
      | | cherry
      | date |
    • Uses | instead of tabs for clarity.
$ comm --help
Usage: comm [OPTION]... FILE1 FILE2
Compare sorted files FILE1 and FILE2 line by line.

When FILE1 or FILE2 (not both) is -, read standard input.

With no options, produce three-column output. Column one contains
lines unique to FILE1, column two contains lines unique to FILE2,
and column three contains lines common to both files.

-1 suppress column 1 (lines unique to FILE1)
-2 suppress column 2 (lines unique to FILE2)
-3 suppress column 3 (lines that appear in both files)

--check-order check that the input is correctly sorted, even
if all input lines are pairable
--nocheck-order do not check that the input is correctly sorted
--output-delimiter=STR separate columns with STR
--total output a summary
-z, --zero-terminated line delimiter is NUL, not newline
--help display this help and exit
--version output version information and exit

Note, comparisons honor the rules specified by 'LC_COLLATE'.

Examples:
comm -12 file1 file2 Print only lines present in both file1 and file2.
comm -3 file1 file2 Print lines in file1 not in file2, and vice versa.