How to Compare Files Line-by-Line Using the comm Command in Linux

When you dump two highly similar lists on a Linux server (e.g., a list of active users from yesterday versus a list of active users from today) and you need to mathematically determine exactly which users were deleted and which users were added, using standard diff can be overly chaotic. To force the Linux kernel to algorithmically parse two text streams and output a pristine, three-column matrix isolating the common and distinct lines, you must deploy the comm command.

Executing the Comparison Engine

The comm (Compare) command is a highly optimized, line-by-line parsing engine. It ingests two separate data streams and outputs exactly three mathematical columns:

  • Column 1: Lines that exist only in File A.
  • Column 2: Lines that exist only in File B.
  • Column 3: Lines that are mathematically identical in both files.

CRITICAL ARCHITECTURAL WARNING: The comm engine strictly requires that both input files be mathematically sorted in ascending alphabetical or numerical order before execution. If the files are chaotic and unsorted, the engine will violently fail and output corrupted comparison data.

Executing a Basic Comparison

Imagine you have two perfectly sorted files: list_A.txt and list_B.txt.

To execute the comparison protocol, open your terminal and type:

comm list_A.txt list_B.txt

The exact millisecond you press Enter, the comm engine rips through both files simultaneously and dumps the three-column matrix directly to standard output.

Suppressing Output Columns

The true power of the comm engine lies in its ability to mathematically suppress specific columns. If you only want to see the lines that are identical in both files (Column 3), you must instruct the engine to suppress Column 1 and Column 2 using the -1 and -2 flags.

comm -12 list_A.txt list_B.txt

The engine instantly recalculates, vaporizes the first two columns, and outputs a single, pristine column containing only the data shared perfectly between both files. Conversely, if you want to see lines that are unique to File B (Column 2), you would suppress Columns 1 and 3: comm -13 list_A.txt list_B.txt.

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.