Comparing and Merging Files - 12. Invoking diff

Go to the first, previous, next, last section, table of contents.

# 12. Invoking `diff`

The format for running the `diff` command is:

    diff options... from-file to-file

In the simplest case, `diff` compares the contents of the two files from-file and to-file. A file name of ``-'` stands for text read from the standard input. As a special case, ``diff - -'` compares a copy of standard input to itself.

If from-file is a directory and to-file is not, `diff` compares the file in from-file whose file name is that of to-file, and vice versa. The non-directory file must not be ``-'`.

If both from-file and to-file are directories, `diff` compares corresponding files in both directories, in alphabetical order; this comparison is not recursive unless the ``-r'` or ``--recursive'` option is given. `diff` never compares the actual contents of a directory as if it were a file. The file that is fully specified may not be standard input, because standard input is nameless and the notion of "file with the same name" does not apply.

`diff` options begin with ``-'`, so normally from-file and to-file may not begin with ``-'`. However, ``--'` as an argument by itself treats the remaining arguments as file names even if they begin with ``-'`.

An exit status of 0 means no differences were found, 1 means some differences were found, and 2 means trouble.

## 12.1 Options to `diff`

Below is a summary of all of the options that GNU `diff` accepts. Most options have two equivalent names, one of which is a single letter preceded by ``-'`, and the other of which is a long name preceded by ``--'`. Multiple single letter options (unless they take an argument) can be combined into a single command line word: ``-ac'` is equivalent to ``-a -c'`. Long named options can be abbreviated to any unique prefix of their name. Brackets ([ and ]) indicate that an option takes an optional argument.

``-lines'`
  Show lines (an integer) lines of context. This option does not specify an output format by itself; it has no effect unless it is combined with ``-c'` (see section 2.3.1 Context Format) or ``-u'` (see section 2.3.2 Unified Format). This option is obsolete. For proper operation, `patch` typically needs at least two lines of context.
``-a'`
  Treat all files as text and compare them line-by-line, even if they do not seem to be text. See section 1.7 Binary Files and Forcing Text Comparisons.
``-b'`
  Ignore changes in amount of white space. See section 1.2 Suppressing Differences in Blank and Tab Spacing.
``-B'`
  Ignore changes that just insert or delete blank lines. See section 1.3 Suppressing Differences in Blank Lines.
``--binary'`
  Read and write data in binary mode. See section 1.7 Binary Files and Forcing Text Comparisons.
``--brief'`
  Report only whether the files differ, not the details of the differences. See section 1.6 Summarizing Which Files Differ.
``-c'`
  Use the context output format. See section 2.3.1 Context Format.
``-C lines'`
``--context[=lines]'`
  Use the context output format, showing lines (an integer) lines of context, or three if lines is not given. See section 2.3.1 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. See section 2.7.1 Line Group Formats.
``-d'`
  Change the algorithm perhaps find a smaller set of changes. This makes `diff` slower (sometimes much slower). See section 5. `diff` Performance Tradeoffs.
``-D name'`
  Make merged ``#ifdef'` format output, conditional on the preprocessor macro name. See section 2.7 Merging Files with If-then-else.
``-e'`
``--ed'`
  Make output that is a valid `ed` script. See section 2.6.1 `ed` Scripts.
``--exclude=pattern'`
  When comparing directories, ignore files and subdirectories whose basenames match pattern. See section 3. Comparing Directories.
``--exclude-from=file'`
  When comparing directories, ignore files and subdirectories whose basenames match any pattern contained in file. See section 3. Comparing Directories.
``--expand-tabs'`
  Expand tabs to spaces in the output, to preserve the alignment of tabs in the input files. See section 4.1 Preserving Tabstop Alignment.
``-f'`
  Make output that looks vaguely like an `ed` script but has changes in the order they appear in the file. See section 2.6.2 Forward `ed` Scripts.
``-F regexp'`
  In context and unified format, for each hunk of differences, show some of the last preceding line that matches regexp. See section 2.3.3.1 Showing Lines That Match Regular Expressions.
``--forward-ed'`
  Make output that looks vaguely like an `ed` script but has changes in the order they appear in the file. See section 2.6.2 Forward `ed` Scripts.
``-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. See section 5. `diff` Performance Tradeoffs.
``--horizon-lines=lines'`
  Do not discard the last lines lines of the common prefix and the first lines lines of the common suffix. See section 5. `diff` Performance Tradeoffs.
``-i'`
  Ignore changes in case; consider upper- and lower-case letters equivalent. See section 1.4 Suppressing Case Differences.
``-I regexp'`
  Ignore changes that just insert or delete lines that match regexp. See section 1.5 Suppressing Lines Matching a Regular Expression.
``--ifdef=name'`
  Make merged if-then-else output using name. See section 2.7 Merging Files with If-then-else.
``--ignore-all-space'`
  Ignore white space when comparing lines. See section 1.2 Suppressing Differences in Blank and Tab Spacing.
