[Previous] [Contents] [Next]

diff

Report the difference between two files (POSIX)

Syntax:

diff [-C n|-c|-e|-D string|-s|-n] [-L label1 [label2]]
     [-br] file1 file2

Options:

-b
Ignore trailing blank characters and treat other strings of blank characters as equivalent.
-c
Produce output in a form that provides three lines of context (see "Context output format").
-C n
Produce output in a form that provides n lines of context (see "Context output format").
-D string
(QNX extension) Produce output merging file1 and file2 using C Language preprocessor controls (see "C output format").
-e
Produce output in a form suitable as input for the Unix line editor. The editor can then be used to convert file1 into file2 (see "Edit output format").
-L label1 [label2]
(historical extension) Print these labels for context information (used by RCS).
-n
Produce output as RCS-style differences.
-r
When file1 and file2 are both directories, apply diff recursively to files and directories of the same name (see "Directory output format").
-s
(QNX extension) Produce only a summary of the differences (see "Summary output format").
file1
The pathname of a file to be compared.
file2
The pathname of a file to be compared.

Description:

The diff utility reports the differences between two files. For any two files, there may be several correct interpretations of the differences between them. The diff utility attempts to generate the smallest number of additions, changes, and deletions required to convert file1 into file2. No output is produced if the files are identical.

The diff utility may generate one of the following output formats:

Default output format

The default output contains lines of this form:

where cmd is one of: a (add), c (change), or d (delete).

The options range1 and range2 are the line numbers that differ between file1 and file2 respectively.

A range will consist of "startline" or "startline,endline" if more than one line is affected.

Each of the default output lines is followed by a sequence of zero or more affected lines from file1, preceded by the < character, then a sequence of zero or more affected lines from file2 preceded by the > character. If lines are actually printed from both (i.e. a "change" command), then a line of three dashes (---) is output between the listing of lines from file1 and file2.

Edit output format

The edit output is similar to the default output, except only the edit commands necessary to convert file1 into file2 are output with the relevant text.

Context output format

The context output consists of an identification line for each file, containing the filename and modification date of the file. If the -L option is specified, labels will also be printed. Each of the remaining output lines is prefixed by one of the following characters:

Character: Meaning:
space an unaffected line, shown for context
- a line to be deleted from the first file
+ a line to be added to the second file
! a line to be changed

Directory output format

For each file present in both directories, if the files are not identical the string:

is produced. The field diff_options refers to the options given to diff to check the two files. Each file that is present in only one directory is reported by the string:

Subdirectories that are common to the two directories are reported as:

C output format

The C output format is a merged version of file1 and file2 with C preprocessor controls included so that compiling without defining string is equivalent to compiling file1. Defining string makes it equivalent to compiling file2.

Summary output format

The summary output format is one line that contains two decimal numbers separated by a single tab character. The numbers represent the number of lines inserted and deleted to convert file1 into file2.

Examples:

The following examples show how the diff utility operates on two simple files containing seven lines each:

The first file, myfile, contains

  a
  b
  c
  d
  e
  f
  g

and the second file, yourfile, contains:

  w
  a
  b
  x
  y
  z
  e

Default output example

Notice that to change myfile into yourfile, the following could be performed: insert a w before line 1, change lines 3 and 4 from c d to x y z, delete lines 6 and 7, which were f and g, respectively.

Invoking diff myfile yourfile gives the following
Default output:

  1a1
  > w
  3,4c4,6
  < c
  < d
  ---
  > x
  > y
  > z
  6,7d7
  < f
  < g

Edit output example

Executing diff -e myfile yourfile gives the following
Edit output:

  0a
  w
  .
  4,5c
  x
  y
  z
  .
  8,9d

You may notice that the Edit output has different line numbering conventions from the Default output. This is because the script must "track" the changes it makes to the original since the Unix line editor (ed) evaluates the line numbers based upon the current state of the file.

Files:

The input files processed by diff must be ASCII text files.

Exit status:

0
No differences were found.
1
Differences were found.
>1
An error occurred.

Caveats:

The diff utility doesn't always generate the minimum set of differences between two files. It does, however, always generate a set of differences that may be used by a program to derive one file from another.

When you use the -D option, diff simply inserts preprocessor directives without regard for existing directives, which it may contradict.

See also:

cksum, cmp, comm, diff3, wc


[Previous] [Contents] [Next]