DIFF(1) BSD DIFF(1)
NAME
diff - differential file and directory comparator
SYNOPSIS
diff [ -l ] [ -r ] [ -s ] [ -Sname ] [ -cefhn ] [ -biwt ] dir1 dir2
diff [ -cefhn ] [ -biwt ] file1 file2
diff [ -Dstring ] [ -biw ] file1 file2
DESCRIPTION
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 that are different. The diff command
lists binary files that differ, common subdirectories, and files that
appear in only one directory.
When run on regular files, and when comparing text files that differ
during directory comparison, diff tells what lines must be changed in the
files to bring them into agreement. Except in rare circumstances, diff
finds the smallest sufficient set of file differences. If neither file1
nor file2 is a directory, then either may be given as -, in which case
diff uses the standard input. If file1 is a directory, diff uses a file
in that directory whose filename is the same as the filename of file2,
and vice versa.
There are several options for output format. The default output format
contains lines of these forms:
n1 a n3,n4
n1,n2 d n3
n1,n2 c n3,n4
These lines resemble ed(1) commands to convert file1 into file2. The
numbers after the letters pertain to file2. In fact, by exchanging `a'
for `d' and reading backward you can ascertain how to convert file2 into
file1. diff, like ed, abbreviates as a single number identical pairs
where n1 = n2 or n3 = n4.
Following each of these lines, diff prints all affected lines from the
first file, flagged by <. Then diff prints all affected lines from the
second file, flagged by >.
OPTIONS
The valid options for diff are presented below. The first four options
(-l, -r, -s, and -S) pertain to comparisons between directories.
-l Produce long output format. Pipe each text file diff through
pr(1) to paginate it, and remember and summarize other
differences after all text file differences are reported.
-r Apply diff recursively to common subdirectories encountered.
-s Report files that are the same, that are otherwise not
mentioned.
-Sname Start a directory comparison, beginning with file name.
The following options are mutually exclusive, except for -b, -w, -i, or
-t, which you can specify with any of the others.
-e Produce 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
diff adds extra commands to the output when comparing
directories with -e, so that the result is a sh(1) script for
converting text files that are common to the two directories,
from their state in dir1 to their state in dir2.
-f Produce a script similar to that of -e, not useful with ed, and
in the opposite order.
-n Produce 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.
-c[n] Produce a comparison with lines of context. The default is to
present three lines of context; you can change it. For
example, -c10 changes the lines of context presented to 10.
With -c the output format is modified slightly. The output
begins with identification of the files involved and their
creation dates, then each change is separated by a line with a
dozen *'s. The lines removed from file1 are marked with -;
those those added to file2 are marked with +. Lines which are
changed from one file to the other are marked in both files
with !.
Changes that 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 interpret.)
-h Do a fast, half-hearted job. This option is useful only when
changed stretches are short and well separated, but it does
work on files of unlimited length.
-Dstring 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 the defining string is
equivalent to compiling file1, while defining string yields
file2.
-b Ignore trailing blanks (spaces and tabs), and cause other
strings of blanks to compare equal.
-w Ignore white space (blanks and tabs) completely. For example,
"if ( a == b )" will compare equal to "if(a==b)".
-i Ignore the case of letters. For example, "A" compares equal to
"a".
-t Expand tabs in output lines. Normal or -c output adds
character(s) to the front of each line which may change the
indentation of the original source lines and make the output
listing difficult to interpret. This option preserves the
original source's indentation.
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 dot (.).
When you specify -b, -w, or -i, and diff is comparing directories, it
first compares the files as in cmp(1), and then decides to run the diff
algorithm if they are not equal. This may cause a small amount of
spurious output if the files are identical except for insignificant
blank-string or case differences.
FILES
/tmp/d?????
/usr/lib/diffh for -h
/bin/diff for directory diffs
/bin/pr
SEE ALSO
cmp(1), cc(1), comm(1), diff3(1), ed(1), pr(1)
Using Your BSD Environment.