The Linux comm command tells you which lines two sorted files share and which appear in just one. Imagine comparing Friday’s package inventory with Monday’s. Both contain three distinct names. Yet jq arrived, tar disappeared, and one copy of curl went missing. Here is how to uncover all three changes.
In this guide

How the Linux comm command reads its two files
Put the older inventory on the left and the newer inventory on the right. By default, comm prints three columns: lines only on the left, lines only on the right, and lines present in both. Each digit in an option hides that column. For example, -13 hides columns one and three, leaving the additions. Reversing the files reverses the meaning of an addition.
Before using the table: sort both files with the same locale you use for the comparison. The exercise below uses LC_ALL=C throughout. The .unique files hold one line per distinct name.
| Question | Command for sorted lists |
|---|---|
| Which names appeared? | comm -13 old.unique new.unique |
| Which names disappeared? | comm -23 old.unique new.unique |
| Which names stayed? | comm -12 old.unique new.unique |
Try it with two small inventories
The following exercise uses GNU Coreutils on Linux. Run the block in a new, empty directory so the example filenames are free. The parentheses keep the locale change inside this exercise. Sorting both exports under the same locale gives comm the ordering it expects. Here we use sort -u because each distinct package name should count once.
(
export LC_ALL=C
printf '%s\n' tar curl git curl > friday.txt
printf '%s\n' git jq curl > monday.txt
sort -u friday.txt > old.unique
sort -u monday.txt > new.unique
printf 'Added\n'
comm --check-order -13 old.unique new.unique
printf 'Removed\n'
comm --check-order -23 old.unique new.unique
printf 'Shared\n'
comm --check-order -12 old.unique new.unique
)
You should see this output:
Added
jq
Removed
tar
Shared
curl
git
There is a quick way to check your interpretation. Friday has three distinct names: one removed plus two shared. Monday also has three: one added plus two shared. Both totals match even though the membership changed. Comparing line counts alone would miss the swap.
Read the three columns before hiding any
From the same directory, run LC_ALL=C comm old.unique new.unique. The tabs place each record in its column. Read from the left edge: no leading tab means Friday only, one means Monday only, and two mean both days.
curl
git
jq
tar
Follow jq across the line. It starts after one tab, so it belongs to column two. To print only that column, hide columns one and three with -13. With only one column left, the leading tab disappears.
When the extra copy matters
Suppose each row records a separate installation. Friday then has two installations labeled curl and Monday has one. Deduplicating the input would erase a change you care about. Repeat the comparison with plain sort:
(
export LC_ALL=C
sort friday.txt > old.all
sort monday.txt > new.all
comm --check-order -23 old.all new.all
)
Compare both results again whenever you like. LC_ALL=C comm -23 old.unique new.unique prints only tar. With the duplicate-preserving files above, the output contains curl and tar. One Friday copy of curl paired with Monday’s copy. The second Friday copy had no partner. You can infer that the count fell by one. To identify which machine lost the package, you would need a machine identifier in each record.

What to check if the answer looks wrong
A file is reported as unsorted.
Sort both inputs again using the same locale as the comparison. A list sorted with sort -n may put 2 before 10, while byte order puts 10 first. If GNU comm --check-order reports an error, discard any partial output. See why Linux sorts 10 before 2 for the ordering example.

