| [Top] | [Contents] | [Index] | [ ? ] |
This file documents the GNU diff, diff3,
sdiff, and cmp commands for showing the
differences between files and the GNU patch command for
using their output to update files.
This is Edition 2.8, dated 23 March 2002,
for diffutils 2.8 and patch 2.5.4.
Overview Preliminary information. 1. What Comparison Means What file comparison means.
2. diffOutput FormatsFormats for two-way difference reports. 3. Incomplete Lines Lines that lack trailing newlines. 4. Comparing Directories Comparing files and directories. 5. Making diffOutput PrettierMaking diffoutput prettier.6. diffPerformance TradeoffsMaking diffsmarter or faster.
7. Comparing Three Files Formats for three-way difference reports. 8. Merging From a Common Ancestor Merging from a common ancestor.
9. Interactive Merging with sdiffInteractive merging with sdiff.
10. Merging with patchUsing patchto change old files into new ones.11. Tips for Making and Using Patches Tips for making and using patch distributions.
12. Invoking cmpCompare two files byte by byte. 13. Invoking diffCompare two files line by line. 14. Invoking diff3Compare three files line by line. 15. Invoking patchApply a diff file to an original. 16. Invoking sdiffSide-by-side merge of file differences.
17. Standards conformance Conformance to the POSIX standard. 18. Future Projects If you think you've found a bug or other shortcoming.
A. Copying This Manual How to make copies of this manual. B. Concept Index Index of concepts.
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
Computer users often find occasion to ask how two files differ. Perhaps one file is a newer version of the other file. Or maybe the two files started out as identical copies but were changed by different people.
You can use the diff command to show 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.
You can use the cmp command to show the byte and line numbers
where two files differ. cmp can also show all the bytes
that differ between the two files, side by side. A way to compare
two files character by character is the Emacs command M-x
compare-windows. See section `Other Window' in The GNU Emacs Manual, for more information on that command.
You can use the diff3 command to show differences among three
files. When two people have made independent changes to a common
original, diff3 can report the differences between the original
and the two changed versions, and can produce a merged file that
contains both persons' changes together with warnings about conflicts.
You can use the sdiff command to merge two files interactively.
You can use the set of differences produced by diff to distribute
updates to text files (such as program source code) to other people.
This method is especially useful when the differences are small compared
to the complete files. Given diff output, you can use the
patch program to update, or patch, a copy of the file. If you
think of diff as subtracting one file from another to produce
their difference, you can think of patch as adding the difference
to one file to reproduce the other.
This manual first concentrates on making diffs, and later shows how to use diffs to update files.
GNU diff was written by Paul Eggert, Mike Haertel,
David Hayes, Richard Stallman, and Len Tower. Wayne Davison designed and
implemented the unified output format. The basic algorithm is described
in "An O(ND) Difference Algorithm and its Variations", Eugene W. Myers,
Algorithmica Vol. 1 No. 2, 1986, pp. 251--266; and in "A File
Comparison Program", Webb Miller and Eugene W. Myers,
Software--Practice and Experience Vol. 15 No. 11, 1985,
pp. 1025--1040.
The algorithm was independently discovered as described in
"Algorithms for Approximate String Matching",
E. Ukkonen, Information and Control Vol. 64, 1985, pp. 100--118.
GNU diff3 was written by Randy Smith. GNU
sdiff was written by Thomas Lord. GNU cmp
was written by Torbjorn Granlund and David MacKenzie.
patch was written mainly by Larry Wall and Paul Eggert;
several GNU enhancements were contributed by Wayne Davison and
David MacKenzie. Parts of this manual are adapted from a manual page
written by Larry Wall, with his permission.
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
There are several ways to think about the differences between two files.
One way to think of the differences is as a series of lines that were
deleted from, inserted in, or changed in one file to produce the other
file. diff compares two files line by line, finds groups of
lines that differ, and reports each group of differing lines. It can
report the differing lines in several formats, which have different
purposes.
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.
Another way to think of the differences between two files is as a
sequence of pairs of bytes that can be either identical or
different. cmp reports the differences between two files
byte by byte, instead of line by line. As a result, it is often
more useful than diff for comparing binary files. For text
files, cmp is useful mainly when you want to know only whether
two files are identical, or whether one file is a prefix of the other.
To illustrate the effect that considering changes byte by byte
can have compared with considering them line by line, think of what
happens if a single newline character is added to the beginning of a
file. If that file is then compared with an otherwise identical file
that lacks the newline at the beginning, diff will report that a
blank line has been added to the file, while cmp will report that
almost every byte of the two files differs.
diff3 normally compares three input files line by line, finds
groups of lines that differ, and reports each group of differing lines.
Its output is designed to make it easy to inspect two different sets of
changes to the same file.
1.1 Hunks Groups of differing lines. 1.2 Suppressing Differences in Blank and Tab Spacing Suppressing differences in white space. 1.3 Suppressing Differences in Blank Lines Suppressing differences in blank lines. 1.4 Suppressing Case Differences Suppressing differences in alphabetic case. 1.5 Suppressing Lines Matching a Regular Expression Suppressing differences that match regular expressions. 1.6 Summarizing Which Files Differ Summarizing which files are different. 1.7 Binary Files and Forcing Text Comparisons Comparing binary files or forcing text comparisons.
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
When comparing two files, diff finds sequences of lines common to
both files, interspersed with groups of differing lines called
hunks. Comparing two identical files yields one sequence of
common lines and no hunks, because no lines differ. Comparing two
entirely different files yields no common lines and one large hunk that
contains all lines of both files. In general, there are many ways to
match up lines between two given files. diff tries to minimize
the total hunk size by finding large sequences of common lines
interspersed with small hunks of differing lines.
For example, suppose the file `F' contains the three lines
`a', `b', `c', and the file `G' contains the same
three lines in reverse order `c', `b', `a'. If
diff finds the line `c' as common, then the command
`diff F G' produces this output:
1,2d0 < a < b 3a2,3 > b > a |
But if diff notices the common line `b' instead, it produces
this output:
1c1 < a --- > c 3c3 < c --- > a |
It is also possible to find `a' as the common line. diff
does not always find an optimal matching between the files; it takes
shortcuts to run faster. But its output is usually close to the
shortest possible. You can adjust this tradeoff with the
`--minimal' option (see section 6. diff Performance Tradeoffs).
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
The `-E' and `--ignore-tab-expansion' options ignore the
distinction between tabs and spaces on input. A tab is considered to be
equivalent to the number of spaces to the next tab stop. diff
assumes that tab stops are set every 8 print columns.
The `-b' and `--ignore-space-change' options are stronger.
They ignore white space at line end, and consider 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 still.
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 ^M$ |
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
The `-B' and `--ignore-blank-lines' options ignore insertions or deletions of blank lines. These options affect only lines that are completely empty; they do not affect lines that look empty but contain space or tab characters. With these options, for example, a file containing
1. A point is that which has no part. 2. A line is breadthless length. -- Euclid, The Elements, I |
1. A point is that which has no part. 2. A line is breadthless length. -- Euclid, The Elements, I |
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
GNU diff can treat lower case letters as equivalent to their
upper case 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.
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
To ignore insertions and deletions of lines that match a
grep-style 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 '^[[:digit:]]'' 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.
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
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. For files that are identical, cmp produces no
output. When the files differ, by default, cmp outputs the byte
and line number where the first difference occurs. You can use
the `-s' option to suppress that information, so that cmp
produces no output and reports whether the files differ using only its
exit status (see section 12. Invoking cmp).
Unlike diff, cmp cannot compare directories; it can only
compare two files.
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
If diff thinks that either of the two files it is comparing is
binary (a non-text file), it normally treats that pair of files much as
if the summary output format had been selected (see section 1.6 Summarizing Which Files Differ), and
reports only that the binary files are different. This is because line
by line comparisons are usually not meaningful for binary files.
diff determines whether a file is text or binary by checking the
first few bytes in the file; the exact number of bytes is system
dependent, but it is typically several thousand. If every byte in
that part of the file is non-null, diff considers the file to be
text; otherwise it considers the file to be binary.
Sometimes you might want to force diff to consider files to be
text. For example, you might be comparing text files that contain
null characters; diff would erroneously decide that those are
non-text files. Or you might be comparing documents that are in a
format used by a word processing system that uses null characters to
indicate special formatting. You can force diff to consider all
files to be text files, and compare them line by line, by using the
`-a' or `--text' option. If the files you compare using this
option do not in fact contain text, they will probably contain few
newline characters, and the diff output will consist of hunks
showing differences between long lines of whatever characters the files
contain.
You can also force diff to consider all files to be binary files,
and report only whether they differ (but not how). Use the
`-q' or `--brief' option for this.
Differing binary files are considered to cause trouble because the
resulting diff output does not capture all the differences.
This trouble causes diff to exit with status 2. However,
this trouble cannot occur with the `--a' or `--text'
option, or with the `-q' or `--brief' option, as these
options both cause diff to treat binary files like text
files.
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.
The `--strip-trailing-cr' causes diff to treat input
lines that end in carriage return followed by newline as if they end
in plain newline. This can be useful when comparing text that is
imperfectly imported from many personal computer operating systems.
This option affects how lines are read, which in turn affects how they
are compared and output.
If you want to compare two files byte by byte, you can use the
cmp program with the `-l' option to show the values
of each differing byte in the two files. With GNU cmp,
you can also use the `-b' option to show the ASCII
representation of those bytes. See section 12. Invoking cmp, for more
information.
If diff3 thinks that any of the files it is comparing is binary
(a non-text file), it normally reports an error, because such
comparisons are usually not useful. diff3 uses the same test as
diff to decide whether a file is binary. As with diff, if
the input files contain a few non-text bytes but otherwise are like
text files, you can force diff3 to consider all files to be text
files and compare them line by line by using the `-a' or
`--text' options.
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
diff Output Formats
diff has several mutually exclusive options for output format.
The following sections describe each format, illustrating how
diff reports the differences between two sample input files.
2.1 Two Sample Input Files Sample diffinput files for examples.2.2 Showing Differences Without Context Showing differences without surrounding text. 2.3 Showing Differences in Their Context Showing differences with the surrounding text. 2.4 Showing Differences Side by Side Showing differences in two columns. 2.5 Making Edit Scripts Generating scripts for other programs. 2.6 Merging Files with If-then-else Merging files with if-then-else.
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
Here are two sample files that we will use in numerous examples to
illustrate the output of diff and how various options can change
it.
This is the file `lao':
The Way that can be told of is not the eternal Way; The name that can be named is not the eternal name. The Nameless is the origin of Heaven and Earth; The Named is the mother of all things. Therefore let there always be non-being, so we may see their subtlety, And let there always be being, so we may see their outcome. The two are the same, But after they are produced, they have different names. |
This is the file `tzu':
The Nameless is the origin of Heaven and Earth; The named is the mother of all things. Therefore let there always be non-being, so we may see their subtlety, And let there always be being, so we may see their outcome. The two are the same, But after they are produced, they have different names. They both may be called deep and profound. Deeper and more profound, The door of all subtleties! |
In this example, the first hunk contains just the first two lines of `lao', the second hunk contains the fourth line of `lao' opposing the second and third lines of `tzu', and the last hunk contains just the last three lines of `tzu'.
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
The "normal" diff output format shows each hunk of differences
without any surrounding context. Sometimes such output is the clearest
way to see how lines have changed, without the clutter of nearby
unchanged lines (although you can get similar results with the context
or unified formats by using 0 lines of context). However, this format
is no longer widely used for sending out patches; for that purpose, the
context format (see section 2.3.1 Context Format) and the unified format
(see section 2.3.2 Unified Format) are superior. Normal format is the default for
compatibility with older versions of diff and the POSIX
standard. Use the `--normal' option to select this output
format explicitly.
2.2.1 Detailed Description of Normal Format A detailed description of normal output format. 2.2.2 An Example of Normal Format Sample output in the normal format.
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
The normal output format consists of one or more hunks of differences; each hunk shows one area where the files differ. Normal format hunks look like this:
change-command < from-file-line < from-file-line... --- > to-file-line > to-file-line... |
There are three types of change commands. Each consists of a line number or comma-separated range of lines in the first file, a single character indicating the kind of change to make, and a line number or comma-separated range of lines in the second file. All line numbers are the original line numbers in each file. The types of change commands are:
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
Here is the output of the command `diff lao tzu' (see section 2.1 Two Sample Input Files, for the complete contents of the two files). Notice that it shows only the lines that are different between the two files.
1,2d0 < The Way that can be told of is not the eternal Way; < The name that can be named is not the eternal name. 4c2,3 < The Named is the mother of all things. --- > The named is the mother of all things. > 11a11,13 > They both may be called deep and profound. > Deeper and more profound, > The door of all subtleties! |
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
Usually, when you are looking at the differences between files, you will also want to see the parts of the files near the lines that differ, to help you understand exactly what has changed. These nearby parts of the files are called the context.
GNU diff provides two output formats that show context
around the differing lines: context format and unified
format. It can optionally show in which function or section of the
file the differing lines are found.
If you are distributing new versions of files to other people in the
form of diff output, you should use one of the output formats
that show context so that they can apply the diffs even if they have
made small changes of their own to the files. patch can apply
the diffs in this case 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.
See section 10.3 Applying Imperfect Patches, for more information on using patch to apply
imperfect diffs.
2.3.1 Context Format An output format that shows surrounding lines. 2.3.2 Unified Format A more compact output format that shows context. 2.3.3 Showing Which Sections Differences Are in Showing which sections of the files differences are in. 2.3.4 Showing Alternate File Names Showing alternate file names in context headers.
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
The context output format shows several lines of context around the lines that differ. It is the standard format for distributing updates to source code.
To select this output format, use the `-C lines',
`--context[=lines]', or `-c' option. The
argument lines that some of these options take is the number of
lines of context to show. If you do not specify lines, it
defaults to three. For proper operation, patch typically needs
at least two lines of context.
2.3.1.1 Detailed Description of Context Format A detailed description of the context output format. 2.3.1.2 An Example of Context Format Sample output in context format. 2.3.1.3 An Example of Context Format with Less Context Another sample with less context.
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
The context output format starts with a two-line header, which looks like this:
*** from-file from-file-modification-time --- to-file to-file-modification time |
The time stamp normally looks like `2002-02-21 23:30:39.942229878
-0800' to indicate the date, time with fractional seconds, and time
zone in Internet RFC
2822 format. However, a traditional time stamp like `Thu Feb 21
23:30:39 2002' is used if the LC_TIME locale category is either
`C' or `POSIX'.
You can change the header's content with the `--label=label' option; see 2.3.4 Showing Alternate File Names.
Next come one or more hunks of differences; each hunk shows one area where the files differ. Context format hunks look like this:
*************** *** from-file-line-range **** from-file-line from-file-line... --- to-file-line-range ---- to-file-line to-file-line... |
The lines of context around the lines that differ start with two space characters. The lines that differ between the two files start with one of the following indicator characters, followed by a space character:
If all of the changes in a hunk are insertions, the lines of from-file are omitted. If all of the changes are deletions, the lines of to-file are omitted.
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
Here is the output of `diff -c lao tzu' (see section 2.1 Two Sample Input Files, for the complete contents of the two files). Notice that up to three lines that are not different are shown around each line that is different; they are the context lines. Also notice that the first two hunks have run together, because their contents overlap.
*** lao 2002-02-21 23:30:39.942229878 -0800
--- tzu 2002-02-21 23:30:50.442260588 -0800
***************
*** 1,7 ****
- The Way that can be told of is not the eternal Way;
- The name that can be named is not the eternal name.
The Nameless is the origin of Heaven and Earth;
! The Named is the mother of all things.
Therefore let there always be non-being,
so we may see their subtlety,
And let there always be being,
--- 1,6 ----
The Nameless is the origin of Heaven and Earth;
! The named is the mother of all things.
!
Therefore let there always be non-being,
so we may see their subtlety,
And let there always be being,
***************
*** 9,11 ****
--- 8,13 ----
The two are the same,
But after they are produced,
they have different names.
+ They both may be called deep and profound.
+ Deeper and more profound,
+ The door of all subtleties!
|
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
Here is the output of `diff -C 1 lao tzu' (see section 2.1 Two Sample Input Files, for the complete contents of the two files). Notice that at most one context line is reported here.
*** lao 2002-02-21 23:30:39.942229878 -0800
--- tzu 2002-02-21 23:30:50.442260588 -0800
***************
*** 1,5 ****
- The Way that can be told of is not the eternal Way;
- The name that can be named is not the eternal name.
The Nameless is the origin of Heaven and Earth;
! The Named is the mother of all things.
Therefore let there always be non-being,
--- 1,4 ----
The Nameless is the origin of Heaven and Earth;
! The named is the mother of all things.
!
Therefore let there always be non-being,
***************
*** 11 ****
--- 10,13 ----
they have different names.
+ They both may be called deep and profound.
+ Deeper and more profound,
+ The door of all subtleties!
|
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
The unified output format is a variation on the context format that is more compact because it omits redundant context lines. To select this output format, use the `-U lines', `--unified[=lines]', or `-u' option. The argument lines is the number of lines of context to show. When it is not given, it defaults to three.
At present, only GNU diff can produce this format and
only GNU patch can automatically apply diffs in this
format. For proper operation, patch typically needs at
least three lines of context.
2.3.2.1 Detailed Description of Unified Format A detailed description of unified format. 2.3.2.2 An Example of Unified Format Sample output in unified format.
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
The unified output format starts with a two-line header, which looks like this:
--- from-file from-file-modification-time +++ to-file to-file-modification-time |
The time stamp looks like `2002-02-21 23:30:39.942229878 -0800' to indicate the date, time with fractional seconds, and time zone.
You can change the header's content with the `--label=label' option; see See section 2.3.4 Showing Alternate File Names.
Next come one or more hunks of differences; each hunk shows one area where the files differ. Unified format hunks look like this:
@@ from-file-range to-file-range @@ line-from-either-file line-from-either-file... |
The lines common to both files begin with a space character. The lines that actually differ between the two files have one of the following indicator characters in the left print column:
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
Here is the output of the command `diff -u lao tzu' (see section 2.1 Two Sample Input Files, for the complete contents of the two files):
--- lao 2002-02-21 23:30:39.942229878 -0800 +++ tzu 2002-02-21 23:30:50.442260588 -0800 @@ -1,7 +1,6 @@ -The Way that can be told of is not the eternal Way; -The name that can be named is not the eternal name. The Nameless is the origin of Heaven and Earth; -The Named is the mother of all things. +The named is the mother of all things. + Therefore let there always be non-being, so we may see their subtlety, And let there always be being, @@ -9,3 +8,6 @@ The two are the same, But after they are produced, they have different names. +They both may be called deep and profound. +Deeper and more profound, +The door of all subtleties! |
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
Sometimes you might want to know which part of the files each change
falls in. If the files are source code, this could mean which function
was changed. If the files are documents, it could mean which chapter or
appendix was changed. GNU diff can show this by displaying the
nearest section heading line that precedes the differing lines. Which
lines are "section headings" is determined by a regular expression.
2.3.3.1 Showing Lines That Match Regular Expressions Showing headings that match regular expressions. 2.3.3.2 Showing C Function Headings Showing headings of C functions.
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
To show in which sections differences occur for files that are not
source code for C or similar languages, use the `-F regexp'
or `--show-function-line=regexp' option. diff
considers lines that match the grep-style regular expression
regexp to be the beginning
of a section of the file. Here are suggested regular expressions for
some common languages:
This option does not automatically select an output format; in order to use it, you must select the context format (see section 2.3.1 Context Format) or unified format (see section 2.3.2 Unified Format). In other output formats it has no effect.
The `-F' and `--show-function-line' options find the nearest
unchanged line that precedes each hunk of differences and matches the
given regular expression. Then they add that line to the end of the
line of asterisks in the context format, or to the `@@' line in
unified format. If no matching line exists, they leave the output for
that hunk unchanged. If that line is more than 40 characters long, they
output only the first 40 characters. You can specify more than one
regular expression for such lines; diff tries to match each line
against each regular expression, starting with the last one given. This
means that you can use `-p' and `-F' together, if you wish.
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
To show in which functions differences occur for C and similar languages, you can use the `-p' or `--show-c-function' option. This option automatically defaults to the context output format (see section 2.3.1 Context Format), with the default number of lines of context. You can override that number with `-C lines' elsewhere in the command line. You can override both the format and the number with `-U lines' elsewhere in the command line.
The `-p' and `--show-c-function' options are equivalent to
`-F '^[[:alpha:]$_]'' if the unified format is specified, otherwise
`-c -F '^[[:alpha:]$_]'' (see section 2.3.3.1 Showing Lines That Match Regular Expressions). GNU
diff provides them for the sake of convenience.
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
If you are comparing two files that have meaningless or uninformative
names, you might want diff to show alternate names in the header
of the context and unified output formats. To do this, use the
`--label=label' option. The first time
you give this option, its argument replaces the name and date of the
first file in the header; the second time, its argument replaces the
name and date of the second file. If you give this option more than
twice, diff reports an error. The `--label' option does not
affect the file names in the pr header when the `-l' or
`--paginate' option is used (see section 5.2 Paginating diff Output).
Here are the first two lines of the output from `diff -C 2 --label=original --label=modified lao tzu':
*** original --- modified |
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
diff can produce a side by side difference listing of two files.
The files are listed in two columns with a gutter between them. The
gutter contains one of the following markers:
Normally, an output line is incomplete if and only if the lines that it contains are incomplete; See section 3. Incomplete Lines. However, when an output line represents two differing lines, one might be incomplete while the other is not. In this case, the output line is complete, but its the gutter is marked `\' if the first line is incomplete, `/' if the second line is.
Side by side format is sometimes easiest to read, but it has limitations. It generates much wider output than usual, and truncates lines that are too long to fit. Also, it relies on lining up output more heavily than usual, so its output looks particularly bad if you use varying width fonts, nonstandard tab stops, or nonprinting characters.
You can use the sdiff command to interactively merge side by side
differences. See section 9. Interactive Merging with sdiff, for more information on merging files.
2.4.1 Controlling Side by Side Format Controlling side by side output format. 2.4.2 An Example of Side by Side Format Sample side by side output.
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
The `-y' or `--side-by-side' option selects side by side format. Because side by side output lines contain two input lines, the output is wider than usual: normally 130 print columns, which can fit onto a traditional printer line. You can set the width of the output with the `-W columns' or `--width=columns' option. The output is split into two halves of equal width, separated by a small gutter to mark differences; the right half is aligned to a tab stop so that tabs line up. Input lines that are too long to fit in half of an output line are truncated for output.
The `--left-column' option prints only the left column of two common lines. The `--suppress-common-lines' option suppresses common lines entirely.
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
Here is the output of the command `diff -y -W 72 lao tzu' (see section 2.1 Two Sample Input Files, for the complete contents of the two files).
The Way that can be told of is n <
The name that can be named is no <
The Nameless is the origin of He The Nameless is the origin of He
The Named is the mother of all t | The named is the mother of all t
>
Therefore let there always be no Therefore let there always be no
so we may see their subtlety, so we may see their subtlety,
And let there always be being, And let there always be being,
so we may see their outcome. so we may see their outcome.
The two are the same, The two are the same,
But after they are produced, But after they are produced,
they have different names. they have different names.
> They both may be called deep and
> Deeper and more profound,
> The door of all subtleties!
|
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
Several output modes produce command scripts for editing from-file to produce to-file.
2.5.1 edScriptsUsing diffto produce commands fored.2.5.2 Forward edScriptsMaking forward edscripts.2.5.3 RCS Scripts A special diffoutput format used by RCS.
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
ed Scripts
diff can produce commands that direct the ed text editor
to change the first file into the second file. Long ago, this was the
only output mode that was suitable for editing one file into another
automatically; today, with patch, it is almost obsolete. Use the
`-e' or `--ed' option to select this output format.
Like the normal format (see section 2.2 Showing Differences Without Context), this output format does not show any context; unlike the normal format, it does not include the information necessary to apply the diff in reverse (to produce the first file if all you have is the second file and the diff).
If the file `d' contains the output of `diff -e old new', then the command `(cat d && echo w) | ed - old' edits `old' to make it a copy of `new'. More generally, if `d1', `d2', ..., `dN' contain the outputs of `diff -e old new1', `diff -e new1 new2', ..., `diff -e newN-1 newN', respectively, then the command `(cat d1 d2 ... dN && echo w) | ed - old' edits `old' to make it a copy of `newN'.
2.5.1.1 Detailed Description of edFormatA detailed description of edformat.2.5.1.2 Example edScriptA sample edscript.
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
ed Format
The ed output format consists of one or more hunks of
differences. The changes closest to the ends of the files come first so
that commands that change the number of lines do not affect how
ed interprets line numbers in succeeding commands. ed
format hunks look like this:
change-command to-file-line to-file-line... . |
Because ed uses a single period on a line to indicate the end of
input, GNU diff protects lines of changes that contain a single
period on a line by writing two periods instead, then writing a
subsequent ed command to change the two periods into one. The
ed format cannot represent an incomplete line, so if the second
file ends in a changed incomplete line, diff reports an error and
then pretends that a newline was appended.
There are three types of change commands. Each consists of a line number or comma-separated range of lines in the first file and a single character indicating the kind of change to make. All line numbers are the original line numbers in the file. The types of change commands are:
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
ed Script Here is the output of `diff -e lao tzu' (see section 2.1 Two Sample Input Files, for the complete contents of the two files):
11a They both may be called deep and profound. Deeper and more profound, The door of all subtleties! . 4c The named is the mother of all things. . 1,2d |
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
ed Scripts
diff can produce output that is like an ed script, but
with hunks in forward (front to back) order. The format of the commands
is also changed slightly: command characters precede the lines they
modify, spaces separate line numbers in ranges, and no attempt is made
to disambiguate hunk lines consisting of a single period. Like
ed format, forward ed format cannot represent incomplete
lines.
Forward ed format is not very useful, because neither ed
nor patch can apply diffs in this format. It exists mainly for
compatibility with older versions of diff. Use the `-f' or
`--forward-ed' option to select it.
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
The RCS output format is designed specifically for use by the Revision
Control System, which is a set of free programs used for organizing
different versions and systems of files. Use the `-n' or
`--rcs' option to select this output format. It is like the
forward ed format (see section 2.5.2 Forward ed Scripts), but it can represent
arbitrary changes to the contents of a file because it avoids the
forward ed format's problems with lines consisting of a single
period and with incomplete lines. Instead of ending text sections with
a line consisting of a single period, each command specifies the number
of lines it affects; a combination of the `a' and `d'
commands are used instead of `c'. Also, if the second file ends
in a changed incomplete line, then the output also ends in an
incomplete line.
Here is the output of `diff -n lao tzu' (see section 2.1 Two Sample Input Files, for the complete contents of the two files):
d1 2 d4 1 a4 2 The named is the mother of all things. a11 3 They both may be called deep and profound. Deeper and more profound, The door of all subtleties! |
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
You can use diff to merge two files of C source code. The output
of diff in this format contains all the lines of both files.
Lines common to both files are output just once; the differing parts are
separated by the C preprocessor directives #ifdef name or
#ifndef name, #else, and #endif. When
compiling the output, you select which version to use by either defining
or leaving undefined the macro name.
To merge two files, use diff with the `-D name' or
`--ifdef=name' option. The argument name is the C
preprocessor identifier to use in the #ifdef and #ifndef
directives.
For example, if you change an instance of wait (&s) to
waitpid (-1, &s, 0) and then merge the old and new files with
the `--ifdef=HAVE_WAITPID' option, then the affected part of your code
might look like this:
do {
#ifndef HAVE_WAITPID
if ((w = wait (&s)) < 0 && errno != EINTR)
#else /* HAVE_WAITPID */
if ((w = waitpid (-1, &s, 0)) < 0 && errno != EINTR)
#endif /* HAVE_WAITPID */
return w;
} while (w != child);
|
You can specify formats for languages other than C by using line group formats and line formats, as described in the next sections.
2.6.1 Line Group Formats Formats for general if-then-else line groups. 2.6.2 Line Formats Formats for each line in a line group. 2.6.3 Detailed Description of If-then-else Format A detailed description of if-then-else format. 2.6.4 An Example of If-then-else Format Sample if-then-else format output.
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
Line group formats let you specify formats suitable for many applications that allow if-then-else input, including programming languages and text formatting languages. A line group format specifies the output format for a contiguous group of similar lines.
For example, the following command compares the TeX files `old' and `new', and outputs a merged file in which old regions are surrounded by `\begin{em}'-`\end{em}' lines, and new regions are surrounded by `\begin{bf}'-`\end{bf}' lines.
diff \
--old-group-format='\begin{em}
%<\end{em}
' \
--new-group-format='\begin{bf}
%>\end{bf}
' \
old new
|
The following command is equivalent to the above example, but it is a little more verbose, because it spells out the default line group formats.
diff \
--old-group-format='\begin{em}
%<\end{em}
' \
--new-group-format='\begin{bf}
%>\end{bf}
' \
--unchanged-group-format='%=' \
--changed-group-format='\begin{em}
%<\end{em}
\begin{bf}
%>\end{bf}
' \
old new
|
Here is a more advanced example, which outputs a diff listing with headers containing line numbers in a "plain English" style.
diff \ --unchanged-group-format='' \ --old-group-format='-------- %dn line%(n=1?:s) deleted at %df: %<' \ --new-group-format='-------- %dN line%(N=1?:s) added after %de: %>' \ --changed-group-format='-------- %dn line%(n=1?:s) changed at %df: %<-------- to: %>' \ old new |
To specify a line group format, use diff with one of the options
listed below. You can specify up to four line group formats, one for
each kind of line group. You should quote format, because it
typically contains shell metacharacters.
In a line group format, ordinary characters represent themselves; conversion specifications start with `%' and have one of the following forms.
printf conversion specification and n is one
of the following letters, stands for n's value formatted with F.
The printf conversion specification can be `%d',
`%o', `%x', or `%X', specifying decimal, octal,
lower case hexadecimal, or upper case hexadecimal output
respectively. After the `%' the following options can appear in
sequence: a series of zero or more flags; an integer
specifying the minimum field width; and a period followed by an
optional integer specifying the minimum number of digits.
The flags are `-' for left-justification, `'' for separating
the digit into groups as specified by the LC_NUMERIC locale category,
and `0' for padding with zeros instead of spaces.
For example, `%5dN' prints the number of new lines in the group
in a field of width 5 characters, using the printf format "%5d".
For example, `%(N=0?no:%dN) line%(N=1?:s)' is equivalent to `no lines' if N (the number of lines in the group in the the new file) is 0, to `1 line' if N is 1, and to `%dN lines' otherwise.
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
Line formats control how each line taken from an input file is output as part of a line group in if-then-else format.
For example, the following command outputs text with a one-character change indicator to the left of the text. The first character of output is `-' for deleted lines, `|' for added lines, and a space for unchanged lines. The formats contain newline characters where newlines are desired on output.
diff \ --old-line-format='-%l ' \ --new-line-format='|%l ' \ --unchanged-line-format=' %l ' \ old new |
To specify a line format, use one of the following options. You should quote format, since it often contains shell metacharacters.
In a line format, ordinary characters represent themselves; conversion specifications start with `%' and have one of the following forms.
printf conversion specification,
stands for the line number formatted with F.
For example, `%.5dn' prints the line number using the
printf format "%.5d". See section 2.6.1 Line Group Formats, for
more about printf conversion specifications.
The default line format is `%l' followed by a newline character.
If the input contains tab characters and it is important that they line up on output, you should ensure that `%l' or `%L' in a line format is just after a tab stop (e.g. by preceding `%l' or `%L' with a tab character), or you should use the `-t' or `--expand-tabs' option.
Taken together, the line and line group formats let you specify many
different formats. For example, the following command uses a format
similar to normal diff format. You can tailor this command
to get fine control over diff output.
diff \ --old-line-format='< %l ' \ --new-line-format='> %l ' \ --old-group-format='%df%(f=l?:,%dl)d%dE %<' \ --new-group-format='%dea%dF%(F=L?:,%dL) %>' \ --changed-group-format='%df%(f=l?:,%dl)c%dF%(F=L?:,%dL) %<--- %>' \ --unchanged-group-format='' \ old new |
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
For lines common to both files, diff uses the unchanged line
group format. For each hunk of differences in the merged output
format, if the hunk contains only lines from the first file,
diff uses the old line group format; if the hunk contains only
lines from the second file, diff uses the new group format;
otherwise, diff uses the changed group format.
The old, new, and unchanged line formats specify the output format of lines from the first file, lines from the second file, and lines common to both files, respectively.
The option `--ifdef=name' is equivalent to the following sequence of options using shell syntax:
--old-group-format='#ifndef name %<#endif /* ! name */ ' \ --new-group-format='#ifdef name %>#endif /* name */ ' \ --unchanged-group-format='%=' \ --changed-group-format='#ifndef name %<#else /* name */ %>#endif /* name */ ' |
You should carefully check the diff output for proper nesting.
For example, when using the `-D name' or
`--ifdef=name' option, you should check that if the
differing lines contain any of the C preprocessor directives
`#ifdef', `#ifndef', `#else', `#elif', or
`#endif', they are nested properly and match. If they don't, you
must make corrections manually. It is a good idea to carefully check
the resulting code anyway to make sure that it really does what you
want it to; depending on how the input files were produced, the output
might contain duplicate or otherwise incorrect code.
The patch `-D name' option behaves like
the diff `-D name' option, except it operates on
a file and a diff to produce a merged file; See section 15.1 Options to patch.
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
Here is the output of `diff -DTWO lao tzu' (see section 2.1 Two Sample Input Files, for the complete contents of the two files):
#ifndef TWO The Way that can be told of is not the eternal Way; The name that can be named is not the eternal name. #endif /* ! TWO */ The Nameless is the origin of Heaven and Earth; #ifndef TWO The Named is the mother of all things. #else /* TWO */ The named is the mother of all things. #endif /* TWO */ Therefore let there always be non-being, so we may see their subtlety, And let there always be being, so we may see their outcome. The two are the same, But after they are produced, they have different names. #ifdef TWO They both may be called deep and profound. Deeper and more profound, The door of all subtleties! #endif /* TWO */ |
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
When an input file ends in a non-newline character, its last line is called an incomplete line because its last character is not a newline. All other lines are called full lines and end in a newline character. Incomplete lines do not match full lines unless differences in white space are ignored (see section 1.2 Suppressing Differences in Blank and Tab Spacing).
An incomplete line is normally distinguished on output from a full line
by a following line that starts with `\'. However, the RCS format
(see section 2.5.3 RCS Scripts) outputs the incomplete line as-is, without any trailing
newline or following line. The side by side format normally represents
incomplete lines as-is, but in some cases uses a `\' or `/'
gutter marker; See section 2.4 Showing Differences Side by Side. The if-then-else line format
preserves a line's incompleteness with `%L', and discards the
newline with `%l'; See section 2.6.2 Line Formats. Finally, with the
ed and forward ed output formats (see section 2. diff Output Formats)
diff cannot represent an incomplete line, so it pretends there
was a newline and reports an error.
For example, suppose `F' and `G' are one-byte files that contain just `f' and `g', respectively. Then `diff F G' outputs
1c1 < f \ No newline at end of file --- > g \ No newline at end of file |
(The exact message may differ in non-English locales.) `diff -n F G' outputs the following without a trailing newline:
d1 1 a1 1 g |
`diff -e F G' reports two errors and outputs the following:
1c g . |
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
You can use diff to compare some or all of the files in two
directory trees. When both file name arguments to diff are
directories, it compares each file that is contained in both
directories, examining file names in alphabetical order as specified by
the LC_COLLATE locale category. Normally
diff is silent about pairs of files that contain no differences,
but if you use the `-s' or `--report-identical-files' option,
it reports pairs of identical files. Normally diff reports
subdirectories common to both directories without comparing
subdirectories' files, but if you use the `-r' or
`--recursive' option, it compares every corresponding pair of files
in the directory trees, as many levels deep as they go.
For file names that are in only one of the directories, diff
normally does not show the contents of the file that exists; it reports
only that the file exists in that directory and not in the other. You
can make diff act as though the file existed but was empty in the
other directory, so that it outputs the entire contents of the file that
actually exists. (It is output as either an insertion or a
deletion, depending on whether it is in the first or the second
directory given.) To do this, use the `-N' or `--new-file'
option.
If the older directory contains one or more large files that are not in the newer directory, you can make the patch smaller by using the `--unidirectional-new-file' option instead of `-N'. This option is like `-N' except that it only inserts the contents of files that appear in the second directory but not the first (that is, files that were added). At the top of the patch, write instructions for the user applying the patch to remove the files that were deleted before applying the patch. See section 11. Tips for Making and Using Patches, for more discussion of making patches for distribution.
To ignore some files while comparing directories, use the `-x pattern' or `--exclude=pattern' option. This option ignores any files or subdirectories whose base names match the shell pattern pattern. Unlike in the shell, a period at the start of the base of a file name matches a wildcard at the start of a pattern. You should enclose pattern in quotes so that the shell does not expand it. For example, the option `-x '*.[ao]'' ignores any file whose name ends with `.a' or `.o'.
This option accumulates if you specify it more than once. For example, using the options `-x 'RCS' -x '*,v'' ignores any file or subdirectory whose base name is `RCS' or ends with `,v'.
If you need to give this option many times, you can instead put the patterns in a file, one pattern per line, and use the `-X file' or `--exclude-from=file' option.
If you have been comparing two directories and stopped partway through, later you might want to continue where you left off. You can do this by using the `-S file' or `--starting-file=file' option. This compares only the file file and all alphabetically later files in the topmost directory level.
If two directories differ only in that file names are lower case in
one directory and upper case in the upper, diff normally
reports many differences because it compares file names in a
case sensitive way. With the `--ignore-file-name-case' option,
diff ignores case differences in file names, so that for example
the contents of the file `Tao' in one directory are compared to
the contents of the file `TAO' in the other. The
`--no-ignore-file-name-case' option cancels the effect of the
`--ignore-file-name-case' option, reverting to the default
behavior.
If an `-x pattern', `--exclude=pattern', `-X file', or `--exclude-from=file' option is specified while the `--ignore-file-name-case' option is in effect, case is ignored when excluding file names matching the specified patterns.
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
diff Output Prettier
diff provides several ways to adjust the appearance of its output.
These adjustments can be applied to any output format.
5.1 Preserving Tab Stop Alignment Preserving the alignment of tab stops. 5.