Articles
Published · November 18, 2026

Find Lines Two Text Lists Share or Lack With the comm Command

comm compares two sorted text files and prints lines unique to each plus lines they share, in three columns. Sort first, then use -12, -23 or -13 to keep one answer.

Also in: Español中文हिन्दी

Use comm file1 file2 to compare two sorted text lists. It prints three columns: lines only in the first file, lines only in the second, and lines in both. Add -12 to keep only the shared lines, or -23 to keep only what the first list has and the second lacks.

That makes comm the right tool when order doesn't matter and you want set membership, such as which tags appear in both exports, which tasks vanished since last week, or which filenames are missing from a backup list. diff reports edits in order. comm answers "in A, in B, or both?" directly.

The three columns comm prints

The man page defines the output: column one holds lines unique to FILE1, column two lines unique to FILE2, and column three lines common to both. The GNU Coreutils manual adds that the columns are separated by a single TAB.

Take two small sorted lists:

$ cat a.txt
apple
banana
cherry

$ cat b.txt
banana
cherry
date

Run comm a.txt b.txt and you get:

apple
		banana
		cherry
	date

apple sits flush left because only a.txt has it. date has one leading tab because only b.txt has it. banana and cherry have two tabs because both files do. The indent is the column.

If the synopsis comm [OPTION]... FILE1 FILE2 looks cryptic, our guide to reading a man page SYNOPSIS explains the brackets and ellipses. A file name of - reads standard input, but only one of the two operands may be -.

Sort first, or comm gives wrong answers

comm doesn't sort anything for you. The POSIX specification says both files should be ordered in the current collating sequence, and that if they aren't, the results are unspecified. The GNU manual says the inputs must be sorted using the collating sequence of the LC_COLLATE locale, and that plain sort output with no options is suitable.

So sort each list into a new file, then compare:

sort a.txt > a.sorted
sort b.txt > b.sorted
comm a.sorted b.sorted

In bash or zsh you can skip the temporary files with process substitution:

comm <(sort a.txt) <(sort b.txt)

If a list has repeated lines, sort and uniq can clean it up first with sort -u.

One locale caution from POSIX: when processing pathnames, it recommends setting LC_ALL (or at least LC_CTYPE and LC_COLLATE) to POSIX or C, because pathnames can contain byte sequences that aren't valid characters in some locales. If you do, use the same setting for the sort and the comm step so the two agree on order.

Keep only one column: -1, -2, -3 recipes

The options -1, -2 and -3 each suppress a column. Combine them to keep the one you want:

| Command | You get | | --- | --- | | comm -12 a.sorted b.sorted | lines in both files | | comm -23 a.sorted b.sorted | lines only in a.sorted | | comm -13 a.sorted b.sorted | lines only in b.sorted | | comm -3 a.sorted b.sorted | lines in one file but not the other |

The man page's own examples are comm -12 file1 file2 for lines present in both and comm -3 file1 file2 for lines in one but not the other. If you suppress -1, -2 and -3 together, comm writes nothing at all.

POSIX also shows chaining three lists by feeding one result back in on standard input:

comm -12 xcu xpg3 | comm -12 - svid89

That prints the lines found in all three files. Because -12 leaves a single column with no leading tabs, the output is clean enough to pipe straight on.

Catch unsorted input and count the results

By default, GNU comm diagnoses wrongly sorted input only when a file contains lines it can't pair. In that case it exits nonzero and the output shouldn't be used. Two options change that, per the GNU manual:

  • --check-order makes unsorted input a fatal error, even if every line pairs up.
  • --nocheck-order never reports unsorted input. The manual warns that with wrongly sorted input the output is unpredictable.

Use --check-order in scripts. A plausible but wrong answer is worse than an error.

Don't rely on the exit status to tell you whether the lists differ. The manual states that comm's exit status doesn't depend on the comparison: zero on normal completion, nonzero on error.

To count, GNU adds --total:

$ comm --total -123 a.sorted b.sorted
1	1	2	total

That's one line only in a.sorted, one only in b.sorted, two in both. The manual calls this a GNU extension and suggests wc for portable scripts: comm -23 a.sorted b.sorted | wc -l.

Two more GNU options help in scripts. --output-delimiter=STR replaces the TAB between columns. And -z (--zero-terminated) uses NUL instead of newline as the line delimiter, which the manual recommends alongside find -print0 and xargs -0 so file names with blanks stay intact.

Doing this with Carets

comm runs in a terminal, so you'll use it on a Mac or over SSH. Carets fits around it. It's a native notes and code editor for iPhone, iPad, and Mac that keeps your lists as plain text files, so the same tasks.txt you sort and compare on the command line opens in Carets with no export step.

Three features matter here:

  • Plain text and Markdown in one editor: lists stay portable and diff cleanly, which is what sort and comm need.
  • Files, projects, and tags: keep each month's list in a project and tag the ones you compare often.
  • Native and fast on iOS and Mac: review a comm -23 result on your phone without waiting on a web wrapper.

We think Carets is the best fit when your working notes live as text and you want them on every device. It isn't a shell, so run comm itself on a Mac, then read the results anywhere. Get Carets on the App Store for iPhone, iPad, and Mac. To search inside those notes, see searching plain text notes with grep.

Frequently Asked Questions

Does comm need sorted input?

Yes. POSIX says both files should be ordered in the current collating sequence and that results are unspecified otherwise. Run sort on each file first, and use --check-order in GNU comm to fail loudly if you forget.

How do I show only the lines two files have in common?

Use comm -12 file1 file2. It suppresses the columns for lines unique to either file and leaves the lines found in both.

What is the difference between comm and diff?

comm treats each file as a sorted set of lines and reports membership in three columns. diff compares in order and reports edits. For lists where order doesn't matter, comm is the simpler fit.

Does comm's exit status tell me whether the files differ?

No. The GNU manual says the exit status doesn't depend on the comparison result. It is zero on normal completion and nonzero on error.