FUNDAMENTALS A Complete Guide for Beginners
The comm command in Linux is used to compare the contents of the files and shows the contents of different files in different columns as the output. The files are compared line by line using the comm command in Linux. In this article, I will explain the syntax, options, and practical uses of the comm command in Linux.
A. Description
The comm command in Linux compares the sorted files and displays the output accordingly. If the files are not sorted, the terminal screen will show an error and will not display the desired output. Using the comm command, the files are compared line by line and the terminal screen displays the contents of the first file in the first column, the contents of the second line in the second column, and the common contents of both files in the third column.
B. Syntax
The comm command has a basic syntax in Linux. It includes the comm command, options & filenames to be compared. And the syntax is in this manner:
comm [OPTION]... [FILE1] [FILE2]
C. Options
Various options are available for the comm command in Linux. I have written down some useful options for this command here. Even so, you can explore the man page to know further details of the comm command.
man comm
Useful Options:
- -1, Suppresses the first column of the output.
- -2, Suppresses the second column of the output.
- -3, Suppresses the third column of the output.
- –check-order, Checks if the inputs are sorted or not.
- –nocheck-order, Does not check whether the inputs are sorted or not.
- –output-delimiter=STR, separate columns with string STR
- –version, output version information, and exit.
Practical Examples of the “comm” Command in Linux
The comm command in Linux is an easy-to-use command that will compare the contents of the sorted files and display the unique contents of the first file and the second file, and the common contents of both the files in the first, second, and third column respectively. Here you will get to know some practical uses of the comm command in Linux. In this article, I have worked with the files below.
Example 1: Compare Two Sorted Files
To compare two sorted files named file1.txt & file2.txt I am using the comm command in Linux. You have to make sure the files are sorted. To do the same you can follow the procedure below:
Steps to Follow >
➊ At first open the Ubuntu Terminal.
➋ Type the following command in the command prompt:
comm file1.txt file2.txt
➌ Then, press the ENTER button.
❹ To check the files manually, type the following command in the command prompt:
cat file1.txt
&
cat file2.txt
➎ Now, press the ENTER button.
Output >
As you can see, the first & second columns of the output show the unique contents of file1.txt & file2.txt respectively. But the third column shows the common contents of both files.
Example 2: Compare Two Unsorted Files Using the “comm” Command in Linux
To compare two unsorted files named file3.txt & file4.txt I am using the comm command in Linux. You have to make sure the files are sorted. To do the same you can follow the procedure below:
Steps to Follow >
➊ At first open the Ubuntu Terminal.
➋ Type the following command in the command prompt:
comm file3.txt file4.txt
➌ Then, press the ENTER button.
❹ To check the files manually, type the following command in the command prompt:
cat file3.txt
&
cat file4.txt
➎ Now, press the ENTER button.
Output >
As you can see, there are errors in the terminal screen. As the files are not sorted the terminal screen does not show the desired output.
Similar Readings
- The “pwd” Command in Linux [4 Practical Examples]
- The “cd” Command in Linux [6 Practical Examples]
- The “ls” Command in Linux [7+ Practical Examples]
Example 3: Suppress the First Column While the Comparison
To suppress the first column of the output after comparing two sorted files named file1.txt & file2.txt I am using the comm command along with the option -1 in Linux. To do so you can follow the process below:
Steps to Follow >
➊ At first open the Ubuntu Terminal.
➋ Type the following command in the command prompt:
comm -1 file1.txt file2.txt
➌ Then, press the ENTER button.
❹ To check the files manually, type the following command in the command prompt:
cat file1.txt
&
cat file2.txt
➎ Now, press the ENTER button.
Output >
In the following image, the first column of the output is suppressed as option -1 is used with the comm command in Linux.
Example 4: Suppress the Second Column While the Comparison
To suppress the second column of the output after comparing two sorted files named file1.txt & file2.txt you can use the comm command along with the option -2 in Linux.
Steps to Follow >
➊ At first open the Ubuntu Terminal.
➋ Type the following command in the command prompt:
comm -2 file1.txt file2.txt
➌ Then, press the ENTER button.
❹ To check the files manually, type the following command in the command prompt:
cat file1.txt
&
cat file2.txt
➎ Now, press the ENTER button.
Output >
In the image below, the second column of the output is suppressed as option -2 is used with the comm command in Linux.
Example 5: Suppress the Third Column While the Comparison
To suppress the third column of the output after comparing two sorted files named file1.txt & file2.txt you can use the comm command along with the option -3 in Linux.
Steps to Follow >
➊ At first open the Ubuntu Terminal.
➋ Type the following command in the command prompt:
comm -3 file1.txt file2.txt
➌ Then, press the ENTER button.
❹ To check the files manually, type the following command in the command prompt:
cat file1.txt
&
cat file2.txt
➎ Now, press the ENTER button.
Output >
In the image below, the third column of the output is suppressed because option -3 is used with the comm command in Linux.
Example 6: Check Whether the Files Are Sorted or Not While the Comparison
To check whether the files named file3.txt & file4.txt are sorted or not, I am using the comm command along with the option –check-order in Linux. When it finds an unsorted line it immediately stops working and shows that the file is unsorted. To do the same you can follow the steps below:
Steps to Follow >
➊ At first open the Ubuntu Terminal.
➋ Type the following command in the command prompt:
comm --check-order file3.txt file4.txt
➌ Then, press the ENTER button.
❹ To check the files manually, type the following command in the command prompt:
cat file3.txt
&
cat file4.txt
➎ Now, press the ENTER button.
Output >
In the image below, the output shows that the files are unsorted using the comm command in Linux.
Similar Readings
- The “mv” Command in Linux [8 Practical Examples]
- The “cp” Command in Linux [6 Practical Examples]
- The “rm” Command in Linux [7 Practical Examples]
Example 7: Does Not Check Whether the Files Are Sorted or Not While the Comparison
To check whether the files named file3.txt & file4.txt are sorted or not, I am using the comm command along with the option –nocheck-order in Linux. When it finds an unsorted line it immediately stops working and shows that the file is unsorted. To do the same you can follow the steps below:
Steps to Follow >
➊ At first open the Ubuntu Terminal.
➋ Type the following command in the command prompt:
comm --nocheck-order file3.txt file4.txt
➌ Then, press the ENTER button.
❹ To check the files manually, type the following command in the command prompt:
cat file3.txt
&
cat file4.txt
➎ Now, press the ENTER button.
Output >
In the following image, using option –nocheck-order with the comm command in Linux the output shows the order of the files checked.
Example 8: Separate the Columns of the Output Using the “comm” Command in Linux
As explained above, by default the columns of the output are separated by spaces. Nevertheless, you can also separate using any string of your choice by using option –output-delimiter=STR along with the comm command in Linux. Here, I am going to compare two files named file1.txt and file2.txt.
Steps to Follow >
➊ At first open the Ubuntu Terminal.
➋ Type the following command in the command prompt:
comm --output-delimiter=# file1.txt file2.txt
➌ Then, press the ENTER button.
❹ To check the files manually, type the following command in the command prompt:
cat file1.txt
&
cat file2.txt
➎ Now, press the ENTER button.
Output >
As you can see, the string I used here is the “#” symbol. The “#” symbol represents the unique contents of file 2 and “##” represents the common contents of both files. “No string” represents the unique contents of file 1.
Example 9: Display the Line Counts Using the “comm” Command in Linux
To display the line counts, you can use the comm command with the option –total in Linux. To do the same you can follow the steps below:
Steps to Follow >
➊ At first open the Ubuntu Terminal.
➋ Type the following command in the command prompt:
comm --total file1.txt file2.txt
❸ Now, press the ENTER button.
Output >
In the following image, the output displays the line counts of each column.
Example 10: Display Version Info Using the “comm” Command in Linux
To display version info, I am using the comm command along with the option –version. To do the same you can follow the process below:
Steps to Follow >
➊ At first open the Ubuntu Terminal.
➋ Type the following command in the command prompt:
comm --version
❸ Now, press the ENTER button.
Output >
In the following image, the version info is displayed.
Conclusion
As stated above, the comm command has multiple applications in Linux. You’ve also got to know the syntax, some functional options, and the practical applications of this command. To become an expert in Linux, go through the command and its practical applications in detail.
Similar Readings
- The “touch” Command in Linux [8 Practical Examples]
- The “mkdir” Command in Linux [6+ Practical Examples]
- The “locate” Command in Linux [7 Practical Examples]
- The “find” Command in Linux [10+ Practical Examples]
- The “chmod” Command in Linux [6 Practical Examples]
- The “chown” Command in Linux [8 Practical Examples]