``--ignore-blank-lines'`
  Ignore changes that just insert or delete blank lines. See section 1.3 Suppressing Differences in Blank Lines.
``--ignore-case'`
  Ignore changes in case; consider upper- and lower-case to be the same. See section 1.4 Suppressing Case Differences.
``--ignore-matching-lines=regexp'`
  Ignore changes that just insert or delete lines that match regexp. See section 1.5 Suppressing Lines Matching a Regular Expression.
``--ignore-space-change'`
  Ignore changes in amount of white space. See section 1.2 Suppressing Differences in Blank and Tab Spacing.
``--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. See section 4.1 Preserving Tabstop Alignment.
``-l'`
  Pass the output through `pr` to paginate it. See section 4.2 Paginating `diff` Output.
``-L label'`
  Use label instead of the file name in the context format (see section 2.3.1 Context Format) and unified format (see section 2.3.2 Unified Format) headers. See section 2.6.3 RCS Scripts.
``--label=label'`
  Use label instead of the file name in the context format (see section 2.3.1 Context Format) and unified format (see section 2.3.2 Unified Format) headers.
``--left-column'`
  Print only the left column of two common lines in side by side format. See section 2.5 Controlling Side by Side Format.
``--line-format=format'`
  Use format to output all input lines in if-then-else format. See section 2.7.2 Line Formats.
``--minimal'`
  Change the algorithm to perhaps find a smaller set of changes. This makes `diff` slower (sometimes much slower). See section 5. `diff` Performance Tradeoffs.
``-n'`
  Output RCS-format diffs; like ``-f'` except that each command specifies the number of lines affected. See section 2.6.3 RCS Scripts.
``-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. See section 3. 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. See section 2.7.1 Line Group Formats.
``--new-line-format=format'`
  Use format to output a line taken from just the second file in if-then-else format. See section 2.7.2 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. See section 2.7.1 Line Group Formats.
``--old-line-format=format'`
  Use format to output a line taken from just the first file in if-then-else format. See section 2.7.2 Line Formats.
``-p'`
  Show which C function each change is in. See section 2.3.3.2 Showing 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. See section 3. Comparing Directories.
``--paginate'`
  Pass the output through `pr` to paginate it. See section 4.2 Paginating `diff` Output.
``-q'`
  Report only whether the files differ, not the details of the differences. See section 1.6 Summarizing Which Files Differ.
``-r'`
  When comparing directories, recursively compare any subdirectories found. See section 3. Comparing Directories.
``--rcs'`
  Output RCS-format diffs; like ``-f'` except that each command specifies the number of lines affected. See section 2.6.3 RCS Scripts.
``--recursive'`
  When comparing directories, recursively compare any subdirectories found. See section 3. Comparing Directories.
``--report-identical-files'`
  Report when two files are the same. See section 3. Comparing Directories.
``-s'`
  Report when two files are the same. See section 3. Comparing Directories.
``-S file'`
  When comparing directories, start with the file file. This is used for resuming an aborted comparison. See section 3. 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. See section 2.3.3.2 Showing 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. See section 2.3.3.1 Showing Lines That Match Regular Expressions.
``--side-by-side'`
  Use the side by side output format. See section 2.5 Controlling Side by Side Format.
``--speed-large-files'`
  Use heuristics to speed handling of large files that have numerous scattered small changes. See section 5. `diff` Performance Tradeoffs.
``--starting-file=file'`
  When comparing directories, start with the file file. This is used for resuming an aborted comparison. See section 3. Comparing Directories.
``--suppress-common-lines'`
  Do not print common lines in side by side format. See section 2.5 Controlling Side by Side Format.
``-t'`
  Expand tabs to spaces in the output, to preserve the alignment of tabs in the input files. See section 4.1 Preserving Tabstop Alignment.
``-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. See section 4.1 Preserving Tabstop Alignment.
``--text'`
  Treat all files as text and compare them line-by-line, even if they do not appear to be text. See section 1.7 Binary Files and Forcing Text Comparisons.
``-u'`
  Use the unified output format. See section 2.3.2 Unified Format.
``--unchanged-group-format=format'`
  Use format to output a group of common lines taken from both files in if-then-else format. See section 2.7.1 Line Group Formats.
``--unchanged-line-format=format'`
  Use format to output a line common to both files in if-then-else format. See section 2.7.2 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. See section 3. 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. See section 2.3.2 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. See section 1.2 Suppressing Differences in Blank and Tab Spacing.
``-W columns'`
``--width=columns'`
  Use an output width of columns in side by side format. See section 2.5 Controlling Side by Side Format.
``-x pattern'`
  When comparing directories, ignore files and subdirectories whose basenames match pattern. See section 3. Comparing Directories.
``-X file'`
  When comparing directories, ignore files and subdirectories whose basenames match any pattern contained in file. See section 3. Comparing Directories.
``-y'`
  Use the side by side output format. See section 2.5 Controlling Side by Side Format.

Go to the first, previous, next, last section, table of contents.