diff Command Purpose Compares text files. Syntax To Compare the Contents of Two Files diff [ -c [ Lines ] | -C Lines | -D [ String ] | -e | -f | -h | -n ] [ -b ] [ -i ] [ -t ] [ -w ] File1 File2 To Sort the Contents of Directories and Compares Files That Are Different diff [ -b ] [ -l ] [ -r ] [ -s ] [ -i ] [ -t ] [ -w ] [ -c [ Lines ] | -C Lines | -e | -f | -h | -n ] [ -S File ] 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 subdirec- tories, 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 com- mand 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 normal 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 backward, 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 differ- ences found, and 2 indicates an error. Flags -b Ignores leading spaces and tab characters and considers other strings of spaces to compare as equal. -C Lines Produces a diff command comparison with a number of lines of context equal to the value specified by the Lines vari- able. The -C flag modifies the output slightly. The output be- gins 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 - (dash ) 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 context lines of each other are grouped together as output. Note: The -C and -c flags are not equivalent. The Lines variable is required with the -C flag, but it is optional with the -c flag. -c [ Lines ] Produces a diff command comparison with a number of lines of context equal to the value specified by the Lines vari- able. The default is three lines of context. The -c flag modi- fies 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 re- moved from File1 are marked with a - (dash ) 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 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 fol- lows: (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 con- verting text files which 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 which may be faster if the changed sections are short and well separated. It works on files of any length. The -c, -C, -D, -e, -f, and -n flags can not 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. -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 op- posite 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. Normal or the -c flag output adds character(s) to the front of each line, which may affect in- dentation of the original source lines and makes the output list- ing difficult to interpret. This flag preserves the original source's indentation. -w Ignores all spaces and tab characters. File1, File 2 Specifies path names of the files to be compared. If a - (minus sign) is specified, files are read from standard input. If both the File1 and File2 variables are directories, the files within the directory are compared, except for block special files, character special files, or first-in-first-out (FIFO) special files. If one variable specifies a file and the other a directory, the diff command compares the file with that file in the directory that has the same name. Exit Status This command returns the following exit values: 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 sub- commands 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 informa- tion since it would be mixed with the text of chap2.old. Implementation Specifics This command is part of Base Operating System (BOS) Runtime. Files /usr/bin/diff Contains the diff command. Related Information Files Overview in AIX Version 3.2 System User's Guide: Base and Devices introduces you to files and the way you can work with them. Input and Output Redirection Overview in AIX Version 3.2 System User's Guide: Base and Devices describes how the operating system processes input and output. The bdiff command, cmp command, ed command, diff3 command, pr command.