Linux Commands

How to use comm command in Linux

The comm command is used in Linux to compare different files, this command compares each and every line of the files and displays the unique lines and common lines of the files in separate columns. The need for comparison of files mostly occurs to the programmers; when they need to find out the modifications made in the program over time.

In this write-up, we are going to explain the utilization of the comm command in Linux with a detailed explanation.

How to use the comm command in Linux

The comm command can be used to compare two files line by line, the general syntax of using the comm command:

$ comm [option] FILE_NAME1 FILE_NAME2

The above command syntax will display the result in three columns; the first column will display the unique lines of the file1, the second column will display the unique lines of the file2, and the third column will display the common lines of both files.

We can also use some options along with the comm command; some commonly used options are:

Options Explanation
-1 It will not display the first column of the result, which contains the unique lines of the file1
-2 It will not display the second column of the result, which contains the unique lines of the file2
-3 It will not display the third column of the result, which contains the common lines of both files; file1 and file2
–check-order It will check all the lines of both files are properly sorted or not
–nocheck-order It will not check the sorting order and just display the results
–help It will show a help message prompt and will exit
–version It will show the version of the information and exits
–total It will display the total number of lines present in each column of the result
-z, –zero-delimiter It will display the files separately instead of the columns; the value of zero delimiters is null
–output-delimiter=[any character] It will replace the “spaces” in the results by using the character you want to put there

To understand all these options, we will consider two text files with the name; mytestfile1.txt and mytestfile2.txt, whose contents can be displayed using the commands:

$ cat mytestfile1.txt

$ cat mytestfile2.txt

First, we will compare both files using the comm command without any options, for this purpose, we have to use the command:

$ comm mytestfile1.txt mytestfile2.txt

As we know, the comm command is applicable on the sorted files, and the given files in the command are not sorted so it generated the “files not sorted” warnings, to remove these comments, we will use the “–nocheck-order” flag, which will ignore checking the sorting order and display the results:

$ comm --nocheck-order mytestfile1.txt mytestfile2.txt

In the above image, we have marked three columns for a better understanding of the results, the first column displayed the unique lines of mytestfile1.txt which is only “Fedora”, the second column displayed the unique lines of mytestfile2.txt which is only “Debian”, and the last column displayed the common lines of both files. If we want to display column 1 (unique lines of mytestfile1.txt) and column 3 (common lines of both files), we will suppress column 2 by using the “-2” flag:

$ comm -2 --nocheck-order mytestfile1.txt mytestfile2.txt

Likewise, we can suppress both column 1, column 2, and display only column 3 (containing the common lines of both files) by running the command:

$ comm -12 --nocheck-order mytestfile1.txt mytestfile2.txt

The output displayed only the third column of the result, in order to find out the total number of lines of each column, use the command:

$ comm --total --nocheck-order mytestfile1.txt mytestfile2.txt

To check whether the sorting order of both files are either in the correct order or not, execute the comm command using the “–check-order” flag:

$ comm --check-order mytestfile1.txt mytestfile2.txt

The results show that file 1 is not in sorted order because the names of the files are not arranged alphabetically either in ascending order or descending order, similarly, the “–zero-delimiter” flag is used:

$ comm --zero-terminated mytestfile1.txt mytestfile2.txt

Likewise, we can use the “–output-delimiter=** ” with the comm command to replace the space with “star (*)”:

$ comm --output-delimiter=** --nocheck-order mytestfile1.txt mytestfile2.txt

To check the version of the comm command:

$ comm --version

If you want to know more about the comm command, you can check its manual by using the command:

$ man comm

Conclusion

The comm command is used to compare the lines of the sorted files in Linux, though, there are some other ways too for comparing files such as the diff command and using the vim editor. The comm command is easy to use and recommended where the files contain scripts and only a simpler comparison is needed. In this write-up, we have discussed the comm command and its various options briefly with the help of examples.

About the author

Hammad Zahid

I'm an Engineering graduate and my passion for IT has brought me to Linux. Now here I'm learning and sharing my knowledge with the world.