Home·Terminal commands

comm

Text processing

Usage

comm -12 [file1] [file2]

Compares two sorted files and prints three columns — only-left, only-right, in-both; unsorted input gives nonsense, and the flags hide columns rather than select them.

Common flags

FlagMeaning
-1Hide column 1, the lines only in the first file
-2Hide column 2, the lines only in the second file
-3Hide column 3, the lines in both
-iCompare without regard to case
--check-orderGNU: stop when the input is not sorted

Examples

comm -12 a.txt b.txt

Only the lines both files share.

comm -23 a.txt b.txt

Only the lines the first file has.

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

Only the lines the second file added (bash).

These were built to be piped together. Three joined commands are usually shorter than one that does everything.

How to read this

  • Square brackets [ ] mark a part you may leave out.
  • An ellipsis … means you can list more than one.
  • Flags are case-sensitive — in some commands -r and -R do different things.

Questions

Q. What does comm do?

Compares two sorted files and prints three columns — only-left, only-right, in-both; unsorted input gives nonsense, and the flags hide columns rather than select them.

Q. How do I type it?

comm -12 [file1] [file2] — square brackets mark the parts you can leave out.

Q. How many flags are worth knowing?

5 are listed here; the full set is in man comm. This command sits under Text processing.

Related commands

man page: man comm