comm command in Linux using practical Examples

Linux

The comm command is a powerful tool in Linux that can be used to compare two files line by line and to find out the differences between them. It is available in all major Linux distributions, including Ubuntu, Debian, CentOS, RedHat, and others.

Here at Fixwebnode, we shall look into how to use the comm command in Linux.

 

How to Use the Comm Command ?

The comm command is a command-line tool that is used to compare two files line by line. It can be used to find out the differences between the files, as well as any common lines.

To use the comm command, you need to specify the two files that you want to compare. This can be done using the following syntax:

$ comm [file1] [file2]

For example, if you wanted to compare two files named file1.txt and file2.txt, you could use the following command:

$ comm file1.txt file2.txt

The output of the command will be three columns, with the first column showing the lines that are only in the first file, the second column showing the lines that are only in the second file, and the third column showing the lines that are common to both files.

 

Comm Command Options

The comm command has several options that can be used to customize the output. The following options are available:

  • -1 : Suppress the output of the first column.
  • -2 : Suppress the output of the second column.
  • -3 : Suppress the output of the third column.
  • -c : Ignore case when comparing the files.
  • -d : Include the duplicates in the output.

 

Practical Examples of using Comm Command 

1. Comparing two files

Let's say we have two files named file1.txt and file2.txt and we want to compare them. We can do this using the following command:

$ comm file1.txt file2.txt

This will output the differences between the two files, with the first column showing the lines that are only in the first file, the second column showing the lines that are only in the second file, and the third column showing the common lines.

 

2. Ignoring case

If we want to compare the two files but ignore case, we can use the -c option.

$ comm -c file1.txt file2.txt

This will compare the two files but ignore case, so upper and lower case letters will be treated as the same.

 

3. Suppressing output

If we want to suppress the output of one of the columns, we can use the -1, -2, or -3 options.

For example, if we want to suppress the output of the third column, we can use the following command:

$ comm -3 file1.txt file2.txt

This will compare the two files, but only show the lines that are different between them.

 

4. Including duplicates

If we want to include the duplicates in the output, we can use the -d option.

$ comm -d file1.txt file2.txt

This will compare the two files and include any duplicated lines in the output.

 

5. Comparing multiple files

The comm command can also be used to compare multiple files. For example, if we have three files named file1.txt, file2.txt, and file3.txt, we can compare them using the following command:

$ comm file1.txt file2.txt file3.txt

This will output the differences between the three files, with the first column showing the lines that are only in the first file, the second column showing the lines that are only in the second file, the third column showing the lines that are only in the third file, and the fourth column showing the lines that are common to all files.

 

[Need help in fixing Linux system issues ? We can help you. ]

 


Conclusion

This article covers how to use the comm command in Linux. We've also provided examples of how to use it in different distributions and explained the options available with each command. In fact, you should be able to use the comm command to compare and find the differences between two or more files.

Your Cart