Learn › Linux Foundations › Text Processing
comm - a hands-on Linux lab on a real virtual machine.
Compare two sorted lists with comm: three columns for only-in-first, only-in-second, and in-both. Suppress columns by number with -1, -2, -3; use -12 for the overlap and -23 for what only the first file has. Meet the sorted-input rule that keeps the answer honest, all in the practice terminal.
You have two files. One is yesterday's list of users, the other is today's. Some names are on both. Some dropped off. Some are brand new. Reading them side by side and squinting works for three names. It falls apart at three hundred.
There is a command built for exactly this: line up two lists and sort every line into one of three buckets. Only in the first file. Only in the second file. In both. Its name is comm, short for common, and answering that question is the whole command.
The black boxes in this lesson are a practice terminal: a safe sandbox that only checks the one command each step teaches, so you cannot break anything. At the end of the module one real Linux machine boots just for you, and you run the full mission there.
You are in a folder with two tiny files. a.txt holds three fruit, one per line, in order: apple, banana, cherry. b.txt also holds three, in order: banana, cherry, date. Point comm at both, first file first:
comm a.txt b.txt
prompt: student@linuxcamp:~$ answer: comm a.txt b.txt output: apple banana cherry date hint: Type comm, a space, then the two filenames in order: comm a.txt b.txt
Four lines, and their indentation is not decoration. apple sits hard against the left margin. date is pushed in a little. banana and cherry are pushed in further still. Those three indent levels are three columns, and the next step names them. Once you can read the columns, you can read any comparison comm will ever print.
comm prints exactly three columns, and it tells them apart with tabs, not headers. Read the output you just saw by indentation:
So the machine told you the whole story in four lines. apple is only in a.txt. date is only in b.txt. banana and cherry are in both. The further right a line sits, the more files it belongs to: left is one file, middle is the other file, right is shared.
Column 1 is the first file, column 2 is the second file, column 3 is both. The order of your filenames on the command line decides which file is column 1 and which is column 2. Swap them and columns 1 and 2 swap with them.
The full three-column view is useful, but often you want just one bucket. comm lets you switch columns OFF by number. -1 hides column 1, -2 hides column 2, -3 hides column 3. You can combine them: give the numbers of every column you want GONE.
To keep only the shared lines, you want column 3 and nothing else, so you hide columns 1 and 2. That is -12:
comm -12 a.txt b.txt
prompt: student@linuxcamp:~$ answer: comm -12 a.txt b.txt output: banana cherry hint: Hide columns 1 and 2 to leave only the shared column: comm -12 a.txt b.txt
Two lines, banana and cherry, and notice they are no longer indented. Once a column is the only one left, comm stops padding it, so the shared lines print flush left. -12 is the single most common way people use comm: it answers "what is in both lists?" in one line. Think of it as the overlap between the two files.
Same trick, different bucket. This time you want the lines that are ONLY in the first file, a.txt, which live in column 1. So you hide the other two columns, 2 and 3. That is -23:
comm -23 a.txt b.txt
prompt: student@linuxcamp:~$ answer: comm -23 a.txt b.txt output: apple hint: Hide columns 2 and 3 to leave only the first-file column: comm -23 a.txt b.txt
One line, apple, flush left because it is the only column left. apple was in a.txt and nowhere in b.txt, so it is what a.txt has that b.txt lost or never had. -23 answers "what is in the first file but not the second?" That is the everyday way to find dropped entries: yesterday's list minus today's. Flip the filenames and the same -23 would show what the other file has that this one does not.
Here is the rule that catches everyone once. comm does not read the whole file and think about it. It walks both files top to bottom at the same time, one line each, assuming the lines are already in sorted order. Sorted means alphabetical, the order sort would put them in. If the lines are out of order, comm marches straight past matches it should have caught, and the answer is silently wrong.
It does warn you. When comm notices a file is out of order, it prints a warning line to the screen. Here is what that line looks like.
comm: file 1 is not in sorted order
That message means the FIRST file you named was not sorted. file 2 in the message would mean the second one. The moment you see it, stop trusting the columns, because the comparison is unreliable. The fix is to sort each file first (the sort command has its own lesson), then run comm on the sorted versions. In this sandbox both files are already sorted, which is why every run above was clean.
A wrong comm answer usually is not a comm bug. It is unsorted input. If the numbers look impossible, check that BOTH files are sorted before you doubt anything else. comm trusts you to hand it sorted lists, and it does exactly what you told it.
Scaffolding off. No command is shown this time. You have every piece you need.
You just used -23 to find what is only in the FIRST file (apple). Now flip the question: you want the lines that are only in the SECOND file, b.txt, and nothing else. That is column 2 on its own. Work out which two columns to hide, then run it on a.txt and b.txt in that same order.
prompt: student@linuxcamp:~$ answer: comm -13 a.txt b.txt output: date hint: You want only column 2 (the second file). Hide columns 1 and 3: comm -13 a.txt b.txt
One line, date. Column 2 is the second file, so to keep only it you hid columns 1 and 3 with -13. date is the fruit that b.txt has and a.txt does not, the mirror image of the apple you found with -23. You picked the columns to suppress from memory and typed the flag yourself, which is exactly the recall the real machine will ask of you.
You earned this cheat sheet. Every row is a form of comm you already ran, on the same two files (a.txt holds apple, banana, cherry; b.txt holds banana, cherry, date).
The two rules to carry away: column 1 is the first file, column 2 is the second file, column 3 is both, and you SUPPRESS a column by its number. And the one that saves you: both files must be sorted first, or comm warns and the answer is wrong.
comm -12 (what they share), comm -23 (what only the first has), and comm -13 (what only the second has) are the three you will actually reach for. Between them they answer every "what is common, what is missing, what is new" question about two lists.
The practice terminal has shown you every form of comm. The three-column default, the -1 -2 -3 suppress flags, the two everyday combinations -12 and -23, and the sorted-input rule that keeps the answer honest. Every one of those you typed yourself.
The Text Processing module ends with one real Linux machine, the text-processing capstone mission, and that is where you compare lists for real. A VM boots just for you with its own files. Its objectives use exactly what you practiced across this module: sorting, cutting, counting, and now comparing two lists with comm, each of which has its own lesson. One difference from here: the mission shows no commands. You read the objective, recall the flag, and type it. That recall is what makes it stick.
Finish the other Text Processing lessons, then go line up two real lists for yourself.
Practice comm in a real Linux terminal at The Linux Camp. Progress is verified automatically as you type commands on the machine.