diff, sdiff - display file or directory differences


diff [-abBcdefhintwy] [-I regexp] [-u [n]]
	 [-X pat] file1 file2

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

diff [-l] [-r] [-s] [-cefn] [-bhiwt] [-S name] dir1 dir2

sdiff [-abBdHilstvW] [-I regexp] [-w n] file1 file2


The diff(1) utility compares the contents of file1 and file2 and writes to the standard output the list of changes necessary to convert one file into the other. No output is produced if the files are identical.

The sdiff(1) utility, performs a side-by-side comparison similar to diff -y, but supports a different set of options than diff. For information about interpreting the output of sdiff, see the description of the -y option.

Output options

Most of these options are mutually exclusive, although some only modify others:
Display # lines of context. This option is only valid if an unadorned -c or -u option is also given in the command line.
-C n
Display n lines of context. The -C option requires an argument; otherwise, it is identical to -c.
Display n lines of context. For example, -c10 presents 10 lines of context. The default is to present 3 lines of context. With -c, the output format is modified slightly: the output begins with identification of the files involved and their creation dates. Then each change is separated by a line with fifteen asterisks characters (*). The lines removed from file1 are marked with '- '; those added to file2 are marked '+ '. Lines that are changed from one file to the other are marked in both files with '! '. Changes that lie within <context> lines of each other are grouped together on output. (This is a change from the previous "diff -c" but the resulting output is usually much easier to interpret.)

Other options that affect this output format are -F, -L, and -P.

-D string
Create a merged version of file1 and file2 on the standard output, with C preprocessor controls included so that a compilation of the result without defining string is equivalent to compiling file1, while defining string will yield file2. Also known as --ifdef=string.
Produce output in a form suitable as input for the editor utility, ed(1), which can then be used to convert file1 into file2.

Extra commands are added to the output when comparing directories with -e, so that the result is a sh(1) script for converting text files that are common to the two directories from their state in dir1 to their state in dir2.

Like -e, but the output is in reverse order. It cannot be digested by ed(1).
-F regexp
Show as context the most recent line matching the regular expression regexp. Also known as --show-function-line=regexp. Most useful with -c, -C, -u, or -U.
Split the files into chunks for faster processing. This option is accepted but has no effect.
Use an alternate algorithm that can handle very long files. There is a trade off; the algorithm can only deal with changes that are clearly delimited and brief. Long sections of changes and overlaps will confuse it. For sdiff, this is also known as --speed-large-files.
-I regexp
Ignore all changes that match the regular expression regexp. Also known as --ignore-matching-lines=regexp.
-L label
Use label instead of the file name. Most effective with -c, -C, -u, and -U.
-l (sdiff only)
Output only the left column of common lines. This option is only valid for sdiff or diff -y.
Produce a script similar to that of -e, but in the opposite order and with a count of changed lines on each insert or delete command. This is the form used by rcsdiff(1). Also known as --rcsdiff.
Indicate which C-language function the change is in. Also known as --show-c-function. Most effective with -c, -C, -u, or -U.
Display quiet output, indicating only whether files differ. Also known as --brief.
-s (sdiff only)
Do not display common lines. This option can only be used with sdiff or diff -y.
-U n
Show n lines of context, but merge those context lines into a single display rather than copying them. The differing lines are identified by prefixed characters. Requires an argument, but is otherwise identical to -u.
Show n lines of context, but merge those context lines into a single display rather than copying them. The differing lines are identified by prefixed characters. The default is 3 lines of context. Other options affecting this output format are -F, -L, and -P.
-v (sdiff only)
Display version information.
-w n
This option can only be used with sdiff or sdiff -y. The maximum length for each column is n characters per line (default is 130).
Write side-by-side output. With this option, diff(1) is equivalent to sdiff(1). The following characters appear in the gutter between the two file listings to indicate the nature of the differences:

Comparison options

