Tutorial on using unix diff for comparing two files

Posted: June 14, 2011 in Tutorials
Tags: ,

For comparing two files on unix, diff utility is often used. In this tutorial I will cover on understanding output generated by diff utility. Unix diff actually works on Longest common sequence algorithm, which means it tries to find the longest common sequence between two files and compares the remaining uncommon part, part which is uncommon is known as Hunks. So if two files are exactly same, then there will be only longest common sequence and no hunks, while in case if files are totally different then there will be hunks only and no common sequence.

Diff change notation includes 2 numbers and a character between them. Characters tell you what kind of change was discovered:

d – a line was deleted
c – a line was changed
a – a line was added

Number to the left of the character gives you the line number in the original (first) file, and the number to the right of the character tells you the line number in the second file used in comparison.

Consider two files “file1” and “file2” have following contents:

File1:

A

B

C

File2:

D

B

C

Now when you compare these files, diff will generate following output:

47 C% diff file1 file2
3c3
< c

> d

Now here C stands for change, number on left side of character shows line number corresponding to file1 and the one on right are corresponding to file2.

So 3c3 means line 3 on file1 is changed. contents after < shows contents in file1 at line3 and contents after > shows contents at line3 in file2.

Now if i add a new empty line in file2 and compare the two files, then diff output will be like:

52 C% diff file1 file2
0a1
>
3c4
< c

> d

Here “a” in 0a1 means contents after > (Empty line in this case on line 1 of file2) needs to be added in file1 after 0 line in order to make it identical.

If file2 could have contained two empty lines in beginning, then diff output would be :

54 C% diff file1 file2
0a1,2
>
>
3c5
< c

> d

which means contents of file2 (line1 & line2) needs to be added to file1 after line 0 (0 refers to beginning of file in diff output).

If file1 and file2 could have contained the following contents:

File1:

a

b

c

File2:

a

b

Then on diffing these two files using diff utility, then following output is displayed

56 C% diff file1 file2
3d2
< c

Here 3d2 means line 3 in file1 needs to be deleted in order to make file1 and file2 identical.

Advertisement
Comments
  1. Tutorial on using unix diff for comparing two files Techbie's Weblog Excellent goods from you, man. I have understand your stuff previous to and you’re just too great. I actually like what you’ve acquired here, certainly like what you’re stating and the way in which you say it. You make it enjoyable and you still care for to keep it wise. I can not wait to read far more Tutorial on using unix diff for comparing two files Techbie's Weblog again from you. Thanks For Share.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s