DIFF(1) — Unix Programmer’s Manual
NAME
diff − differential file and directory comparator
SYNOPSIS
diff [ −l ] [ −r ] [ −s ] [ −cefh ] [ −b ] dir1 dir2
diff [ −cefh ] [ −b ] file1 file2
diff [ −Dstring ] [ −b ] file1 file2
DESCRIPTION
Diff is a differential directory and file comparator.
Directory Comparison
If both arguments are directories, diff sorts the contents of the directories by name, and then runs the regular file diff algorithm (described below) on text files which are different. It lists binary files which differ, common subdirectories, and files which appear in only one directory.
Options:
Options when comparing directories are:
−l Long output format. Each text file diff is piped through pr(1) to paginate it. Other differences are remembered and summarized after all text file differences are reported.
−r Applies diff recursively to common subdirectories encountered.
−s Causes diff to report files which are the same; otherwise these are not mentioned.
−Sname Performs a diff beginning at specified file name in the directory.
File or Directory Comparison
When run on regular files, and when comparing text files which differ during directory comparison, diff identifies those lines which must be changed in the files to bring them into agreement. Except in rare circumstances, diff finds a smallest sufficient set of file differences. If neither file1 nor file2 is a directory, then either may be given as − , in which case the standard input is used. If file1 is a directory, then a file in that directory whose filename is the same as that of file2 is used (and vice versa).
There are several options for output format. The default output format contains lines of the following forms:
n1 a n3,n4
n1,n2 d n3
n1,n2 c n3,n4
These lines resemble ed commands to convert file1 into file2. The numbers after the letters pertain to file2. In fact, by exchanging “a” for “d” and reading backward, one may ascertain equally how to convert file2 into file1. As in ed, identical pairs in which n1 = n2 or n3 = n4 are abbreviated as a single number.
Following each of these lines come all the affected lines in the first file, flagged by < ; then all the affected lines in the second file, flagged by > .
Options:
Except for −b, which may be given with any of the others, the following options are mutually exclusive:
−b Causes trailing blanks (spaces and tabs) to be ignored, and other strings of blanks to compare equal.
−c Produces a diff with lines of context. The default is to present 3 lines of context. This may be changed to 10, for example, by −c10. With −c the output format is modified slightly: the output begins with identification of the files involved and their creation dates, and then each change is separated by a line containing a dozen asterisks. The lines removed from file1 are marked with − ; those added to file2 are marked + . Lines which are changed from one file to the other are marked in both files with ! .
−Dstring Causes diff to create a merged version of file1 and file2 on the standard output, with C preprocessor controls included, so that a compilation of the result without defining string is equivalent to compiling file1, while defining string will yield file2.
−e Produces a script of “a,” “c” and “d” commands for the editor ed, which will recreate file2 from file1. In connection with −e, the following shell program may help maintain multiple versions of a file. Only an ancestral file ($1) and a chain of version-to-version ed scripts ($2,$3,...) made by diff need be on hand. A “latest version” appears on the standard output.
(shift; cat $∗; echo ´1,$p´) │ ed − $1
Extra commands are added to the output when comparing directories with −e, so that the result is an sh(1) script for converting text files common to the two directories from their state in dir1 to their state in dir2.
−f Produces a similar script to make file2 from file1, but in reverse order. Not compatible with ed.
−h Operates a rough, quick pass of diff. Works only when changed stretches are short and well separated, but does work on files of unlimited length.
FILES
/tmp/d?????
/usr/lib/diffh for −h
/bin/pr
SEE ALSO
cmp(1), cc(1), comm(1), ed(1), diff3(1)
DIAGNOSTICS
Exit status is 0 for no differences, 1 for some, 2 for trouble.
BUGS
Editing scripts produced under the −e or −f option are naive about creating lines consisting of a single period ( . ).
When comparing directories with the −b option specified, diff first compares the files in the manner of cmp, and then runs the diff algorithm if they are not equal. This may cause a small amount of spurious output if the files then turn out to be identical because the only differences are insignificant blank string differences.
4th Berkeley Distribution — 1 August 1985