OpenWindows Advanced User's Guide

3.4 Looking at Differences Between Files (diff)

It often happens that different people with access to a file make copies of the file and then edit their copies. diff will show you the specific differences between versions of an ASCII file. The command:

$ diff leftfile rightfile

scans each line in leftfile and rightfile looking for differences. When it finds a line (or lines) that differ, it determines whether the difference is the result of an addition, a deletion, or a change to the line, and how many lines are affected. It tells you the respective line number(s) in each file, followed by the relevant text from each.

If the difference is the result of an addition, diff displays a line of the form:

   
l[,l] a r[,r]

where l is a line number in leftfile and r is a line number in rightfile.

If the difference is the result of a deletion, diff uses a d in place of a; if it is the result of a change on the line, diff uses a c.

The relevant lines from both files immediately follow the line number information. Text from leftfile is preceded by a left angle bracket (<). Text from rightfile is preceded by a right angle bracket (>).

This example shows two sample files, followed by their diff output:

$ cat sched.7.15
Week of 7/15

Day:  Time:        Action Item:          Details:

T     10:00        Hardware mtg.         every other week
W     1:30         Software mtg.
T     3:00         Docs. mtg.
F     1:00         Interview
$ cat sched.7.22
Week of 7/22

Day:  Time:        Action Item:          Details:

M     8:30         Staff mtg.            all day
T     10:00        Hardware mtg.         every other week
W     1:30         Software mtg.
T     3:00         Docs. mtg.
$ diff sched.7.15 sched.7.22
1c1
< Week of 7/15
---
> Week of 7/22
4a5
> M     8:30         Staff mtg.            all day
8d8
< F     1:00         Interview

If the two files to be compared are identical, there is no output from diff.

The diff(1) command has many more options than those discussed here. For more information, refer to the man Pages(1): User Commands.

3.4.1 Comparing Three Different Files (diff3)

If you have three versions of a file that you want to compare at once, use the diff3 command as follows:

$ diff3 file1 file2 file3

diff3 compares three versions of a file and publishes disagreeing ranges of text flagged with these codes:

==== all three files differ

====1 file1 is different

====2 file2 is different

====3 file3 is different

3.4.2 Using bdiff on Large Files

If you are comparing very large files, use bdiff instead of diff. Both programs work in a similar manner:

$ bdiff leftfile rightfile

Use bdiff instead of diff for files longer than 3500 lines or so.