How to use Linux comm command?

The Linux comm is a command that compares two sorted files and finds the common and distinct lines in both. By default, the output contains three columns the first column shows the non-matching or unique lines of the first file, the second column shows the non-matching lines of the second file, and the third column shows the matching lines of both the files.

In this article, I will discuss how to use the comm command in Linux along with some examples.

Syntax of Linux comm command

The basic syntax of the Linux comm command is given below –

comm [Options] file1 file2

Where you can use the following options with the given command-

-1 – Suppress column 1 i.e. lines unique to the first file
-2 – Suppress the column 2 i.e. lines unique to the second file
-3 – Suppress the column 3 i.e. lines unique to the third file
--check-order – Checks if the input is correctly sorted
--nocheck-order – Do not check if provided input is correctly sorted
--output-delimiter=STR – Separate column with STR
--total – Display the summary of the output
-z, --zero-terminated -Line delimiter is NUL, not newline
--help – Used to display the help
--version – Display the current version of the comm

How to use the comm command in Linux

The following example demonstrates the usage of the comm command in Linux.

Here file1.txt and file2.txt are files that contain the name of some cities of the world as you can see in the image below.

cat file1.txt

file1.txt

cat file2.txt

file2.txt

Now run the comm command as it given below –

comm file1.txt file2.txt

comm command

You can display the lines that are unique in a specific file lets say in file2.txt by using –

comm -13 file1.txt file2.txt

This will display the given output –

suppress line 1 and 3

If you want to display the summary of the output of the comm command you can use –

comm --total file1.txt file2.txt

You can see the output of the above command in the given image –

total comm

Similarly, you can use the other options with this command.

To know more about the comm command usage and its options you can check its manual page in your terminal by running –

man comm

Conclusion

Here you have learned how to use the comm command in Linux. Now if you have a query then write us in the comments below.

Leave a Comment