Report the difference between two files (POSIX)
diff [-C n|-c|-e|-D string|-s|-n] [-L label1 [label2]] [-br] file1 file2
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:
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.
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.
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 |
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:
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.
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.
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
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
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.
The input files processed by diff must be ASCII text files.
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.