diff

Display the differences between two files, or each corresponding file in two directories. `diff' outputs differences between files line by line in any of several formats, selectable by command line options. This set of differences is often called a "diff" or "patch". For files that are identical, `diff' normally produces no output; for binary (non-text) files, `diff' normally reports only that they are different.

SYNTAX
      diff OPTIONS... FROM-FILE TO-FILE 

OPTIONS
      Multiple single letter options (unless they take an argument)
      can be combined into a single command line word: 
      so `-ac' is equivalent to `-a -c'. 

`-LINES'
     Show LINES (an integer) lines of context.This option is obsolete.

`-a'
     Treat all files as text and compare them line-by-line, even if they
     do not seem to be text.

`-b'
     Ignore changes in amount of white space.

`-B'
     Ignore changes that just insert or delete blank lines.

`--binary'
     Read and write data in binary mode.

`--brief'
     Report only whether the files differ, not the details of the
     differences.  *Note Brief::.

`-c'
     Use the context output format.

`-C LINES'
`--context[=LINES]'
     Use the context output format, showing LINES (an integer) lines of
     context, or three if LINES is not given.  *Note Context Format::.
     For proper operation, `patch' typically needs at least two lines of
     context.

`--changed-group-format=FORMAT'
     Use FORMAT to output a line group containing differing lines from
     both files in if-then-else format.  *Note Line Group Formats::.

`-d'
     Change the algorithm perhaps find a smaller set of changes.  This
     makes `diff' slower (sometimes much slower).  *Note diff
     Performance::.

`-D NAME'
     Make merged `#ifdef' format output, conditional on the preprocessor
     macro NAME.  *Note If-then-else::.

`-e'
`--ed'
     Make output that is a valid `ed' script.

`--exclude=PATTERN'
     When comparing directories, ignore files and subdirectories whose
     basenames match PATTERN.  *Note Comparing Directories::.

`--exclude-from=FILE'
     When comparing directories, ignore files and subdirectories whose
     basenames match any pattern contained in FILE.  *Note Comparing
     Directories::.

`--expand-tabs'
     Expand tabs to spaces in the output, to preserve the alignment of
     tabs in the input files.  *Note Tabs::.

`-f'
     Make output that looks vaguely like an `ed' script but has changes
     in the order they appear in the file.  *Note Forward ed::.

`-F REGEXP'
     In context and unified format, for each hunk of differences, show
     some of the last preceding line that matches REGEXP.

`--forward-ed'
     Make output that looks vaguely like an `ed' script but has changes
     in the order they appear in the file.

`-h'
     This option currently has no effect; it is present for Unix
     compatibility.

`-H'
     Use heuristics to speed handling of large files that have numerous
     scattered small changes.  *Note diff Performance::.

`--horizon-lines=LINES'
     Do not discard the last LINES lines of the common prefix and the
     first LINES lines of the common suffix.  *Note diff Performance::.

`-i'
     Ignore changes in case; consider upper- and lower-case letters
     equivalent.  *Note Case Folding::.

`-I REGEXP'
     Ignore changes that just insert or delete lines that match REGEXP.
     *Note Specified Folding::.

`--ifdef=NAME'
     Make merged if-then-else output using NAME.  *Note If-then-else::.

`--ignore-all-space'
     Ignore white space when comparing lines.  *Note White Space::.

`--ignore-blank-lines'
     Ignore changes that just insert or delete blank lines.  *Note
     Blank Lines::.

`--ignore-case'
     Ignore changes in case; consider upper- and lower-case to be the
     same.  *Note Case Folding::.

`--ignore-matching-lines=REGEXP'
     Ignore changes that just insert or delete lines that match REGEXP.
     *Note Specified Folding::.

`--ignore-space-change'
     Ignore changes in amount of white space.  *Note White Space::.

`--initial-tab'
     Output a tab rather than a space before the text of a line in
     normal or context format.  This causes the alignment of tabs in
     the line to look normal.  *Note Tabs::.

`-l'
     Pass the output through `pr' to paginate it.  *Note Pagination::.

`-L LABEL'
     Use LABEL instead of the file name in the context format (*note
     Context Format::.) and unified format (*note Unified Format::.)
     headers.  *Note RCS::.

`--label=LABEL'
     Use LABEL instead of the file name in the context format (*note
     Context Format::.) and unified format (*note Unified Format::.)
     headers.

`--left-column'
     Print only the left column of two common lines in side by side
     format.  *Note Side by Side Format::.

`--line-format=FORMAT'
     Use FORMAT to output all input lines in if-then-else format.
     *Note Line Formats::.

