diff -- compare two text files


diff [ -br ] [ -c | -C n | -e | -f | -h ] file1 file2


The diff command compares file1 and file2, and writes to the standard output a list of the changes necessary to make them identical. If the two files are identical, no output is generated. If file1 or file2 is a dash (-), the standard input is used. If file1 or file2 is a directory, diff uses the file in that directory that has the same name as the file (file2 or file1 respectively) it is compared to. For example:

diff /tmp dog

compares the file named dog that is in the /tmp directory, with the file dog in the current directory.

The normal output contains lines of these 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 can find out in just the same way how to convert file2 into file1. As in ed, identical pairs where n1 = n2 or n3 = n4 are abbreviated as a single number.

Following each of these lines come all the lines that are affected in the first file flagged by ``<'', then all the lines that are affected in the second file flagged by ``>''.

diff recognizes block and character special files and FIFOs, and will not attempt to diff these files against regular files.

If both file1 and file2 are directories, there are a number of possible messages that diff might generate. If a file only occurs in one directory, a message, as in the following example, will be displayed:

$ diff /u/nigel/devices /dev
Only in /u/nigel/devices: rfd01

where /u/nigel/devices is the directory pathname and rfd01 is the filename present only in /u/nigel/devices. If a filename is common to each directory, but the two files are of different types, a message such as the following will be displayed:

$ diff /u/nigel/devices /dev
File /u/nigel/devices/rfd01 is a regular file while file /dev/rfd01
is a character special file

diff recognizes the following options:

Ignores trailing blanks (spaces and tabs) and cause other strings of blanks to compare equal.

Produces output in a form that provides three lines of context.

-C n
Produces output in a form that provides n lines of context.

Produces a script of a, c, and d commands for the editor ed, which will recreate file2 from file1.

Produces a similar script to -e, not useful with ed, and in the opposite order.

Makes diff do a fast, less-rigorous job. This works only when changed stretches are short and well separated, but the files can be of unlimited length.

Applies diff recursively to files and directories when file1 and file2 are directories.

Exit values

Exit status is 0 for no differences, 1 for some differences, 2 for errors.


To maintain multiple versions of a file in the manner of SCCS (see get(CP)), use diff with the -e option and the following shell procedure:

(shift; cat $*; echo '1,$p') | ed - $1

This script works by performing a sequence of editing operations on an original ancestral file. Specify this file as the first argument ($1) to the script. Subsequent arguments ($2,$3, ... are a sequence of ed scripts. These scripts are presumed to have been created in the order given on the command line.

The command to create the scripts is:

diff -e file_N file_N+1 > del_N

ed can recreate the N+1th version of file_1 from the Nth version file_N using the ed commands in del_N:

(cat del_N; echo '1,$p') | ed - file_N > file_N+1

Similarly, the command to recreate the fifth version (file_5) from the ancestral file would be:

(cat del_1 del_2 del_3 del_4; echo '1,$p') | ed - file_1 > file_5

ed takes the commands from each del_* file, performs them on the ancestral file file_1, and writes the desired version to file_5.


There are two versions of diff provided with this release. The version /bin/posix/diff differs from /bin/diff in the operation of the -c and -C options. /bin/posix/diff conforms to the POSIX.2/XPG4 specification that the context line ``***************'' must be output before each set of changes. The /bin/diff version outputs traditional context to provide historic compatibility with other vendors' versions of patch(C).

The version of patch provided by The Santa Cruz Operation, Inc. can understand both context formats.


Except in rare circumstances, diff finds the smallest sufficient set of file differences.

The -e and -f options cannot be used with the -h option.

Editing scripts produced under the -e or -f option do not always work correctly on lines consisting of a single dot ``.''.



(executable used when -h option is specified)

See also

cmp(C), comm(C), ed(C)

Standards conformance

diff is conformant with:

ISO/IEC DIS 9945-2:1992, Information technology - Portable Operating System Interface (POSIX) - Part 2: Shell and Utilities (IEEE Std 1003.2-1992);
AT&T SVID Issue 2;
X/Open CAE Specification, Commands and Utilities, Issue 4, 1992.

© 2003 Caldera International, Inc. All rights reserved.
SCO OpenServer Release 5.0.7 -- 11 February 2003