Text and processing

The comm command

Compares two sorted files and shows in three columns which lines appear only in the first, only in the second, or in both.

Syntax

comm [opciones] archivo1 archivo2Try it in the terminal →

Most useful options

-1Hides the column of lines found only in the first file
-2Hides the column of lines found only in the second file
-3Hides the column of shared lines. With this option only the differences in both directions remain
--totalAdds a final line with the number of lines in each column
--output-delimiter CADENAChanges the separator between columns, which is a tab by default

Copy-ready examples

$ comm socios.txt inscritos.txtshows the three columns: no indent for the first file, one tab for the second and two tabs for shared lines.

Example output

ana
		beto
		carla
	david
$ comm -12 <(sort socios.txt) <(sort inscritos.txt)intersection: only the lines present in both. <() sorts each file on the fly in Bash.

Example output

beto
carla
$ comm -23 socios.txt inscritos.txtwhat is in socios.txt but missing from inscritos.txt.

Example output

ana

You can try them in the browser terminal: it does not touch your computer.

Common errors

MessageCauseFix
comm: missing operand after 'socios.txt'You gave only one file. comm needs exactly two to compare.Add the second file: comm socios.txt inscritos.txt.
comm: unknown option -- 4comm has no -4 option: only -1, -2 and -3 exist. This is the text coreutils 8.32 prints.Check the valid options with comm --help.
comm: nonexist: No such file or directoryOne of the files does not exist or the path is misspelled.Check the name with ls and run comm again.
comm: file 1 is not in sorted orderThe first file is not sorted. comm warns, but the affected lines are not compared correctly.Sort it first with process substitution: comm <(sort a.txt) <(sort b.txt).

Typical mistakes

  • Both inputs must be sorted with the same criteria. If they are not, the result can be wrong and a warning does not always appear.
  • Always sort with the same locale, for example LC_ALL=C, or uppercase letters and accents will change the order and the result.
  • Lines are compared whole. A trailing space or a Windows CRLF line ending makes two identical lines fail to match.

Differences from similar commands

  • diff: compares files in their original order and prints the changes as a patch. comm groups lines into set columns regardless of where they were.
  • join: joins two files on a key field, like a relational table. comm only compares whole lines.

Related commands

FAQ

How do I get the lines that appear in both files?

Sort both files and use comm -12. It is the equivalent of a set intersection.

What does comm -3 do?

It hides the shared lines and keeps the ones that appear in only one of the two files: the symmetric difference.

More Text and processing commands

Learn to actually use it

The basic Linux course takes you from zero to console fluency with 24 self-grading lessons.

Start the free course