How to compare two file ?

In Linux we can compare whether two files are the same or not using more than one command. They are
  1. cmp command
  2. diff command
  3. sdiff command


cmp command

Using cmp command we can compare two file byte by byte. If there is any difference between the files, the command displays  the first byte and line number where the two files differ. The syntax of cmp file is
cmp [options] file1 file2

Example

Consider two files , file1 and file2 and displayed below is the contents of both files

file1
This is a demo to compare two files
I am file1
This is the third line
Linux is the best

file2
This is a demo to compare two files
I am file12
This is the third line
Linux is the best OS

Now compare both files with cmp command
cmp file1 file2
Sample Output:
file1 file2 differ: byte 46, line 2

The output means file1 and file2 differ from each other and the first occurrence of difference appears at byte 46, line2


diff command

Using diff command you can compare two files line by line.  This is more advanced than the cmp command and displays all the differences. The syntax for diff command is 
diff [options] file1 file2

diff command not only tells you the difference between 2 files but make suggestions to make the files similar. Following characters in a diff commands output have such significance


a - Added
c - Changed
d - deleted 

Example

Consider two files, file1 and file2 and displayed below is the contents of both files

file1                                                                                            file2
This is a demo to compare two files         This is a demo to compare two files
I am file1                                  I am file2
This is the third line                      This is the third line
Linux is the best                           Linux is the best OS

Now compare both files with diff command
diff file1 file2
Sample Output:
2c2
< I am file1
---
> I am file2
4c4
< Linux is the best
---
> Linux is the best OS

2c2 in the first line of output means there is difference in line 2 of both the files. To make both files similar change line 2 of file1 with line2 of file2. The string after less than(<) symbol in the second line of the output is the line 2 content of file1 and the string after greater than(>) symbol in the fourth line of the output is the line 2 of file2. Similarly the rest of the output say there is a difference between 2 files in line4.

I am now going to make some modification to file1 and the file's content will be as follows

file1                                                                                            file2
This is a demo to compare two files         This is a demo to compare two files
I am a file                                 I am a file
This is the third line                      Linux is the best
Linux is the best                          

Now compare both files with diff command

diff file1 file2
Sample Output:
3d2
< This is the third line
Here 3d2 means, line number 3 in file1 is missing in file2 and deleting the line 3 in file1 will make the both files similar from line 2 onwards.
Again I am modifying the files and after the modification the files will look like

file1                                                                                            file2
This is a demo to compare two files         This is a demo to compare two files
I am a file                                 I am a file
This is the third line                      This is the third line
Linux is the best                           Linux is the best
                                            End Of File

Comparing both files with diff command
diff file1 file2
Sample Output:
4a5
> End Of File
A line was added to the file2. 4a5 means add a new line after line 4 of the file1. The text to be added is displayed in the second line of the output

sdiff command


Apart from cmp and diff command you can also use sdiff command which will give a side-by-side comparison of two files.The syntax of sdiff command is

sdiff [options] file1 file2

Example
sdiff file1 file2
Sample Output:
This is a demo to compare two files           This is a demo to compare two files
I am file1                               |    I am file2
This is the third line                        This is the third line
Linux is the best                        |    Linux is the best OS

In the output, content of both files are shown side-by-side and differences are marked with a pipe ( | ) symbol.
For more options on cmp, diff and sdiff see corresponding man pages

Comments

Popular posts from this blog

Understanding awk command with examples

Understanding sed command with example -Part 1

How to install a Software in Linux