`--minimal'
     Change the algorithm to perhaps find a smaller set of changes.
     This makes `diff' slower (sometimes much slower).  *Note diff
     Performance::.

`-n'
     Output RCS-format diffs; like `-f' except that each command
     specifies the number of lines affected.  *Note RCS::.

`-N'
`--new-file'
     In directory comparison, if a file is found in only one directory,
     treat it as present but empty in the other directory.  *Note
     Comparing Directories::.

`--new-group-format=FORMAT'
     Use FORMAT to output a group of lines taken from just the second
     file in if-then-else format.  *Note Line Group Formats::.

`--new-line-format=FORMAT'
     Use FORMAT to output a line taken from just the second file in
     if-then-else format.  *Note Line Formats::.

`--old-group-format=FORMAT'
     Use FORMAT to output a group of lines taken from just the first
     file in if-then-else format.  *Note Line Group Formats::.

`--old-line-format=FORMAT'
     Use FORMAT to output a line taken from just the first file in
     if-then-else format.  *Note Line Formats::.

`-p'
     Show which C function each change is in.  *Note C Function
     Headings::.

`-P'
     When comparing directories, if a file appears only in the second
     directory of the two, treat it as present but empty in the other.
     *Note Comparing Directories::.

`--paginate'
     Pass the output through `pr' to paginate it.  *Note Pagination::.

`-q'
     Report only whether the files differ, not the details of the
     differences.  *Note Brief::.

`-r'
     When comparing directories, recursively compare any subdirectories
     found.  *Note Comparing Directories::.

`--rcs'
     Output RCS-format diffs; like `-f' except that each command
     specifies the number of lines affected.  *Note RCS::.

`--recursive'
     When comparing directories, recursively compare any subdirectories
     found.  *Note Comparing Directories::.

`--report-identical-files'
     Report when two files are the same.  *Note Comparing Directories::.

`-s'
     Report when two files are the same.  *Note Comparing Directories::.

`-S FILE'
     When comparing directories, start with the file FILE.  This is
     used for resuming an aborted comparison.  *Note Comparing
     Directories::.

`--sdiff-merge-assist'
     Print extra information to help `sdiff'.  `sdiff' uses this option
     when it runs `diff'.  This option is not intended for users to use
     directly.

`--show-c-function'
     Show which C function each change is in.  *Note C Function
     Headings::.

`--show-function-line=REGEXP'
     In context and unified format, for each hunk of differences, show
     some of the last preceding line that matches REGEXP.  *Note
     Specified Headings::.

`--side-by-side'
     Use the side by side output format.  *Note Side by Side Format::.

`--speed-large-files'
     Use heuristics to speed handling of large files that have numerous
     scattered small changes.  *Note diff Performance::.

`--starting-file=FILE'
     When comparing directories, start with the file FILE.  This is
     used for resuming an aborted comparison.  *Note Comparing
     Directories::.

`--suppress-common-lines'
     Do not print common lines in side by side format.  *Note Side by
     Side Format::.

`-t'
     Expand tabs to spaces in the output, to preserve the alignment of
     tabs in the input files.  *Note Tabs::.

`-T'
     Output a tab rather than a space before the text of a line in
     normal or context format.  This causes the alignment of tabs in
     the line to look normal.  *Note Tabs::.

`--text'
     Treat all files as text and compare them line-by-line, even if they
     do not appear to be text.  *Note Binary::.

`-u'
     Use the unified output format.  *Note Unified Format::.

`--unchanged-group-format=FORMAT'
     Use FORMAT to output a group of common lines taken from both files
     in if-then-else format.  *Note Line Group Formats::.

`--unchanged-line-format=FORMAT'
     Use FORMAT to output a line common to both files in if-then-else
     format.  *Note Line Formats::.

`--unidirectional-new-file'
     When comparing directories, if a file appears only in the second
     directory of the two, treat it as present but empty in the other.
     *Note Comparing Directories::.

`-U LINES'
`--unified[=LINES]'
     Use the unified output format, showing LINES (an integer) lines of
     context, or three if LINES is not given.  *Note Unified Format::.
     For proper operation, `patch' typically needs at least two lines of
     context.

`-v'
`--version'
     Output the version number of `diff'.

`-w'
     Ignore white space when comparing lines.

`-W COLUMNS'
`--width=COLUMNS'
     Use an output width of COLUMNS in side by side format.

`-x PATTERN'
     When comparing directories, ignore files and subdirectories whose
     basenames match PATTERN.  *Note Comparing Directories::.

`-X FILE'
     When comparing directories, ignore files and subdirectories whose
     basenames match any pattern contained in FILE.  *Note Comparing
     Directories::.

`-y'
     Use the side by side output format.

