Linux offers two commands to compare the text written within files: comm and diff. In this article we focus on the use of comm and diff commands in Linux and understand the difference in the output by each.
The comm command is used to compare two sorted files. The output contains three columns. The first column includes lines unique to FILE1, the second column contains lines unique to FILE2 and the third column contains the lines common to both files
|$comm [OPTIONS] FILE1 FILE2|
Figure 8 shows the comparison between two sorted files win and mac. In the output the first column contains lines unique to file win, the second column contains lines unique to mac and the third column contains the common lines.
Note: comm command will work only on sorted files
-1: will suppress the first column from the output
-2: will suppress the second column from the output
-3: will suppress the third column from the output
Another command used to compare files is diff. The command displays the two files and the difference between them. The output contains certain symbols which suggest how to make one file look like the other. The less than (<) symbol with a line means that line should be removed from the first file because it doesn’t appear in the second. The greater than (>) symbol with a line after it means that line should be added from the second line. Apart from this the lines numbers are also mentioned for each file that would be affected by deletion (d), addition (a) and change (c) operations.
|$diff [OPTION]… FILES|
-b: ignore spacing difference
-i: ignore case difference
-w: ignore spacing difference and tab
Q1. Suppose you have two files F1 and F2 which have 100 lines of text each. You need to compare them as they are supposed to be similar. Which command would you use?