Solaris Advanced User's Guide

Viewing Differences Between Files (diff)

Use the diff command to view differences between similar files. The following command scans each line in leftfile and rightfile to check for differences.


$ diff leftfile rightfile

When the diff utility finds a line or lines that differ, diff determines if the difference is the result of an addition, a deletion, or a change to the line, and how many lines are affected. diff tells you the respective line number or numbers in each file, followed by the relevant text from each file.

If the difference is the result of an addition, diff displays a line with the following format.


   
l[,l] a r[,r]

In the previous example, 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 the difference is the result of a change on the line, diff uses a c.

The relevant text 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, diff does not display output.

For more information on the diff(1) command, refer to the man Pages(1): User Commands.

Comparing Three Different Files (diff3)

To compare three different versions of a file, use the diff3 command.


$ diff3 file1 file2 file3

diff3 compares three versions of a file and publishes the differing ranges of text that are flagged with these codes:

==== all three files differ

====1 file1 is different

====2 file2 is different

====3 file3 is different

Using bdiff on Large Files

If you are comparing large files, use bdiff instead of diff. Use the diff command syntax with bdiff.


$ bdiff leftfile rightfile

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