A shared name appears as both added and removed.
Compare the full rows. A trailing space, a carriage return from a Windows export, or a changed version field can distinguish two lines that look alike. On GNU/Linux, sed -n '1,10l' friday.txt displays the first ten lines with escaped control characters and a visible end marker. Apply the same check to Monday’s export.
Every package upgrade looks like a removal and an addition.
Decide whether the comparison unit is a package name or a name-and-version pair. If you need membership by name, export that field consistently from both inventories before sorting. Keep the original rows for investigating version changes.
A script reports success even when the lists differ.
A successful comm run returns zero even when it prints differences. Check command success first, then inspect the output. An empty additions list still leaves open the possibility of removals.
Apply the exercise to a real inventory
Pick the field that identifies an item
Suppose Friday’s row reads web01 curl 8.5 and Monday’s reads web01 curl 8.6. Comparing the whole line puts the first row in the removal list and the second in the additions list. That is a useful answer to “Which exact records changed?” For “Which machines gained or lost curl?”, use the machine and package fields as the identity. Both records then have the same identity, web01 curl. Keep the original exports nearby so you can look up the version change.
If you compare package names alone, moving a package from web01 to web02 can disappear from the result. Both days still contain the same name. Write down the question before choosing the columns. A list of machine–package pairs answers a different question from a list of package names, even when both came from the same export.
Check what an empty result means
Try the same file on both sides. With LC_ALL=C comm -13 old.unique old.unique, an empty terminal is the expected result: no name occurs only on the right. Change the flag to -12 and the same two inputs print every name. This is a quick control when an empty additions report makes you wonder whether the command read your files at all.
Next, imagine an export with a header and zero records. If the other export contains data, nearly every row may appear to have vanished. Inspect the first few lines and confirm both exports cover the same machines and dates. A successful comparison establishes what is in the files. An inventory report also depends on the collection job having finished.
Separate a failed check from a real change
Save this block as check-additions.sh in the exercise directory and run sh check-additions.sh. On success it prints the added names or an empty-report message. On failure it passes the original exit status back to the caller.
if LC_ALL=C comm --check-order -13 old.unique new.unique > additions.txt; then
if test -s additions.txt; then
cat additions.txt
else
printf '%s\n' 'No package names added.'
fi
else
status=$?
printf '%s\n' 'Comparison failed. Discard the report.' >&2
exit "$status"
fi
On the Friday and Monday inputs, the successful branch prints jq. Using Friday on both sides takes the empty-report branch. A misspelled input filename takes the failure branch. All three runs can leave a file named additions.txt on disk because the shell opens the output file before starting the program. Immediately after running the script, use echo $? to distinguish a completed comparison (zero) from a failed one (nonzero).
Practice with three changes to the sample data
Keep the original inventories. For each check, predict the output before running the command, then compare your prediction with the result.
1. Reverse the timeline
Run LC_ALL=C comm -13 new.unique old.unique. The answer is tar. Why did the same flag that produced jq now produce yesterday’s missing package? The right-hand file is Friday in this command. The operation still means “only on the right.” The labels “added” and “removed” come from your choice of dates. If an automated report suddenly tells you a removed package has returned, check the filenames and collection times before investigating the machine.
Now repeat the swap with -12. You still see curl and git. Shared membership is symmetric. That makes the shared list a poor place to detect a date reversal and a good place to check that both directions agree about the overlap.
2. Make an invisible difference visible
Create one file with a Linux line ending and one with a Windows line ending. With just one record per file, ordering cannot explain the result. Both records may look like the same four letters when you print them on a terminal.
printf 'curl\n' > lf.sample
printf 'curl\r\n' > crlf.sample
LC_ALL=C comm -12 lf.sample crlf.sample
od -An -tx1 lf.sample
od -An -tx1 crlf.sampleThe shared list is empty. The first byte dump ends in 6c 0a; the second ends in 6c 0d 0a. The extra byte is a carriage return. It is part of the line being compared. You can now distinguish a data-format difference from an actual package change. If your export specification requires Linux line endings, normalize a working copy at the import stage and retain the original for inspection. Avoid stripping every space from package descriptions or paths: spaces can distinguish valid records.
3. Reconcile the counts on paper
For the files that preserve duplicates, Friday has four rows and Monday has three. Pair the rows by hand. One curl and one git appear on both days, leaving two Friday rows and one Monday row unpaired. This gives two simple checks: shared plus Friday-only must equal Friday’s total, and shared plus Monday-only must equal Monday’s total. You can run these three commands in the exercise directory to recover the counts:
LC_ALL=C comm -23 old.all new.all | wc -l
LC_ALL=C comm -13 old.all new.all | wc -l
LC_ALL=C comm -12 old.all new.all | wc -lRead the results from top to bottom: two, one, two. Friday reconciles as two plus two. Monday reconciles as one plus two. Repeat using the unique files and only the Friday-only count changes, from two to one. These counts apply to the newline-terminated sample records. In a script, check that both inputs were read successfully before treating a count as evidence.
Choose the comparison your report needs
| Question from a teammate | One row should represent | First check |
|---|---|---|
| Did any new package enter the fleet? | One distinct package name | Right-only names after deduplication |
| Which host gained a package? | One machine and package pair | Right-only pairs |
| Did the installation count fall? | One installation, with repeated names retained | Left-only copies, grouped by name |
| Which version replaced the previous version on web01? | A machine and package identity with a separate version field | Match identities, then compare their version fields |
That last question needs both halves of the record. A line comparison can show one version leaving and another arriving. To establish that one replaced the other on a particular machine, match the machine and package first. Two unpaired lines by themselves cannot prove that relationship.
When to use diff instead
If you are reviewing a configuration file and need to see where a directive moved, keep the original line order and use diff -u. The worked configuration diff shows how to read changed lines in context. Sorting would discard the evidence you need. Use comm when you can finish the sentence “One row represents one ___” and the question concerns which rows belong to each list.
What to put in the handoff note
Use this note when handing the results to a teammate:
Compared
friday.txtwithmonday.txtunderLC_ALL=C. Both have three distinct package names. Monday gainedjqand losttar. Both days listcurl, but its row count fell from two to one. Identifying the affected host requires an inventory with host IDs.
For option details, ordering requirements, and exit status, see the GNU Coreutils manual for comm.