DIFF(1) BSD General Commands Manual DIFF(1)NAMEdiff — differential file and directory comparator
SYNOPSISdiff [-cefhn] [-biwt] file1 file2
diff [-Dstring] [-biw] file1 file2
diff [-l] [-r] [-s] [-cefhn] [-biwt] [-Sname] dir1 dir2
The diff utility compares the contents of file1 and file2 and writes to
the standard output the list of changes necessary to convert one file
into the other. No output is produced if the files are identical.
Output options (mutually exclusive):
-c produces a diff with lines of context. The default is to present
3 lines of context and may be changed, e.g., to 10, by -c10.
With -c the output format is modified slightly: the output begin‐
ning with identification of the files involved and their creation
dates and then each change is separated by a line with a dozen
*'s. 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 `! '. Changes
which lie within <context> lines of each other are grouped
together on output. (This is a change from the previous ``diff
-c'' but the resulting output is usually much easier to inter‐
-e produces output in a form suitable as input for the editor util‐
ity, ed(1), which can then be used to convert file1 into file2.
Extra commands are added to the output when comparing directories
with -e, so that the result is a sh(1) script for converting text
files which are common to the two directories from their state in
dir1 to their state in dir2.
-f identical output to that of the -e flag, but in reverse order.
It cannot be digested by ed(1).
-h Invokes an alternate algorithm which can handle files of very
long lengths. There is a trade off. The algorithm can only deal
with changes which are clearly delimited and brief. Long sections
of changes and overlaps will confuse it.
-n produces a script similar to that of -e, but in the opposite
order and with a count of changed lines on each insert or delete
command. This is the form used by rcsdiff(1).
creates a merged version of file1 and file2 on the standard out‐
put, 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.
-b causes trailing blanks (spaces and tabs) to be ignored, and other
strings of blanks to compare equal.
-i ignores the case of letters. E.g., ``A'' will compare equal to
-t will expand tabs in output lines. Normal or -c output adds char‐
acter(s) to the front of each line which may screw up the inden‐
tation of the original source lines and make the output listing
difficult to interpret. This option will preserve the original
-w is similar to -b but causes whitespace (blanks and tabs) to be
totally ignored. E.g., ``if ( a == b )'' will compare equal to
Directory comparison options:
-l long output format; each text file diff´d is piped through pr(1)
to paginate it, other differences are remembered and summarized
after all text file differences are reported.
-r causes application of diff recursively to common subdirectories
-s causes diff to report files which are the same, which are other‐
wise not mentioned.
-Sname re-starts a directory diff in the middle beginning with file
If both arguments are directories, diff sorts the contents of the direc‐
tories by name, and then runs the regular file diff algorithm, producing
a change list, on text files which are different. Binary files which
differ, common subdirectories, and files which appear in only one direc‐
tory are described as such.
If only one of file1 and file2 is a directory, diff is applied to the
non-directory file and the file contained in the directory file with a
filename that is the same as the last component of the non-directory
If either file1 or file2 is ‘-’, the standard input is used in its place.
The default (without -e, -c, or -n options) output contains lines of
these forms, where XX, YY, ZZ, QQ are line numbers respective of file
XXaYY At (the end of) line XX of file1, append the contents of
line YY of file2 to make them equal.
XXaYY,ZZ Same as above, but append the range of lines, YY through ZZ
of file2 to line XX of file1.
XXdYY At line XX delete the line. The value YY tells to which line
the change would bring file1 in line with file1.
XX,YYdZZ Delete the range of lines XX through YY in file1.
XXcYY Change the line XX in file1 to the line YY in file2.
XX,YYcZZ Replace the range of specified lines with the line ZZ.
XX,YYcZZ,QQ Replace the range XX,YY from file1 with the range ZZ,QQ from
These lines resemble ed(1) subcommands to convert file1 into file2. The
line numbers before the action letters pertain to file1; those after per‐
tain to file2. Thus, by exchanging a for d and reading the line in
reverse order, one can also determine how to convert file2 into file1.
As in ed(1), identical pairs (where num1 = num2) are abbreviated as a
TMPDIR If the environment variable TMPDIR exists, diff will use the
directory specified by TMPDIR as the temporary directory.
/usr/bin/diffh Alternate algorithm version (used by option -h).
/usr/bin/diff for directory diffs
/usr/bin/pr used by the -l option.
SEE ALSOcmp(1), cc(1), comm(1), ed(1), diff3(1)DIAGNOSTICS
The diff utility exits with one of the following values:
0 No differences were found.
1 Differences were found.
>1 An error occurred.
The -f and -e options do not provide special handling for lines on which
the first and only character is “.”. This can cause problems for ed(1).
When comparing directories with the -b, -w or -i options specified, diff
first compares the files ala cmp, and then decides to run the diff algo‐
rithm 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 dif‐
ferences are insignificant white space or case differences.
A diff command appeared in Version 6 AT&T UNIX.
4th Berkeley Distribution June 30, 1993 4th Berkeley Distribution