Compare Files With Comm Command In Linux


Linux have a lot of tools to compare files. These tools provides a lot of powerful features to meet user needs. But sometimes just simple comparison is enough for some users. comm is a tool for this purpose . Comm only compare files and shows differences in a visual manner.

Syntax

We will use following syntax for comm command.

comm [OPTION]... FILE1 FILE2

Help

$ comm --help
Help
Help

Compare

We will simply compare two files those have same and different lines. We just need to provide file names to the comm command. Here is our files.

a.txt

test 
pof 
apple 
banana

b.txt

test 
ok 
apple 
banana 
lemon

Now we issue the command like below.

$ comm a.txt b.txt
Compare
Compare

As we can see the differences ok and lemon are printed in required column.

Check Order

In previous example we see that there are some information about unsorted columns. comm command automatically checks the order of the provided text file. This can be disabled with --nocheck-order parameter.

$ comm --nocheck-order a.txt b.txt
Do Check Order
Do Check Order

Suppress Columns

Columns can be suppressed with the suppress feature. We just need to provide column number to suppress like -1 , -2 , -3

  •  -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)
$ comm -1 a.txt b.txt
Suppress Columns
Suppress Columns

LEARN MORE  Python Set Data Structure With Examples

Leave a Comment