Quick take: The comm command compares two sorted files and outputs three columns: lines only in file 1, lines only in file 2, and lines in both. Suppress columns with -1, -2, -3 to perform set operations.

Introduction

The comm command compares two sorted files and separates their lines into three groups: unique to the first, unique to the second, and common to both. By hiding columns, it becomes a quick way to compute set differences and intersections between line-based lists.

Syntax

The basic syntax of the comm command is:

comm [OPTIONS] FILE1 FILE2

Common Options and Parameters

The most useful options and parameters for the comm command:

OptionDescription
-1Suppress column 1 (lines unique to file 1).
-2Suppress column 2 (lines unique to file 2).
-3Suppress column 3 (lines common to both).
-12Show only common lines (intersection).
-23Show only lines unique to file 1 (difference).

Practical Examples

Real comm commands you can run today:

# Three-column comparison (files must be sorted)
comm sorted1.txt sorted2.txt
# Lines common to both (intersection)
comm -12 a.txt b.txt
# Lines only in the first file
comm -23 a.txt b.txt
# Lines only in the second file
comm -13 a.txt b.txt
# Sort on the fly with process substitution
comm -12 <(sort a.txt) <(sort b.txt)

Tips and Best Practices

  • Both files must be sorted — sort them first, or use process substitution <(sort file) to sort on the fly.
  • Remember the column suppression: -12 shows the intersection, -23 shows what is only in file 1.
  • comm is perfect for answering “which entries are in list A but not list B?” without scripting.

Final Thoughts

comm turns two sorted lists into set operations — intersection, difference, and unique members — by selectively hiding its three columns. The one rule is that the input must be sorted, easily handled with process substitution. For comparing the content of files rather than set membership, use diff instead.

FAQ: comm Command in Linux

What does the comm command do?+

comm compares two sorted files and prints three columns: lines only in the first file, lines only in the second, and lines common to both. Hiding columns lets you compute set operations.

Why does comm require sorted files?+

comm works by stepping through both files in order, so it only gives correct results when the inputs are sorted. Sort them first or use process substitution like comm -12 <(sort a) <(sort b).

How do I find lines common to two files?+

Use comm -12 file1 file2, which suppresses the unique columns and shows only the lines present in both (the intersection).

How do I find lines in one file but not another?+

Use comm -23 file1 file2 to show lines only in the first file, or comm -13 to show lines only in the second.

What is the difference between comm and diff?+

comm performs set operations on sorted lines (intersection and difference), while diff shows the line-by-line edits needed to turn one file into another. Use comm for membership and diff for changes.

Need help with Linux servers or infrastructure?

Work directly with Muhammad Irfan Aslam for Linux, Ubuntu, Docker, DevOps, cloud, CI/CD, or infrastructure support.

Hire Me for Support