An Overview of Linux Comm Command for File Comparison and Difference Detection

04 May 2023 Balmiki Mandal 0 Linux

Comm Command in Linux

The comm command in Linux is a command used to compare two sorted files line by line and then output a three-column result that summarizes the differences between those files. This command is useful for quickly seeing what lines are common, unique, or differing between the two files being compared. Let's take a closer look at how this command works.

Using the Comm Command

To use the comm command, you need two files that have already been sorted. If the files are not already sorted, you can sort them using the sort command. You also need to ensure that both files contain unique lines; otherwise, the comm command may produce unexpected results.

Once you have your two sorted files, you can now use the comm command to compare them. To do so, enter the following command into the terminal:

comm [options] file1 file2

Where the options are any of the options supported by the comm command, file1 is the first sorted file and file2 is the second sorted file.

Options for the Comm Command

The comm command supports several different options for customizing its output. These include:

  • -1: Suppress column 1 (lines unique to file1).
  • -2: Suppress column 2 (lines unique to file2).
  • -3: Suppress column 3 (lines that appear in both files).
  • -u: Generate output with all three columns regardless of whether any lines occur in both files.

Comm Command Output

The comm command typically produces output in three columns. The first column contains all the lines that are unique to file1, the second column contains all the lines that are unique to file2, and the third column contains all the lines that appear in both files. An example of the output from the comm command is shown below.

file1.txt    file2.txt
foo          bar
baz          qux
            zap
            zab

In this example, the first column indicates that the line “foo” is unique to file1.txt, while the second column indicates that the line “bar” is unique to file2.txt. The third column indicates that the lines “zap” and “zab” appear in both files.

Conclusion

The comm command in Linux is a handy tool for quickly comparing two sorted files line by line. With the various options supported by this command, you can customize the output to get the information you need. If you're ever in need of a quick way to compare two files, then the comm command is just the tool for the job.

BY: Balmiki Mandal

Related Blogs

Post Comments.

Login to Post a Comment

No comments yet, Be the first to comment.