GNU `diff' can show whether files are different without detailing the differences.
It also provides ways to suppress certain kinds of differences that are not important to you.

Most commonly, such differences are changes in the amount of white space between words or lines. `diff' also provides ways to suppress differences in alphabetic case or in lines that match a regular expression that you provide.

These options can accumulate; for example, you can ignore changes in both white space and alphabetic case.

End -of-Line markers

In operating systems that distinguish between text and binary files, `diff' normally reads and writes all data as text.

Use the `--binary' option to force `diff' to read and write binary data instead. This option has no effect on a Posix-compliant system like GNU or traditional Unix. However, many personal computer operating systems represent the end of a line with a carriage return followed by a newline.

On such systems, `diff' normally ignores these carriage returns on input and generates them at the end of each output line, but with the `--binary' option `diff' treats each carriage return as just another input character, and does not generate a carriage return at the end of each output line.

This can be useful when dealing with non-text files that are meant to be interchanged with Posix-compliant systems.

Suppressing Differences in Blank and Tab Spacing

The `-b' and `--ignore-space-change' options ignore white space at line end, and considers all other sequences of one or more white space characters to be equivalent.
With these options, `diff' considers the following two lines to be equivalent, where `$' denotes the line end:

Here lyeth muche rychnesse in lytell space. -- John Heywood$
Here lyeth muche rychnesse in lytell space. -- John Heywood $

The `-w' and `--ignore-all-space' options are stronger than `-b'. They ignore difference even if one file has white space where the other file has none. "White space" characters include tab, newline, vertical tab, form feed, carriage return, and space; some locales may define additional characters to be white space.
With these options, `diff' considers the following two lines to be equivalent, where `$' denotes the line end and `^M' denotes a carriage return:

Here lyeth muche rychnesse in lytell space.-- John Heywood$
He relyeth much erychnes seinly tells pace. --John Heywood

GNU `diff' can treat lowercase letters as equivalent to their uppercase counterparts, so that, for example, it considers `Funky Stuff', `funky STUFF', and `fUNKy stuFf' to all be the same.
To request this, use the `-i' or `--ignore-case' option.

Suppressing Lines Matching a Regular Expression
To ignore insertions and deletions of lines that match a regular expression, use the `-I REGEXP' or `--ignore-matching-lines=REGEXP' option.
You should escape regular expressions that contain shell metacharacters to prevent the shell from expanding them.

For example, `diff -I '^[0-9]'' ignores all changes to lines beginning with a digit.

However, `-I' only ignores the insertion or deletion of lines that contain the regular expression if every changed line in the hunk--every insertion and every deletion--matches the regular expression.

In other words, for each nonignorable change, `diff' prints the complete set of changes in its vicinity, including the ignorable ones. You can specify more than one regular expression for lines to ignore by using more than one `-I' option. `diff' tries to match each line against each regular expression, starting with the last one given.

Summarizing Which Files Differ
When you only want to find out whether files are different, and you don't care what the differences are, you can use the summary output format.
In this format, instead of showing the differences between the files, `diff' simply reports whether files differ.
The `-q' and `--brief' options select this output format.
This format is especially useful when comparing the contents of two directories. It is also much faster than doing the normal line by line comparisons, because `diff' can stop analyzing the files as soon as it knows that there are any differences.
You can also get a brief indication of whether two files differ by using `cmp'.

Using diff to patch a file
To show context around the differing linesGNU `diff' provides these output formats

Normal Format:An output format that shows each hunk of differences without any surrounding context
Context Format:: An output format that shows surrounding lines.
Unified Format:: A more compact output format that shows context.

`patch' can apply diffs by searching in the files for the lines of context around the differing lines; if those lines are actually a few lines away from where the diff says they are, `patch' can adjust the line numbers accordingly and still apply the diff correctly.

For more on patching files and producing commands that direct the `ed' text editor to edit a file - see `info diff'

# Oh lord won't you buy me a Mercedes Benz, my friends all drive Porsches, I must make amends # - Janice Joplin

Related commands:

cmp - Compare two files
dircmp - Compare 2 directories
diff3 - Show differences among three files
sdiff - Merge two files interactively

Equivalent Windows NT commands:

COMP - Compare two files and display any characters which do NOT match
FC - Compare two files and display any LINES which do not match
FIND - Search for a text string in a file
FINDSTR - Search for strings in files
MUNGE - Find and Replace text within file(s)
WINDIFF - GUI, Win 2K Resource Kit


Back to the Top

Simon Sheppard
SS64.com