diff Command

Purpose

Compares text files.

Syntax

To compare the contents of two files
diff [ -c| -C Lines | -D [ String ] | -e | -f | -n  | -u | -U Lines ] [ -b ] [ -i ] [ -t ] [ -w ] File1 File2
diff [ -h ] [ -b ] File1 File2
To sort the contents of directories and compare files that are different
diff [ -c | -C Lines | -e | -f | -n  | -u | -U Lines ] [ -b ] [ -i ] [ -l ] [ -r ] [ -s ] [ -S File ] [ -t ] [ -w ] Directory1 Directory2
diff [ -h ] [ -b ] Directory1 Directory2

Description

The diff command compares text files. It can compare single files or the contents of directories.
Note: The diff command works only with input files that are text files.

If the Directory1 and Directory2 parameters are specified, the diff command compares the text files that have the same name in both directories. Binary files that differ, common subdirectories, and files that appear in only one directory are listed.

When the diff command is run on regular files, and the text files that are compared differ during directory comparison, the diff command tells what lines must be changed in the files to make them agree. If the File1 and File2 parameters are not a directory, then either can be given as - (minus sign), in which case the standard input is used. If the File1 parameter is a directory, then a file in that directory whose file name is the same as the File2 parameter is used.

The typical output contains lines of these forms:

Lines Affected in File1   Action        Lines Affected in File2
Number1                   a             Number2[,Number3]
Number1[,Number2]         d             Number3
Number1[,Number2]         c             Number3[,Number4] 

These lines resemble ed subcommands to convert File1 into File2. The numbers before the action letters pertain to File1 and numbers after the action letters pertain to File2. Thus, by exchanging a for d and reading from right to left, you can also tell how to convert File2 into File1. As in the ed command, identical pairs (where Number1 = Number2) are abbreviated as a single number.

Following each of these lines, the diff command displays all lines that are affected in the first file that is preceded by a <: (less than sign, colon), then displays all lines that are affected in the second file are preceded by a > (greater than sign).

Note: If more than one of the -c, -C, -D, -e, -f, or -n, -u, or -U flags are specified, the last one on the command line takes precedence. The system does not issue an error message.

Flags

Table 1. Flags
Item Description
-b Causes any amount of white space at the end of a line to be treated as a single newline character. The white-space characters that precede the newline character are ignored. Other strings of white-space characters, not including newline characters, are compared equally.
-C Lines Produces a diff command comparison with a number of lines of copied context equal to the value specified by the Lines variable. The -C flag modifies the output slightly. The output begins with identification of the files that are involved and their creation dates. Each change is separated by a line with a dozen * (asterisks). The lines that are removed from File1 are marked with a - (minus sign) and the lines that are added to File2 are marked with a + (plus sign). Lines changed from one file to the other are marked in both files with an ! (exclamation point). Changes that lie within the specified copied context lines of each other are grouped as output.
-c Produces a diff command comparison with three lines of copied context. The -c flag modifies the output slightly. The output begins with identification of the files that are involved and their creation dates. Each change is separated by a line with a dozen * (asterisks). The lines that are removed from File1 are marked with a - (minus sign) and lines that are added to File2 are marked with a + (plus sign). Lines changed from one file to the other are marked in both files with an ! (exclamation point). Changes within the specified copied context lines of each other are grouped as output.
-D [ String ] Causes the diff command to create a merged version of File1 and File2 on the standard output. The C preprocessor controls are included so that a compilation of the result without defining String is equivalent to compiling File1, while defining String yields File2.
-e Produces output in a form suitable for use with the ed editor to convert File1 to File2. When you use this flag, the following shell program might 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 the diff command need to be on hand. The following output is displayed to show the latest version:
(shift; cat $*; echo '1,$p') | ed - $1
Extra commands are added to the output when the -e flag is used to compare directories, so the result is a shell script for converting text files that are common to the two directories from their state in Directory1 to their state in Directory2.
Note: Editing scripts that are produced by the -e or -f flags cannot create lines that consist of a single period.
-f Produces output in a form not suitable for use with the ed editor, showing the modifications necessary to convert File1 to File2 in the reverse order of that produced under the -e flag.
-h Performs an alternate comparison that might be faster if the changed sections are short and properly separated. The -h flag works on files of any length. The -c, -C, -D, -e, -f, and -n flags cannot be used with the -h flag. All other flags except the -b flag are ignored when used with the -h flag.
-i Ignores the case of letters. For example, a lowercase a is treated the same as an uppercase A.
-l Long output format. Each result from the text file comparison of the diff command is piped through the pr command for pagination. Other differences are remembered and summarized after all text file differences are reported.
-n Produces output similar to that of the -e flag, but in the opposite order and with a count of changed lines on each insert or delete command. This form is used by the revision control system (RCS).
-r Causes application of the diff command recursively to common subdirectories encountered.
-s Reports files that are the same and are otherwise not mentioned.
-S [ File ] Ignores files whose names collate before the file specified by the File variable when you compare directories. The -S flag applies only to the directories specified in the Directory1 and Directory2 parameters. If you use the -r flag with the -S flag, the -S flag does not work recursively in the Directory1 and Directory2 subdirectories.
-t Expands tabs in output lines. Typical output or the -c flag output adds characters to the front of each line, which might affect indentation of the original source lines and makes the output listing difficult to interpret. This flag preserves the original source's indentation.
-u Produces a diff command comparison with three lines of unified context.

The output is similar to that of the -c flag, except that the context lines are not repeated. Instead, the context, deleted lines, and added lines are shown together, interleaved.

-U Lines Produces a diff command comparison with few lines of unified context equal to the value specified by the Lines variable. The output is similar to that of the -C flag, except that the context lines are not repeated. Instead, the context, deleted lines, and added lines are shown together, interleaved.
-w All the spaces and tab characters are ignored. For example, if ( a == b ) compares equally to if(a==b).

Exit status

This command returns the following exit values:

Table 2. Exit status
Item Description
0 No differences were found.
1 Differences were found.
>1 An error occurred.

Examples

  1. To compare the two files, enter the following command:
    diff chap1.back chap1

    This command displays the differences between the files chap1.bak and chap1.

  2. To compare two files while it ignores differences in the amount of white space, enter the following command:
    diff  -w prog.c.bak prog.c

    If two lines differ only in the number of spaces and tabs between words, the diff -w command considers them to be the same.

  3. To create a file that contains commands that the ed command can use to reconstruct one file from another, enter the following command:
    diff  -e chap2 chap2.old >new.to.old.ed
    This command creates a file that is named new.to.old.ed that contains the ed subcommands to change chap2 back into the version of the text found in chap2.old. Usually, new.to.old.ed is a smaller file than chap2.old. You can save disk space by deleting chap2.old, and you can reconstruct it at any time by using the following command:
    (cat new.to.old.ed ; echo '1,$p') | ed - chap2 >chap2.old

    The commands in parentheses add 1,$p to the end of the editing commands that are sent to the ed editor. The 1,$p causes the ed command to write the file to standard output after it is edited. This modified command sequence is then piped to the ed command (| ed), and the editor reads it as standard input. The - flag causes the ed command not to display the file size and other extra information as the output is mixed with the text of chap2.old.

Files

Table 3. Files
Item Description
/usr/bin/diff Contains the diff command.