Previous Table of Contents Next

Module 33


The external diff command compares two text files for differences. It determines which lines must be changed to make the two files identical. The diff command scans the two files and indicates editing changes that must be made to the first file to make it identical to the second file. The changes can be saved for use as an ed command script to change the first file. It can also compare directories. System V provides a second command to compare directories; refer to the dircmp command.


Following is the general format of the diff command.

     diff [ -lrs ] [ -S name ] [ -cefhn ] [ -biwt ] dir1 dir2

     diff [ -cefhn ] [ -biwt ] file1 file2
     diff [ -cefhn ] [ -biwt ] - file2
     diff [ -cefhn ] [ -biwt ] file1 -

     diff [ -D string ] [ -biw ] file1 file2
     diff [ -D string ] [ -biw ] - file2
     diff [ -D string ] [ -biw ] file1 -

     diff [ -C string ] [ -biw ] file1 file2
     diff [ -C string ] [ -biw ] - file2
     diff [ -C string ] [ -biw ] file1 -

You may want to think of file1 as the old file and file2 as the new file.


The following list describes the options and their arguments that may be used to control how diff functions.

Comparison control options:

-b Causes blanks (spaces and tabs) to compare equally even if an unequal number of blanks exist. All trailing blanks are ignored. For example, if the first line was in file1 and the second line was in file2, they would compare as equals.
                    file1:    A     sample line of text here
                    file2:    A     sample line of text here
-i Causes the case of letters to be ignored. For example,
                     THE BIG dog ran fast.
                     The big dog ran fast.
                    match as equal lines.
-t Expands tabs on input to spaces on output. Normal output adds additional characters to the front of each line. This may change the indention of the original text, making it difficult to read. This option preserves the original text indention. The -c option also adds additional characters, causing indention problems.
-w Causes all white spaces (blanks and tabs) to be ignored. For example,
                      if ( x == y )
                    compare as equals.

Directory comparison options:

-l Display long output listing. The ouput is piped through pr for pagination. Other differences are saved and summarized after all text file differences are displayed.
-r Recursively descends through subdirectories.
-s Display files that are the same. Normally, identical files are not displayed.
-S name Begin the directory comparison with file name. Normally, all files in the directory are compared.

Mutually exclusive options:

-Dstring Creates a merged version of file1 and file2 on the standard ouput. C preprocessor controls are included in the output. If the ifdef string is not defined then a compile (cc) on the output would yield the same program as a compile on file1. If the ifdef string is defined then a compile would be the same as a compile on file2.
-c[n] Displays a comparison with n lines of context. The default for n is 3. The output begins with the identification and creation dates of each file. Each change encountered is separated with a dozen asterisks (*). Lines removed from file1 are preceded by a hyphen (-). Lines removed from file2 are preceded by a plus (+). Lines changed from file1 to file2 and vice versa are preceded with an exclamation mark (!). System V does not support the [n]. See the -C[n] option for variable context sizes.
-C[n] System V only. Same as the -c[n] option on BSD.
-e Produces an ed script consisting of the a (append), c (change), and d (delete) commands. These commands can be used as input to ed to change file1 to match file2. See the following section on Version Control.
-f Produces a script similar to that produced by -e, but the order is for file1 from file2. These commands are not usable with ed.
-h Does not attempt to find the most efficient way to edit the changes. It is fast, but not thorough. The changes must be short and well separated. It does work on files of unlimited size. The -e and -f options are disabled if -h is specified.
-n Produces a script similar to the -e option. The order is reversed. Each insert or delete command contains a count of changed lines. This format is used by rcsdiff.


The following list describes the arguments that may be passed to the diff command.

file1 The first input file used in the comparison. If file1 is a directory name, the file2 file in directory file1 is used for comparison. For example, if you specify,
                    diff adir afile
diff uses adir/afile afile for the two files.
file2 The second input file used in the comparison. If file2 is a directory, the second file is set to file2/file1.
- A hyphen may be used in place of either file1 or file2 to represent the standard input. This allows you to pipe input to diff redirect input from a file, or type input from your keyboard for comparison.
dir1 The first directory containing files used for comparison.
dir2 The second directory containing files used for comparison.

Previous Table of Contents Next