When you are managing a massive Linux database and you have two giant text files containing thousands of email addresses (e.g., old_subscribers.txt and new_subscribers.txt), you must figure out exactly which emails are new, which ones are old, and which ones exist in both files. Manually reading through thousands of lines of text is impossible. To mathematically cross-reference two sorted lists and instantly isolate the differences line-by-line, you must use the comm (Compare) command.
How the comm Command Works
Unlike the diff command, which highlights changes in code architecture, the comm command is specifically designed for dataset reconciliation. It requires two files as input, and both files must be perfectly alphabetically sorted before execution, or the engine will crash and output garbage data.
If your files are not sorted, you must sort them first:
sort file1.txt -o file1.txt
sort file2.txt -o file2.txt
Once sorted, simply execute the command against both files:
comm file1.txt file2.txt
The terminal will instantly output a highly structured, three-column visual table.
- Column 1 (Far Left): Lines that only exist in file1.txt.
- Column 2 (Middle): Lines that only exist in file2.txt.
- Column 3 (Far Right): Lines that exist in both files simultaneously.
Suppressing Specific Columns
If you are analyzing a database of 50,000 users, printing all three columns to the screen will generate a chaotic, unreadable mess of text. The true power of the comm command lies in its ability to selectively suppress columns using numerical flags (1, 2, or 3), allowing you to extract highly specific datasets.
Scenario A: Find completely new users.
You want to see the emails that are only in file2.txt (Column 2). You must suppress Column 1 and Column 3. You do this by appending the -13 flag.
comm -13 file1.txt file2.txt
The terminal will instantly output a clean, single-column list of brand-new users, completely ignoring the old data.
Scenario B: Find the overlapping users.
You want to see which emails exist in both files simultaneously (Column 3). You must suppress Column 1 and Column 2 using the -12 flag.
comm -12 file1.txt file2.txt
The engine will instantly strip away all unique data and output a perfect, isolated list of the overlapping users.