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.
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 defaultCopy-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
| Message | Cause | Fix |
|---|---|---|
| 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 -- 4 | comm 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 directory | One 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 order | The 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
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.
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