← All commands
THE COMMAND REFERENCE

comm

Compare two sorted files and show lines unique to each or shared.

Last updated

macOS / Apple

Implementation-level reference; examples are not certified for every installed version.

Overview

comm compares two files that are already sorted and prints three columns: lines only in the first file, lines only in the second, and lines in both. You hide a column by adding its number as an option, so comm -12 shows only what is in both, and comm -23 only what is in the first file alone.

Background

A comm command appeared in Version 4 AT&T UNIX, according to the macOS manual. Today the name covers GNU comm, the comm that macOS ships, and the comm applet in BusyBox. They agree on -1, -2 and -3. comm needs sorted input in the same order on both sides, or the result is wrong.

Examples

Show the lines that are in both files, which is the -1 -2 hiding of the first two columns:

printf 'a\nb\nc\n' > one.txt && printf 'b\nc\nd\n' > two.txt && comm -12 one.txt two.txt

Show the lines only in the first file:

printf 'a\nb\nc\n' > one.txt && printf 'b\nc\nd\n' > two.txt && comm -23 one.txt two.txt

Show the lines only in the second file:

printf 'a\nb\nc\n' > one.txt && printf 'b\nc\nd\n' > two.txt && comm -13 one.txt two.txt

Sort unsorted input first, with sort, by writing the sorted copies to files:

printf 'c\na\nb\n' | sort > one.txt && printf 'd\nb\nc\n' | sort > two.txt && comm -12 one.txt two.txt

Options

-1
Hide the first column, the lines only in the first file.

-2
Hide the second column, the lines only in the second file.

-3
Hide the third column, the lines in both files.

Common mistakes

Unsorted input gives wrong answers without an error, and GNU may print a warning about it. Both files must also be sorted the same way, for example with the same LC_ALL, so sort them with the same settings you use for comm. When you show all three columns, the second and third are indented with tabs, which can confuse a script; hide the columns you do not need.

Portability

Only the three column options are in the Common examples. GNU adds --check-order and --output-delimiter, and macOS has -i to ignore case. The Common examples were executed on GNU coreutils 9.11, macOS system tools, and BusyBox 1.37.0 (see the development guide for the exact check).

About macOS comm

This is the comm in /usr/bin on macOS. It follows the BSD implementation, and has -i to ignore case.

Apple notes

macOS comm has -i to ignore case when comparing. It has no --check-order or --output-delimiter.

References

Original explanations, with upstream documentation for further reading. FreeBSD references are comparison material, not a claim of Apple version equivalence.