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:
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.
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
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.