Treat all files as text. Also known as --text.
Ignore trailing blanks (spaces and tabs) and other strings of blanks to compare equal.
Ignore changes whose lines are all blank.
Try to find a smaller set of changes. Also known as --minimal.
Ignore case of letters, so "A" compares equal to "a".
Expand tabs in output lines. Normal or -c output adds character(s) to the front of each line, which can alter the indentation of the original source lines and make the output listing difficult to interpret. This option will preserve the indentation of the original source. Also known as --expand-tabs.
Line up tabs by prepending an initial tab character. Also known as --initial-tab.
-W (sdiff only)
Like -b but does not recognize white space (blanks and tabs). For example, "if ( a == b )" will compare equal to "if(a==b)".

Directory comparison options

Use long output format; each text file to which diff(1) is applied is piped through pr(1) to paginate it, other differences are remembered and summarized after all text file differences are reported. Also known as --paginate.
When comparing directories, treat any absent files as empty. This forces diff(1) to output the contents of a file found in only one of the directories. Also known as --newfile.
Like -N, but treats only an absent first file as empty. This means that diff(1) outputs the contents of a unique file only if it is in the second directory on the command line. Also known as --unidirectional-new-file.
Apply diff(1) recursively to common subdirectories encountered. Also known as --recursive.
Report files which are the same; otherwise, these are not mentioned. Also known as --report-identical-files. This option has a different meaning when used with sdiff.
-S name
Restart a directory diff(1) in the middle beginning with file name. Also known as --startingfile=file.
-x filepat
When comparing directories, exclude any files that match the pattern specified as filepat. Be sure to escape filepat so that it is not interpreted by the shell. Note that unlike shell wildcards, wildcards at the beginning of -filepat will match files whose names start with a dot. Also known as --exclude-from=filepat.
-X fileofpat
Like -x, except that the file patterns to be excluded from the directory comparison are stored in a file, -fileofpat. Enter one pattern per line.

If both arguments are directories, diff(1) sorts the contents of the directories by name, and then runs the regular file diff(1) algorithm, producing a change list on text files that are different. Binary files that differ, common subdirectories, and files that appear in only one directory are described as such.

If only one of file1 and file2 is a directory, diff(1) is applied to the non-directory file and the file contained in the directory file with a filename that is the same as the last component of the non-directory file.

If either file1 or file2 is -, the standard input is used in its place.


The default (without -e, -c, or -n options) output contains lines of these forms, where XX YY ZZ and QQ are line numbers respective of file order.
At (the end of) line XX of file1, append the contents of line YY of file2 to make them equal.
Same as above, but append the range of lines, YY through ZZ of file2 to line XX of file1.
At line XX delete the line. The value YY tells to which line the change would bring file1 in line with file1.
Delete the range of lines XX through YY in file1.
Change the line XX in file1 to the line YY in file2.
Replace the range of specified lines with the line ZZ.
Replace the range XX from file1 with the range ZZ from file2.

These lines resemble ed(1) subcommands to convert file1 into file2. The line numbers before the action letters pertain to file1; those after pertain to file2. Thus, by exchanging a for d and reading the line in reverse order, one can also determine how to convert file2 into file1. As in ed(1), identical pairs (where num1 = num2) are abbreviated as a single number.


The diff(1) utility uses the following environment variable, if set:
If the environment variable TMPDIR exists, diff(1) will use the directory specified by TMPDIR as the temporary directory.


The diff(1) utility uses the following files:
Temporary files.
Used for directory diffs.
Used by the -l option.


The diff(1) utility exits with one of the following values:
No differences were found.
Differences were found.
An error occurred.


The -f and -e options do not provide special handling for lines on which the first and only character is . This can cause problems for ed(1).

When comparing directories with the -b, -w, or -i options specified, diff(1), first compares the files in the manner of cmp(1), and then decides to run the diff(1) algorithm if they are not equal. This may cause a small amount of spurious output if the files then turn out to be identical because the only differences are insignificant white space or case differences.