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 only works 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 when comparing text files that differ during directory comparison, the diff command tells what lines must be changed in the files to make them agree. If neither the File1 nor File2 parameter is a directory, then either may 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; those after 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 affected in the first file preceded by a <: (less than sign, colon), then displays all lines affected in the second file are preceded by a > (greater than sign).

An exit value of 0 indicates no differences, 1 indicates differences found, and 2 indicates an error.

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

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 preceding the newline character are ignored) and other strings of white-space characters, not including newline characters, to compare 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 involved and their creation dates. Each change is separated by a line with a dozen * (asterisks). The lines removed from File1 are marked with a - (minus sign ) and those 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 together 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 involved and their creation dates. Each change is separated by a line with a dozen * (asterisks). The lines removed from File1 are marked with a - (minus sign ) and those 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 together 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 using this flag, the following shell program may 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 latest version appears on the standard output as follows:
(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 produced by the -e or -f flags cannot create lines consisting 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 may be faster if the changed sections are short and well 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 diff command text file comparison 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 is the form 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 otherwise not mentioned.
-S [ File ] Ignores files whose names collate before the file specified by the File variable when comparing directories. The -S flag only applies 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 may 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, and added lines are shown together, interleaved.

-U Lines Produces a diff command comparison with a number of 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, and added lines are shown together, interleaved.
-w Ignores all spaces and tab characters and treats all other strings of blanks as equivalent. For example, if ( a == b ) compares equally to if(a==b).

Exit Status

This command returns the following exit values:

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

Examples

  1. To compare two files, enter:
    diff chap1.back chap1
    This displays the differences between the files chap1.bak and chap1.
  2. To compare two files while ignoring differences in the amount of white space, enter:
    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 containing commands that the ed command can use to reconstruct one file from another, enter:
    diff  -e chap2 chap2.old >new.to.old.ed
    This creates a file named new.to.old.ed that contains the ed subcommands to change chap2 back into the version of the text found in chap2.old. In most cases, new.to.old.ed is a much smaller file than chap2.old. You can save disk space by deleting chap2.old, and you can reconstruct it at any time by entering:
    (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 sent to the ed editor. The 1,$p causes the ed command to write the file to standard output after editing it. 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 because it would be mixed with the text of chap2.old.

Files

Item Description
/usr/bin/diff Contains the diff command.