FactorPad
Faster Learning Tutorials

Linux comm command summary with examples

comm allows you to simply compare two files.
  1. Purpose - Learn what comm is for and how to find help.
  2. Options - Review a few common options and arguments.
  3. Examples - Walk through code examples with comm.
  4. A tip - Finish off with one more insight.
Paul Alan Davis, CFA, October 19, 2016
Updated: July 31, 2018
In this tutorial, 35 of 100, below find a 3-4 minute introductory video, a text-based tutorial and all of the code examples from the video.

Outline Back Next

~/ home  / tech  / linux essentials  / comm command


The Linux comm command

Beginner

Learn to compare two files at the Linux command line.

Video Tutorial

Linux comm command summary with examples (3:36)

Videos can also be accessed from the YouTube Playlist.

Video Script

The Command and Why You Need It

Our thirty-fifth word, or command to memorize is comm from our category Text.

comm allows you to simply compare two files.

Recall that the diff command had many bells and whistles, so comm is an alternative.

Before we start, it helps to think of commands as mini programs and most follow this structure: command -option(s) argument(s).

The comm command has 8 options, instead of the nearly 50 available with diff. And the arguments are the files you'd like to compare.

Like most commands, help is available with double-dash --help. The output here has three columns. First, lines unique to the first file. Second, those unique to the second. Then lines in both files. So with these options you can eliminate any of the default columns.

So why is comm an important command? Well, you might be looking for an uncomplicated way to compare files. And now you know how to do that.

Demonstration

Okay, the best way to embed this in your memory is by typing in your own terminal window.

Find this on your Mac using a program called Terminal. On Linux use Terminal or Konsole, and currently Microsoft is adding this functionality to Windows.

Here we go. Earlier we created two text files using a directory listing. Let's compare them without options, using comm video34.txt video35.txt.

$ comm video34.txt video35.txt Oct 4:video05.txt Oct 10:video11.txt Oct 10:video16.txt Oct 12:video19.txt Oct 12:video20.txt Oct 13:video21.txt Oct 13:video23.txt Oct 15:video24.txt Oct 15:video26.txt Oct 15:video27.txt Oct 17:video31.txt Oct 18:video32.txt Oct 18:video33.txt Oct 9:video16.txt comm: file 1 is not in sorted order Oct 12:video19.txt Oct 12:video20.txt Oct 13:video21.txt Oct 13:video23.txt Oct 15:video24.txt Oct 15:video26.txt Oct 15:video27.txt Oct 17:video31.txt Oct 18:video32.txt Oct 18:video33.txt

Here (first column) we have unique to the first file. The second column unique to the second and here (third column) what is in common. Are you confused? Well, if you saw the last video recall that video34.txt had a line that wasn't in the proper order, as noted here (line noting file 1 is not in sorted order) so all of these (first column) show up as differences. That's a hassle huh?

And last, let's see what's in common here, by adding the -12 option, suppressing these two columns (1 and 2) and then rerun.

$ comm -12 video34.txt video35.txt Oct 4:video05.txt Oct 10:video11.txt comm: file 1 is not in sorted order

So we know what matches now, but have some work to do. Then the lesson here is to make sure files are sorted, or comm won't be of much help.

A Final Tip

Okay now you know how to use comm. And you know the syntax for commands, options and arguments.

One last tip about the comm command. So comm provides a second option, but you may prefer diff or a third option introduced next.

Okay, thanks for visiting today. I hope this was a helpful introduction to the comm command.


Learn More About The Series

For an overview of the 100 videos, the 8 quizzes, a cheat sheet, the categories and a Q&A section, visit:


What's Next?

If you like the videos, subscribe to the YouTube Channel here.

  • For the Outline to all 100 tutorials, click Outline.
  • To go back to the diff command, hit Back.
  • To tune in to our third file comparison command sdiff, click Next.

Outline Back Next

~/ home  / tech  / linux essentials  / comm command



 
 
Keywords:
linux comm command
comm linux
linux compare files
unix comm
compare text files
linux comm examples
linux compare two files
linux compare line by line
comm command syntax
linux comm options
linux comm tutorial
man comm
compare documents
compare two text files
compare tool
compare docs
linux comm summary