DiffAndPatch
DiffAndPatch
15 Royal Park
Clifton
Bristol BS8 3AL
United Kingdom
Email: [email protected]
ISBN 0-9541617-5-0
The texinfo source files for this manual are available from
http://www.network-theory.co.uk/diff/manual/src/
i
Table of Contents
Publisher’s Preface . . . . . . . . . . . . . . . . . . . . . . 1
Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
3 Incomplete Lines . . . . . . . . . . . . . . . . . . . . 33
4 Comparing Directories . . . . . . . . . . . . . . 35
12 Invoking cmp . . . . . . . . . . . . . . . . . . . . . . . 71
12.1 Options to cmp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
13 Invoking diff . . . . . . . . . . . . . . . . . . . . . . 75
13.1 Options to diff. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75
14 Invoking diff3 . . . . . . . . . . . . . . . . . . . . . 83
14.1 Options to diff3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83
15 Invoking patch . . . . . . . . . . . . . . . . . . . . . 87
15.1 Options to patch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
iv Comparing and Merging Files with GNU diff and patch
16 Invoking sdiff . . . . . . . . . . . . . . . . . . . . . 93
16.1 Options to sdiff . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93
17 Standards conformance . . . . . . . . . . . . . 97
18 Reporting Bugs . . . . . . . . . . . . . . . . . . . . 99
History . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109
Publisher’s Preface
This manual describes how to use GNU diff and patch to compare
and merge files.
GNU diff and patch are free software. The term "free software" is
sometimes misunderstood — it has nothing to do with price. It is about
freedom. It refers to your freedom to run, copy, distribute, study, change
and improve the software. With GNU diff and patch you have all these
freedoms.
GNU diff and patch are part of the GNU Project. The GNU Project
was launched in 1984 to develop a complete Unix-like operating system
which is free software: the GNU system. It was conceived as a way
of bringing back the cooperative spirit that prevailed in the computing
community in earlier days. Its goal is to make cooperation possible once
again by removing the obstacles to cooperation imposed by the owners
of proprietary software. Variants of the GNU operating system, which
use the kernel Linux, are now widely used; though these systems are
often referred to as “Linux”, they are more accurately called GNU/Linux
systems.
The Free Software Foundation is a tax-exempt charity that raises funds
for work on the GNU Project. It is dedicated to promoting computer
users’ right to use, study, copy, modify, and redistribute computer pro-
grams. You can support the Free Software Foundation by becoming an
associate member — for more information visit the website www.gnu.org.
Brian Gough
Publisher
June 2003
2 Comparing and Merging Files with GNU diff and patch
Overview 3
Overview
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 be-
tween 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 imple-
mented 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 Compar-
ison 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
4 Comparing and Merging Files with GNU diff and patch
1.1 Hunks
When comparing two files, diff finds sequences of lines common to
both files, interspersed with groups of differing lines called hunks. Com-
paring 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.
6 Comparing and Merging Files with GNU diff and patch
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 Chapter 6 [diff Performance],
page 39).
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 char-
acters; 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 re-
sulting 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 Chapter 12 [Invoking cmp],
page 71, for more information.
10 Comparing and Merging Files with GNU diff and patch
Here is the output of ‘diff -C 1 lao tzu’ (see Section 2.1 [Sample diff
Input], page 11, for the complete contents of the two files). Notice that
at most one context line is reported here.
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.
Chapter 2: diff Output Formats 17
‘^[[:alpha:]$_]’
C, C++, Prolog
‘^(’ Lisp
‘^@node’ Texinfo
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], page 14) or unified format (see Section 2.3.2 [Unified Format],
page 16). In other output formats it has no effect.
The ‘-F’ and ‘--show-function-line’ options find the nearest un-
changed 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 as-
terisks 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, start-
ing with the last one given. This means that you can use ‘-p’ and ‘-F’
together, if you wish.
Chapter 2: diff Output Formats 19
‘<’ The files differ and only the first file contains the line.
‘>’ The files differ and only the second file contains the line.
‘(’ Only the first file contains the line, but the difference is
ignored.
‘)’ Only the second file contains the line, but the difference is
ignored.
‘\’ The corresponding lines differ, and only the first line is in-
complete.
‘/’ The corresponding lines differ, and only the second line is
incomplete.
Normally, an output line is incomplete if and only if the lines that
it contains are incomplete; See Chapter 3 [Incomplete Lines], page 33.
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 Chapter 9 [Interactive Merging], page 51, for more infor-
mation on merging files.
2.5.1 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 [Normal], page 12), 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 com-
mand ‘(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’, respec-
22 Comparing and Merging Files with GNU diff and patch
tively, then the command ‘(cat d1 d2 ... dN && echo w) | ed - old’ ed-
its ‘old’ to make it a copy of ‘newN’.
.
4c
The named is the mother of all things.
.
1,2d
a11 3
They both may be called deep and profound.
Deeper and more profound,
24 Comparing and Merging Files with GNU diff and patch
--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.
‘--old-group-format=format’
These line groups are hunks containing only lines from the
first file. The default old group format is the same as the
changed group format if it is specified; otherwise it is a for-
mat that outputs the line group as-is.
26 Comparing and Merging Files with GNU diff and patch
‘--new-group-format=format’
These line groups are hunks containing only lines from the
second file. The default new group format is same as the
changed group format if it is specified; otherwise it is a for-
mat that outputs the line group as-is.
‘--changed-group-format=format’
These line groups are hunks containing lines from both files.
The default changed group format is the concatenation of
the old and new group formats.
‘--unchanged-group-format=format’
These line groups contain lines common to both files. The
default unchanged group format is a format that outputs the
line group as-is.
In a line group format, ordinary characters represent themselves; con-
version specifications start with ‘%’ and have one of the following forms.
‘%<’ stands for the lines from the first file, including the trailing
newline. Each line is formatted according to the old line
format (see Section 2.6.2 [Line Formats], page 27).
‘%>’ stands for the lines from the second file, including the trailing
newline. Each line is formatted according to the new line
format.
‘%=’ stands for the lines common to both files, including the trail-
ing newline. Each line is formatted according to the un-
changed line format.
‘%%’ stands for ‘%’.
‘%c’C’’ where C is a single character, stands for C. C may not be a
backslash or an apostrophe. For example, ‘%c’:’’ stands for
a colon, even inside the then-part of an if-then-else format,
which a colon would normally terminate.
‘%c’\O’’ where O is a string of 1, 2, or 3 octal digits, stands for the
character with octal code O. For example, ‘%c’\0’’ stands
for a null character.
‘Fn’ where F is a printf conversion specification and n is one of
the following letters, stands for n’s value formatted with F.
‘e’ The line number of the line just before the
group in the old file.
‘f’ The line number of the first line in the group
in the old file; equals e + 1.
Chapter 2: diff Output Formats 27
’ \
--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.
‘--old-line-format=format’
formats lines just from the first file.
‘--new-line-format=format’
formats lines just from the second file.
‘--unchanged-line-format=format’
formats lines common to both files.
‘--line-format=format’
formats all lines; in effect, it sets all three above options
simultaneously.
In a line format, ordinary characters represent themselves; conversion
specifications start with ‘%’ and have one of the following forms.
‘%l’ stands for the contents of the line, not counting its trailing
newline (if any). This format ignores whether the line is
incomplete; See Chapter 3 [Incomplete Lines], page 33.
‘%L’ stands for the contents of the line, including its trailing new-
line (if any). If a line is incomplete, this format preserves its
incompleteness.
‘%%’ stands for ‘%’.
‘%c’C’’ where C is a single character, stands for C. C may not be
a backslash or an apostrophe. For example, ‘%c’:’’ stands
for a colon.
‘%c’\O’’ where O is a string of 1, 2, or 3 octal digits, stands for the
character with octal code O. For example, ‘%c’\0’’ stands
for a null character.
‘Fn’ where F is a printf conversion specification, stands for the
line number formatted with F. For example, ‘%.5dn’ prints
the line number using the printf format "%.5d". See Sec-
tion 2.6.1 [Line Group Formats], page 24, for more about
printf conversion specifications.
Chapter 2: diff Output Formats 29
--unchanged-group-format=’%=’ \
--changed-group-format=’#ifndef name
%<#else /* name */
%>#endif /* name */
’
You should carefully check the diff output for proper nesting. For ex-
ample, when using the ‘-D name’ or ‘--ifdef=name’ option, you should
check that if the differing lines contain any of the C preprocessor direc-
tives ‘#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 Sec-
tion 15.1 [patch Options], page 87.
#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.
Chapter 2: diff Output Formats 31
3 Incomplete Lines
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 [White Space], page 6).
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], page 23) 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 ‘/’ gut-
ter marker; See Section 2.4 [Side by Side], page 19. The if-then-else line
format preserves a line’s incompleteness with ‘%L’, and discards the new-
line with ‘%l’; See Section 2.6.2 [Line Formats], page 27. Finally, with
the ed and forward ed output formats (see Chapter 2 [Output Formats],
page 11) 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’ out-
puts the following without a trailing newline:
d1 1
a1 1
g
‘diff -e F G’ reports two errors and outputs the following:
1c
g
.
34 Comparing and Merging Files with GNU diff and patch
Chapter 4: Comparing Directories 35
4 Comparing Directories
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 com-
pares each file that is contained in both directories, examining file names
in alphabetical order as specified by the LC_COLLATE locale category. Nor-
mally 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 Chap-
ter 11 [Making Patches], page 67, 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 subdirec-
tories 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’.
36 Comparing and Merging Files with GNU diff and patch
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 com-
pares 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 dif-
ferences 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.
Chapter 5: Making diff Output Prettier 37
the hunk. In this case, diff normally shifts the hunk’s boundaries when
this merges adjacent hunks, or shifts a hunk’s lines towards the end of the
file. Merging hunks can make the output look nicer in some cases.
Chapter 7: Comparing Three Files 41
====
1:3c
f
2:3c
g
3:3c
h
because it found a two-way hunk containing ‘a’ in the first and third files
and ‘g’ in the second file, then the single line ‘b’ common to all three files,
then a three-way hunk containing the last line of each file.
====3
1:8c
2:7c
so we may see their outcome.
3:9c
so we may see their result.
====
1:11a
2:11,13c
They both may be called deep and profound.
Deeper and more profound,
44 Comparing and Merging Files with GNU diff and patch
yours where mine and yours are identical, because they assume that such
changes have already been merged. If this assumption is not a safe one,
you can use the ‘-A’ or ‘--show-all’ option (see Section 8.2 [Marking
Conflicts], page 46).
Here is the output of the command diff3 with each of these three
options (see Section 7.1 [Sample diff3 Input], page 41, for the complete
contents of the files). Notice that ‘-e’ outputs the union of the disjoint
sets of changes output by ‘-3’ and ‘-x’.
Output of ‘diff3 -e lao tzu tao’:
11a
lines from B
=======
lines from C
>>>>>>> C
The ‘-A’ or ‘--show-all’ option acts like the ‘-e’ option, except that
it brackets conflicts, and it outputs all changes from older to yours,
not just the unmerged changes. Thus, given the sample input files (see
Section 7.1 [Sample diff3 Input], page 41), ‘diff3 -A lao tzu tao’ puts
brackets around the conflict where only ‘tzu’ differs:
<<<<<<< tzu
=======
The Way that can be told of is not the eternal Way;
The name that can be named is not the eternal name.
>>>>>>> tao
And it outputs the three-way conflict as follows:
<<<<<<< lao
||||||| tzu
They both may be called deep and profound.
Deeper and more profound,
The door of all subtleties!
=======
ends in an conflict and one of the input files ends in an incomplete line,
succeeding ‘|||||||’, ‘=======’ or ‘>>>>>>>’ brackets appear somewhere
other than the start of a line because they are appended to the incomplete
line.
Without ‘-m’, if an ed script option is specified and an incomplete line
is found, diff3 generates a warning and acts as if a newline had been
present.
--ignore-blank-lines
--ignore-case
--ignore-matching-lines=regexp
--ignore-space-change
--ignore-tab-expansion
--left-column
--minimal
--speed-large-files
--strip-trailing-cr
--suppress-common-lines
--expand-tabs
--text
--version
--width=columns
For historical reasons, sdiff has alternate names for some options.
The ‘-l’ option is equivalent to the ‘--left-column’ option, and similarly
‘-s’ is equivalent to ‘--suppress-common-lines’. The meaning of the
sdiff ‘-w’ and ‘-W’ options is interchanged from that of diff: with sdiff,
‘-w columns’ is equivalent to ‘--width=columns’, and ‘-W’ is equivalent to
‘--ignore-all-space’. sdiff without the ‘-o’ option is equivalent to
diff with the ‘-y’ or ‘--side-by-side’ option (see Section 2.4 [Side by
Side], page 19).
52 Comparing and Merging Files with GNU diff and patch
‘-c’
‘--context’
context diff.
‘-e’
‘--ed’ ed script.
‘-n’
‘--normal’ normal diff.
54 Comparing and Merging Files with GNU diff and patch
‘-u’
‘--unified’
unified diff.
which makes patch compare blank characters (i.e. spaces and tabs) loosely
so that any nonempty sequence of blanks in the patch file matches any
nonempty sequence of blanks in the input files. Non-blank characters
must still match exactly. Each line of the context must still match a line
in the input file.
The ‘-F lines’ or ‘--fuzz=lines’ option sets the maximum fuzz factor
to lines. This option only applies to context and unified diffs; it ignores
up to lines lines while looking for the place to install a hunk. Note that a
larger fuzz factor increases the odds of making a faulty patch. The default
fuzz factor is 2; there is no point to setting it to more than the number of
lines of context in the diff, ordinarily 3.
If patch cannot find a place to install a hunk of the patch, it writes the
hunk out to a reject file (see Section 10.10 [Reject Names], page 61, for
information on how reject files are named). It writes out rejected hunks
in context format no matter what form the input patch is in. If the input
is a normal or ed diff, many of the contexts are simply null. The line
numbers on the hunks in the reject file may be different from those in the
patch file: they show the approximate location where patch thinks the
failed hunks belong in the new file rather than in the old one.
If the ‘--verbose’ option is given, then as it completes each hunk
patch tells you whether the hunk succeeded or failed, and if it failed, on
which line (in the new file) patch thinks the hunk should go. If this is
different from the line number specified in the diff, it tells you the offset.
A single large offset may indicate that patch installed a hunk in the wrong
place. patch also tells you if it used a fuzz factor to make the match, in
which case you should also be slightly suspicious.
patch cannot tell if the line numbers are off in an ed script, and can
only detect wrong line numbers in a normal diff when it finds a change
or delete command. It may have the same problem with a context diff
using a fuzz factor equal to or greater than the number of lines of context
shown in the diff (typically 3). In these cases, you should probably look
at a context diff between your original and patched input files to see if the
changes make sense. Compiling without errors is a pretty good indication
that the patch worked, but not a guarantee.
A patch against an empty file applies to a nonexistent file, and vice
versa. See Section 10.4 [Creating and Removing], page 57.
patch usually produces the correct results, even when it must make
many guesses. However, the results are guaranteed only when the patch
is applied to an exact copy of the file that the patch was generated from.
you expect. If the patch does not do what you want, you can modify the
patch (or the other options to patch) and try another dry run. Once you
are satisfied with the proposed patch you can apply it by invoking patch
as before, but this time without the ‘--dry-run’ option.
patch normally refrains from setting a file’s time stamps if the file’s
original last-modified time stamp does not match the time given in the diff
header, of if the file’s contents do not exactly match the patch. However,
if the ‘-f’ or ‘--force’ option is given, the file’s time stamps are set
regardless.
Due to the limitations of the current diff format, patch cannot up-
date the times of files whose contents have not changed. Also, if you set
file time stamps to values other than the current time of day, you should
also remove (e.g., with ‘make clean’) all files that depend on the patched
files, so that later invocations of make do not get confused by the patched
files’ times.
The values of these environment variables and the argument to the ‘-V’
option are like the gnu Emacs version-control variable (see section
“Backup Names” in The gnu Emacs Manual, for more information on
backup versions in Emacs). They also recognize synonyms that are more
descriptive. The valid values are listed below; unique abbreviations are
acceptable.
‘t’
‘numbered’ Always make numbered backups.
‘nil’
‘existing’ Make numbered backups of files that already have them,
simple backups of the others. This is the default.
‘never’
‘simple’ Always make simple backups.
You can also tell patch to prepend a prefix, such as a directory name,
to produce backup file names. The ‘-B prefix’ or ‘--prefix=prefix’ op-
tion makes backup files by prepending prefix to them. The ‘-Y prefix’ or
‘--basename-prefix=prefix’ prepends prefix to the last file name compo-
nent of backup file names instead; for example, ‘-Y ~’ causes the backup
name for ‘dir/file.c’ to be ‘dir/~file.c’. If you use either of these
prefix options, the suffix-based options are ignored.
If you specify the output file with the ‘-o’ option, that file is the one
that is backed up, not the input file.
Options that affect the names of backup files do not affect
whether backups are made. For example, if you specify the
‘--no-backup-if-mismatch’ option, none of the options described in
this section have any affect, because no backups are made.
patch normally prompts you for more information from the keyboard.
There are options to produce more or fewer messages, to have it not ask
for keyboard input, and to affect the way that file names are quoted in
messages.
patch exits with status 0 if all hunks are applied successfully, 1 if some
hunks cannot be applied, and 2 if there is more serious trouble. When
applying a set of patches in a loop, you should check the exit status, so
you don’t apply a later patch to a partially patched file.
interoperate with traditional patch, or with gnu patch version 2.1 and
earlier.
• In traditional patch, the ‘-p’ option’s operand was optional, and
a bare ‘-p’ was equivalent to ‘-p0’. The ‘-p’ option now requires
an operand, and ‘-p 0’ is now equivalent to ‘-p0’. For maximum
compatibility, use options like ‘-p0’ and ‘-p1’.
Also, traditional patch simply counted slashes when stripping path
prefixes; patch now counts pathname components. That is, a se-
quence of one or more adjacent slashes now counts as a single slash.
For maximum portability, avoid sending patches containing ‘//’ in
file names.
• In traditional patch, backups were enabled by default. This behavior
is now enabled with the ‘-b’ or ‘--backup’ option.
Conversely, in posix patch, backups are never made, even when
there is a mismatch. In gnu patch, this behavior is enabled with
the ‘--no-backup-if-mismatch’ option, or by conforming to posix.
The ‘-b suffix’ option of traditional patch is equivalent to the ‘-b
-z suffix’ options of gnu patch.
• Traditional patch used a complicated (and incompletely docu-
mented) method to intuit the name of the file to be patched from
the patch header. This method did not conform to posix, and had
a few gotchas. Now patch uses a different, equally complicated (but
better documented) method that is optionally posix-conforming;
we hope it has fewer gotchas. The two methods are compatible if
the file names in the context diff header and the ‘Index:’ line are all
identical after prefix-stripping. Your patch is normally compatible
if each header’s file names all contain the same number of slashes.
• When traditional patch asked the user a question, it sent the ques-
tion to standard error and looked for an answer from the first file
in the following list that was a terminal: standard error, standard
output, ‘/dev/tty’, and standard input. Now patch sends questions
to standard output and gets answers from ‘/dev/tty’. Defaults for
some answers have been changed so that patch never goes into an
infinite loop when using default answers.
• Traditional patch exited with a status value that counted the number
of bad hunks, or with status 1 if there was real trouble. Now patch
exits with status 1 if some hunks failed, or with 2 if there was real
trouble.
• Limit yourself to the following options when sending instructions
meant to be executed by anyone running gnu patch, traditional
patch, or a patch that conforms to posix. Spaces are significant in
the following list, and operands are required.
Chapter 10: Merging with patch 65
‘-c’
‘-d dir’
‘-D define’
‘-e’
‘-l’
‘-n’
‘-N’
‘-o outfile’
‘-pnum’
‘-R’
‘-r rejectfile’
66 Comparing and Merging Files with GNU diff and patch
Chapter 11: Tips for Making and Using Patches 67
because the two file names have different numbers of slashes, and different
versions of patch interpret the file names differently. To avoid confusion,
send output that looks like this instead:
Make sure you have specified the file names correctly, either in a con-
text diff header or with an ‘Index:’ line. Take care to not send out
reversed patches, since these make people wonder whether they have al-
ready applied the patch.
12 Invoking cmp
The cmp command compares two files, and if they differ, tells the first
byte and line number where they differ. Bytes and lines are numbered
starting with 1. The arguments of cmp are as follows:
cmp options... from-file [to-file [from-skip [to-skip]]]
The file name ‘-’ is always the standard input. cmp also uses the
standard input if one file name is omitted. The from-skip and to-skip
operands specify how many bytes to ignore at the start of each file; they
are equivalent to the ‘--ignore-initial=from-skip:to-skip’ option.
An exit status of 0 means no differences were found, 1 means some
differences were found, and 2 means trouble.
‘-s’
‘--quiet’
‘--silent’ Do not print anything; only return an exit status indicating
whether the files differ.
‘-v’
‘--version’
Output version information and then exit.
In the above table, operands that are byte counts are normally decimal,
but may be preceded by ‘0’ for octal and ‘0x’ for hexadecimal.
A byte count can be followed by a suffix to specify a multiple of that
count; in this case an omitted integer is understood to be 1. A bare size
letter, or one followed by ‘iB’, specifies a multiple using powers of 1024.
A size letter followed by ‘B’ specifies powers of 1000 instead. For example,
‘-n 4M’ and ‘-n 4MiB’ are equivalent to ‘-n 4194304’, whereas ‘-n 4MB’ is
equivalent to ‘-n 4000000’. This notation is upward compatible with the
SI prefixes for decimal multiples and with the IEC 60027-2 prefixes for
binary multiples.
The following suffixes are defined. Large sizes like 1Y may be rejected
by your computer due to limitations of its arithmetic.
‘kB’ kilobyte: 10ˆ3 = 1000.
‘k’
‘K’
‘KiB’ kibibyte: 2ˆ10 = 1024. ‘K’ is special: the SI prefix is ‘k’ and
the IEC 60027-2 prefix is ‘Ki’, but tradition and posix use
‘k’ to mean ‘KiB’.
‘MB’ megabyte: 10ˆ6 = 1, 000, 000.
‘M’
‘MiB’ mebibyte: 2ˆ20 = 1, 048, 576.
‘GB’ gigabyte: 10ˆ9 = 1, 000, 000, 000.
‘G’
‘GiB’ gibibyte: 2ˆ30 = 1, 073, 741, 824.
‘TB’ terabyte: 10ˆ12 = 1, 000, 000, 000, 000.
‘T’
‘TiB’ tebibyte: 2ˆ40 = 1, 099, 511, 627, 776.
‘PB’ petabyte: 10ˆ15 = 1, 000, 000, 000, 000, 000.
‘P’
‘PiB’ pebibyte: 2ˆ50 = 1, 125, 899, 906, 842, 624.
Chapter 12: Invoking cmp 73
13 Invoking diff
The format for running the diff command is:
diff options... files...
In the simplest case, two file names from-file and to-file are given, and
diff compares the contents of 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 one file is a directory and the other is not, diff compares the file in
the directory whose name is that of the non-directory. The non-directory
file must not be ‘-’.
If two file names are given and both are directories, diff compares
corresponding files in both directories, in alphabetical order; this compar-
ison 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.
If the ‘--from-file=file’ option is given, the number of file names
is arbitrary, and file is compared to each named file. Similarly, if the
‘--to-file=file’ option is given, each named file is compared to file.
diff options begin with ‘-’, so normally file names 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.
‘-b’
‘--ignore-space-change’
Ignore changes in amount of white space. See Section 1.2
[White Space], page 6.
‘-B’
‘--ignore-blank-lines’
Ignore changes that just insert or delete blank lines. See
Section 1.3 [Blank Lines], page 7.
‘--binary’ Read and write data in binary mode. See Section 1.7 [Bi-
nary], page 8.
‘-c’ Use the context output format, showing three lines of con-
text. See Section 2.3.1 [Context Format], page 14.
‘-C lines’
‘--context[=lines]’
Use the context output format, showing lines (an integer)
lines of context, or three if lines is not given. See Sec-
tion 2.3.1 [Context Format], page 14. For proper operation,
patch typically needs at least two lines of context.
On older systems, diff supports an obsolete option ‘-lines’
that has effect when combined with ‘-c’ or ‘-p’. posix
1003.1-2001 (see Chapter 17 [Standards conformance],
page 97) does not allow this; use ‘-C lines’ instead.
‘--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.6.1 [Line
Group Formats], page 24.
‘-d’
‘--minimal’
Change the algorithm perhaps find a smaller set of changes.
This makes diff slower (sometimes much slower). See Chap-
ter 6 [diff Performance], page 39.
‘-D name’
‘--ifdef=name’
Make merged ‘#ifdef’ format output, conditional on the
preprocessor macro name. See Section 2.6 [If-then-else],
page 24.
‘-e’
‘--ed’ Make output that is a valid ed script. See Section 2.5.1 [ed
Scripts], page 21.
Chapter 13: Invoking diff 77
‘-E’
‘--ignore-tab-expansion’
Ignore changes due to tab expansion. See Section 1.2 [White
Space], page 6.
‘-f’
‘--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.5.2
[Forward ed], page 23.
‘-F regexp’
‘--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 [Specified Headings], page 18.
‘--from-file=file’
Compare file to each operand; file may be a directory.
‘--help’ Output a summary of usage and then exit.
‘--horizon-lines=lines’
Do not discard the last lines lines of the common prefix and
the first lines lines of the common suffix. See Chapter 6 [diff
Performance], page 39.
‘-i’
‘--ignore-case’
Ignore changes in case; consider upper- and lower-case letters
equivalent. See Section 1.4 [Case Folding], page 7.
‘-I regexp’
‘--ignore-matching-lines=regexp’
Ignore changes that just insert or delete lines that match
regexp. See Section 1.5 [Specified Folding], page 7.
‘--ignore-file-name-case’
Ignore case when comparing file names during recursive com-
parison. See Chapter 4 [Comparing Directories], page 35.
‘-l’
‘--paginate’
Pass the output through pr to paginate it. See Section 5.2
[Pagination], page 37.
‘--label=label’
Use label instead of the file name in the context format (see
Section 2.3.1 [Context Format], page 14) and unified format
78 Comparing and Merging Files with GNU diff and patch
‘-r’
‘--recursive’
When comparing directories, recursively compare any sub-
directories found. See Chapter 4 [Comparing Directories],
page 35.
‘-s’
‘--report-identical-files’
Report when two files are the same. See Chapter 4 [Com-
paring Directories], page 35.
‘-S file’
‘--starting-file=file’
When comparing directories, start with the file file. This is
used for resuming an aborted comparison. See Chapter 4
[Comparing Directories], page 35.
‘--speed-large-files’
Use heuristics to speed handling of large files that have nu-
merous scattered small changes. See Chapter 6 [diff Perfor-
mance], page 39.
‘--strip-trailing-cr’
Strip any trailing carriage return at the end of an input line.
See Section 1.7 [Binary], page 8.
‘--suppress-common-lines’
Do not print common lines in side by side format. See Sec-
tion 2.4.1 [Side by Side Format], page 20.
‘-t’
‘--expand-tabs’
Expand tabs to spaces in the output, to preserve the align-
ment of tabs in the input files. See Section 5.1 [Tabs],
page 37.
‘-T’
‘--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 5.1 [Tabs], page 37.
‘--to-file=file’
Compare each operand to file; file may be a directory.
‘-u’ Use the unified output format, showing three lines of context.
See Section 2.3.2 [Unified Format], page 16.
80 Comparing and Merging Files with GNU diff and patch
‘--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.6.1 [Line
Group Formats], page 24.
‘--unchanged-line-format=format’
Use format to output a line common to both files in if-then-
else format. See Section 2.6.2 [Line Formats], page 27.
‘--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 Chapter 4 [Comparing Directories], page 35.
‘-U lines’
‘--unified[=lines]’
Use the unified output format, showing lines (an integer)
lines of context, or three if lines is not given. See Sec-
tion 2.3.2 [Unified Format], page 16. For proper operation,
patch typically needs at least two lines of context.
On older systems, diff supports an obsolete option ‘-lines’
that has effect when combined with ‘-u’. posix 1003.1-2001
(see Chapter 17 [Standards conformance], page 97) does not
allow this; use ‘-U lines’ instead.
‘-v’
‘--version’
Output version information and then exit.
‘-w’
‘--ignore-all-space’
Ignore white space when comparing lines. See Section 1.2
[White Space], page 6.
‘-W columns’
‘--width=columns’
Output at most columns (default 130) print columns per
line in side by side format. See Section 2.4.1 [Side by Side
Format], page 20.
‘-x pattern’
‘--exclude=pattern’
When comparing directories, ignore files and subdirectories
whose basenames match pattern. See Chapter 4 [Comparing
Directories], page 35.
Chapter 13: Invoking diff 81
‘-X file’
‘--exclude-from=file’
When comparing directories, ignore files and subdirectories
whose basenames match any pattern contained in file. See
Chapter 4 [Comparing Directories], page 35.
‘-y’
‘--side-by-side’
Use the side by side output format. See Section 2.4.1 [Side
by Side Format], page 20.
82 Comparing and Merging Files with GNU diff and patch
Chapter 14: Invoking diff3 83
14 Invoking diff3
The diff3 command compares three files and outputs descriptions of
their differences. Its arguments are as follows:
diff3 options... mine older yours
The files to compare are mine, older, and yours. At most one of these
three file names may be ‘-’, which tells diff3 to read the standard input
for that file.
An exit status of 0 means diff3 was successful, 1 means some conflicts
were found, and 2 means trouble.
=======
lines from yours
>>>>>>> yours
‘--help’ Output a summary of usage and then exit.
‘-i’ Generate ‘w’ and ‘q’ commands at the end of the ed script for
System V compatibility. This option must be combined with
one of the ‘-AeExX3’ options, and may not be combined with
‘-m’. See Section 8.5 [Saving the Changed File], page 49.
‘-L label’
‘--label=label’
Use the label label for the brackets output by the ‘-A’, ‘-E’
and ‘-X’ options. This option may be given up to three times,
one for each input file. The default labels are the names of
the input files. Thus ‘diff3 -L X -L Y -L Z -m A B C’ acts
like ‘diff3 -m A B C’, except that the output looks like it
came from files named ‘X’, ‘Y’ and ‘Z’ rather than from files
named ‘A’, ‘B’ and ‘C’. See Section 8.2 [Marking Conflicts],
page 46.
‘-m’
‘--merge’ Apply the edit script to the first file and send the result to
standard output. Unlike piping the output from diff3 to ed,
this works even for binary files and incomplete lines. ‘-A’ is
assumed if no edit script option is specified. See Section 8.3
[Bypassing ed], page 48.
‘-T’
‘--initial-tab’
Output a tab rather than two spaces before the text of a line
in normal format. This causes the alignment of tabs in the
line to look normal. See Section 5.1 [Tabs], page 37.
‘-v’
‘--version’
Output version information and then exit.
‘-x’
‘--overlap-only’
Like ‘-e’, except output only the overlapping changes. See
Section 8.1 [Which Changes], page 45.
‘-X’ Like ‘-E’, except output only the overlapping changes. In
other words, like ‘-x’, except bracket changes as in ‘-E’. See
Section 8.2 [Marking Conflicts], page 46.
Chapter 14: Invoking diff3 85
‘-3’
‘--easy-only’
Like ‘-e’, except output only the nonoverlapping changes.
See Section 8.1 [Which Changes], page 45.
86 Comparing and Merging Files with GNU diff and patch
Chapter 15: Invoking patch 87
15 Invoking patch
Normally patch is invoked like this:
patch <patchfile
The full format for invoking patch is:
patch options... [origfile [patchfile]]
You can also specify where to read the patch from with the ‘-i patch-
file’ or ‘--input=patchfile’ option. If you do not specify patchfile, or if
patchfile is ‘-’, patch reads the patch (that is, the diff output) from the
standard input.
If you do not specify an input file on the command line, patch tries
to intuit from the leading text (any text in the patch that comes before
the diff output) which file to edit. See Section 10.6 [Multiple Patches],
page 58.
By default, patch replaces the original input file with the patched
version, possibly after renaming the original file into a backup file (see
Section 10.9 [Backup Names], page 60, for a description of how patch
names backup files). You can also specify where to put the output with
the ‘-o file’ or ‘--output=file’ option; however, do not use this option if
file is one of the input files.
‘--binary’ Read and write all files in binary mode, except for standard
output and ‘/dev/tty’. This option has no effect on posix-
conforming systems like gnu/Linux. On systems where this
option makes a difference, the patch should be generated by
‘diff -a --binary’. See Section 1.7 [Binary], page 8.
‘-c’
‘--context’
Interpret the patch file as a context diff. See Section 10.1
[patch Input], page 53.
‘-d directory’
‘--directory=directory’
Make directory directory the current directory for interpret-
ing both file names in the patch file, and file names given as
arguments to other options. See Section 10.7 [patch Direc-
tories], page 59.
‘-D name’
‘--ifdef=name’
Make merged if-then-else output using name. See Section 2.6
[If-then-else], page 24.
‘--dry-run’
Print the results of applying the patches without actually
changing any files. See Section 10.3.4 [Dry Runs], page 56.
‘-e’
‘--ed’ Interpret the patch file as an ed script. See Section 10.1
[patch Input], page 53.
‘-E’
‘--remove-empty-files’
Remove output files that are empty after the patches have
been applied. See Section 10.4 [Creating and Removing],
page 57.
‘-f’
‘--force’ Assume that the user knows exactly what he or she is do-
ing, and do not ask any questions. See Section 10.11 [patch
Messages], page 61.
‘-F lines’
‘--fuzz=lines’
Set the maximum fuzz factor to lines. See Section 10.3.3
[Inexact], page 55.
Chapter 15: Invoking patch 89
‘-g num’
‘--get=num’
If num is positive, get input files from a revision control sys-
tem as necessary; if zero, do not get the files; if negative, ask
the user whether to get the files. See Section 10.2 [Revision
Control], page 54.
‘--help’ Output a summary of usage and then exit.
‘-i patchfile’
‘--input=patchfile’
Read the patch from patchfile rather than from standard
input. See Section 15.1 [patch Options], page 87.
‘-l’
‘--ignore-white-space’
Let any sequence of blanks (spaces or tabs) in the patch file
match any sequence of blanks in the input file. See Sec-
tion 10.3.1 [Changed White Space], page 54.
‘-n’
‘--normal’ Interpret the patch file as a normal diff. See Section 10.1
[patch Input], page 53.
‘-N’
‘--forward’
Ignore patches that patch thinks are reversed or already
applied. See also ‘-R’. See Section 10.3.2 [Reversed Patches],
page 55.
‘--no-backup-if-mismatch’
Do not back up the original contents of files. This is the
default behavior when conforming to posix. See Section 10.8
[Backups], page 60.
‘-o file’
‘--output=file’
Use file as the output file name. See Section 15.1 [patch
Options], page 87.
‘-pnumber’
‘--strip=number’
Set the file name strip count to number. See Section 10.7
[patch Directories], page 59.
‘--posix’ Conform to posix, as if the POSIXLY_CORRECT environment
variable had been set. See Section 10.12 [patch and POSIX],
page 63.
90 Comparing and Merging Files with GNU diff and patch
‘--quoting-style=word’
Use style word to quote names in diagnostics, as if the
QUOTING_STYLE environment variable had been set to word.
See Section 10.11.3 [patch Quoting Style], page 62.
‘-r reject-file’
‘--reject-file=reject-file’
Use reject-file as the reject file name. See Section 10.10
[Reject Names], page 61.
‘-R’
‘--reverse’
Assume that this patch was created with the old and
new files swapped. See Section 10.3.2 [Reversed Patches],
page 55.
‘-s’
‘--quiet’
‘--silent’ Work silently unless an error occurs. See Section 10.11 [patch
Messages], page 61.
‘-t’
‘--batch’ Do not ask any questions. See Section 10.11 [patch Mes-
sages], page 61.
‘-T’
‘--set-time’
Set the modification and access times of patched files from
time stamps given in context diff headers, assuming that
the context diff headers use local time. See Section 10.5
[Patching Time Stamps], page 57.
‘-u’
‘--unified’
Interpret the patch file as a unified diff. See Section 10.1
[patch Input], page 53.
‘-v’
‘--version’
Output version information and then exit.
‘-V backup-style’
‘--version=control=backup-style’
Select the naming convention for backup file names. See
Section 10.9 [Backup Names], page 60.
‘--verbose’
Print more diagnostics than usual. See Section 10.11 [patch
Messages], page 61.
Chapter 15: Invoking patch 91
‘-x number’
‘--debug=number’
Set internal debugging flags. Of interest only to patch patch-
ers.
‘-Y prefix’
‘--basename-prefix=prefix’
Prepend prefix to base names of backup files. See Sec-
tion 10.9 [Backup Names], page 60.
‘-z suffix’
‘--suffix=suffix’
Use suffix as the backup extension instead of ‘.orig’ or ‘~’.
See Section 10.9 [Backup Names], page 60.
‘-Z’
‘--set-utc’
Set the modification and access times of patched files from
time stamps given in context diff headers, assuming that the
context diff headers use utc. See Section 10.5 [Patching
Time Stamps], page 57.
92 Comparing and Merging Files with GNU diff and patch
Chapter 16: Invoking sdiff 93
16 Invoking sdiff
The sdiff command merges two files and interactively outputs the
results. Its arguments are as follows:
sdiff -o outfile options... from-file to-file
This merges from-file with to-file, with output to outfile. If from-file
is a directory and to-file is not, sdiff compares the file in from-file whose
file name is that of to-file, and vice versa. from-file and to-file may not
both be directories.
sdiff 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 ‘-’. You may not use ‘-’
as an input file.
sdiff without ‘-o’ (or ‘--output’) produces a side-by-side difference.
This usage is obsolete; use the ‘-y’ or ‘--side-by-side’ option of diff
instead.
An exit status of 0 means no differences were found, 1 means some
differences were found, and 2 means trouble.
‘-d’
‘--minimal’
Change the algorithm to perhaps find a smaller set of
changes. This makes sdiff slower (sometimes much slower).
See Chapter 6 [diff Performance], page 39.
‘--diff-program=program’
Use the compatible comparison program program to com-
pare files instead of diff.
‘-E’
‘--ignore-tab-expansion’
Ignore changes due to tab expansion. See Section 1.2 [White
Space], page 6.
‘--help’ Output a summary of usage and then exit.
‘-i’
‘--ignore-case’
Ignore changes in case; consider upper- and lower-case to be
the same. See Section 1.4 [Case Folding], page 7.
‘-I regexp’
‘--ignore-matching-lines=regexp’
Ignore changes that just insert or delete lines that match
regexp. See Section 1.5 [Specified Folding], page 7.
‘-l’
‘--left-column’
Print only the left column of two common lines. See Sec-
tion 2.4.1 [Side by Side Format], page 20.
‘-o file’
‘--output=file’
Put merged output into file. This option is required for
merging.
‘-s’
‘--suppress-common-lines’
Do not print common lines. See Section 2.4.1 [Side by Side
Format], page 20.
‘--speed-large-files’
Use heuristics to speed handling of large files that have nu-
merous scattered small changes. See Chapter 6 [diff Perfor-
mance], page 39.
‘--strip-trailing-cr’
Strip any trailing carriage return at the end of an input line.
See Section 1.7 [Binary], page 8.
Chapter 16: Invoking sdiff 95
‘-t’
‘--expand-tabs’
Expand tabs to spaces in the output, to preserve the align-
ment of tabs in the input files. See Section 5.1 [Tabs],
page 37.
‘-v’
‘--version’
Output version information and then exit.
‘-w columns’
‘--width=columns’
Output at most columns (default 130) print columns per
line. See Section 2.4.1 [Side by Side Format], page 20. Note
that for historical reasons, this option is ‘-W’ in diff, ‘-w’ in
sdiff.
‘-W’
‘--ignore-all-space’
Ignore white space when comparing lines. See Section 1.2
[White Space], page 6. Note that for historical reasons, this
option is ‘-w’ in diff, ‘-W’ in sdiff.
96 Comparing and Merging Files with GNU diff and patch
Chapter 17: Standards conformance 97
17 Standards conformance
In a few cases, the gnu utilities’ default behavior is incompatible
with the posix standard. To suppress these incompatibilities, define
the POSIXLY_CORRECT environment variable. Unless you are checking for
posix conformance, you probably do not need to define POSIXLY_CORRECT.
Normally options and operands can appear in any order, and pro-
grams act as if all the options appear before any operands. For example,
‘diff lao tzu -C 2’ acts like ‘diff -C 2 lao tzu’, since ‘2’ is an option-
argument of ‘-C’. However, if the POSIXLY_CORRECT environment variable
is set, options must appear before operands, unless otherwise specified for
a particular command.
Newer versions of posix are occasionally incompatible with older ver-
sions. For example, older versions of posix allowed the command ‘diff
-c -10’ to have the same meaning as ‘diff -C 10’, but posix 1003.1-2001
‘diff’ no longer allows digit-string options like ‘-10’.
The gnu utilities normally conform to the version of posix that is
standard for your system. To cause them to conform to a different ver-
sion of posix, define the _POSIX2_VERSION environment variable to a
value of the form yyyymm specifying the year and month the standard
was adopted. Two values are currently supported for _POSIX2_VERSION:
‘199209’ stands for posix 1003.2-1992, and ‘200112’ stands for posix
1003.1-2001. For example, if you are running older software that assumes
an older version of posix and uses ‘diff -c -10’, you can work around
the compatibility problems by setting ‘_POSIX2_VERSION=199209’ in your
environment.
98 Comparing and Merging Files with GNU diff and patch
Chapter 18: Reporting Bugs 99
18 Reporting Bugs
If you think you have found a bug in gnu cmp, diff, diff3, or sdiff,
please report it by electronic mail to the GNU utilities bug report mailing
list [email protected]. Please send bug reports for gnu patch to
[email protected]. Send as precise a description of the problem as you
can, including the output of the ‘--version’ option and sample input files
that produce the bug, if applicable.
If you have a nontrivial fix for the bug, please send it as well. If you
have a patch, please send it too. It may simplify the maintainer’s job if
the patch is relative to a recent test release, which you can find in the
directory ftp://alpha.gnu.org/gnu/diffutils/.
100 Comparing and Merging Files with GNU diff and patch
Appendix A: Copying This Manual 101
2. VERBATIM COPYING
You may copy and distribute the Document in any medium, either
commercially or noncommercially, provided that this License, the
copyright notices, and the license notice saying this License applies
to the Document are reproduced in all copies, and that you add no
other conditions whatsoever to those of this License. You may not
use technical measures to obstruct or control the reading or further
copying of the copies you make or distribute. However, you may ac-
cept compensation in exchange for copies. If you distribute a large
enough number of copies you must also follow the conditions in sec-
tion 3.
You may also lend copies, under the same conditions stated above,
and you may publicly display copies.
3. COPYING IN QUANTITY
If you publish printed copies of the Document numbering more than
100, and the Document’s license notice requires Cover Texts, you
must enclose the copies in covers that carry, clearly and legibly, all
these Cover Texts: Front-Cover Texts on the front cover, and Back-
Cover Texts on the back cover. Both covers must also clearly and
legibly identify you as the publisher of these copies. The front cover
must present the full title with all words of the title equally prominent
and visible. You may add other material on the covers in addition.
Copying with changes limited to the covers, as long as they preserve
the title of the Document and satisfy these conditions, can be treated
as verbatim copying in other respects.
If the required texts for either cover are too voluminous to fit legibly,
you should put the first ones listed (as many as fit reasonably) on
the actual cover, and continue the rest onto adjacent pages.
If you publish or distribute Opaque copies of the Document num-
bering more than 100, you must either include a machine-readable
Transparent copy along with each Opaque copy, or state in or with
each Opaque copy a publicly-accessible computer-network location
containing a complete Transparent copy of the Document, free of
added material, which the general network-using public has access
to download anonymously at no charge using public-standard net-
work protocols. If you use the latter option, you must take reason-
ably prudent steps, when you begin distribution of Opaque copies
in quantity, to ensure that this Transparent copy will remain thus
accessible at the stated location until at least one year after the last
time you distribute an Opaque copy (directly or through your agents
or retailers) of that edition to the public.
104 Comparing and Merging Files with GNU diff and patch
It is requested, but not required, that you contact the authors of the
Document well before redistributing any large number of copies, to
give them a chance to provide you with an updated version of the
Document.
4. MODIFICATIONS
You may copy and distribute a Modified Version of the Document un-
der the conditions of sections 2 and 3 above, provided that you release
the Modified Version under precisely this License, with the Modified
Version filling the role of the Document, thus licensing distribution
and modification of the Modified Version to whoever possesses a copy
of it. In addition, you must do these things in the Modified Version:
A. Use in the Title Page (and on the covers, if any) a title distinct
from that of the Document, and from those of previous versions
(which should, if there were any, be listed in the History section
of the Document). You may use the same title as a previous
version if the original publisher of that version gives permission.
B. List on the Title Page, as authors, one or more persons or enti-
ties responsible for authorship of the modifications in the Mod-
ified Version, together with at least five of the principal authors
of the Document (all of its principal authors, if it has less than
five).
C. State on the Title page the name of the publisher of the Modified
Version, as the publisher.
D. Preserve all the copyright notices of the Document.
E. Add an appropriate copyright notice for your modifications ad-
jacent to the other copyright notices.
F. Include, immediately after the copyright notices, a license notice
giving the public permission to use the Modified Version under
the terms of this License, in the form shown in the Addendum
below.
G. Preserve in that license notice the full lists of Invariant Sections
and required Cover Texts given in the Document’s license notice.
H. Include an unaltered copy of this License.
I. Preserve the section entitled “History”, and its title, and add
to it an item stating at least the title, year, new authors, and
publisher of the Modified Version as given on the Title Page. If
there is no section entitled “History” in the Document, create
one stating the title, year, authors, and publisher of the Doc-
ument as given on its Title Page, then add an item describing
the Modified Version as stated in the previous sentence.
Appendix A: Copying This Manual 105
5. COMBINING DOCUMENTS
You may combine the Document with other documents released un-
der this License, under the terms defined in section 4 above for mod-
ified versions, provided that you include in the combination all of
the Invariant Sections of all of the original documents, unmodified,
and list them all as Invariant Sections of your combined work in its
license notice.
The combined work need only contain one copy of this License, and
multiple identical Invariant Sections may be replaced with a single
copy. If there are multiple Invariant Sections with the same name
but different contents, make the title of each such section unique
by adding at the end of it, in parentheses, the name of the original
author or publisher of that section if known, or else a unique number.
Make the same adjustment to the section titles in the list of Invariant
Sections in the license notice of the combined work.
In the combination, you must combine any sections entitled “His-
tory” in the various original documents, forming one section enti-
tled “History”; likewise combine any sections entitled “Acknowledg-
ments”, and any sections entitled “Dedications”. You must delete
all sections entitled “Endorsements.”
6. COLLECTIONS OF DOCUMENTS
You may make a collection consisting of the Document and other
documents released under this License, and replace the individual
copies of this License in the various documents with a single copy
that is included in the collection, provided that you follow the rules
of this License for verbatim copying of each of the documents in all
other respects.
You may extract a single document from such a collection, and dis-
tribute it individually under this License, provided you insert a copy
of this License into the extracted document, and follow this License
in all other respects regarding verbatim copying of that document.
7. AGGREGATION WITH INDEPENDENT WORKS
A compilation of the Document or its derivatives with other sepa-
rate and independent documents or works, in or on a volume of a
storage or distribution medium, does not as a whole count as a Mod-
ified Version of the Document, provided no compilation copyright is
claimed for the compilation. Such a compilation is called an “ag-
gregate”, and this License does not apply to the other self-contained
works thus compiled with the Document, on account of their being
thus compiled, if they are not themselves derivative works of the
Document.
Appendix A: Copying This Manual 107
History
This section gives the history of the modifications made to the manual
by the publisher, as required by the GNU Free Documentation License.
5/2002 "Comparing and Merging Files" (original edition)
D. MacKenzie, P. Eggert, R. Stallman
Publisher: Free Software Foundation.
12/2002 "Comparing and Merging Files with GNU diff and patch"
Edited for publication by Brian Gough
Publisher: Network Theory Ltd.
Published under different title, as given above. Added
publisher’s preface. Minor modifications for publication
as a printed book: reformatted several examples to fit
smaller page width, removed some long urls to improve line-
breaking. Replaced chapter "Future Projects" by "Report-
ing Bugs". Added this "History" section.
The source code for the original version of this document is available from
ftp.gnu.org/gnu/diffutils/ in the file ‘diffutils-2.8.1.tar.gz’.
The source code for this version is available from http://www.network-
theory.co.uk/diff/manual/src/. A complete set of differences can be
obtained from the same location.
110 Comparing and Merging Files with GNU diff and patch
Appendix B: Index 111
Appendix B Index
+ D
diagnostics from patch . . . . . . . . . . 61
‘+-’ output format . . . . . . . . . . . . . . 16
diff invocation . . . . . . . . . . . . . . . . . 75
diff merging . . . . . . . . . . . . . . . . . . . . 51
< diff options . . . . . . . . . . . . . . . . . . . . 75
diff sample input . . . . . . . . . . . . . . . 11
‘<’ output format. . . . . . . . . . . . . . . . 12
diff3 hunks . . . . . . . . . . . . . . . . . . . . 42
‘<<<<<<<’ for marking conflicts . . . 46
diff3 invocation . . . . . . . . . . . . . . . . 83
diff3 options . . . . . . . . . . . . . . . . . . . 83
A diff3 sample input . . . . . . . . . . . . .
directories and patch . . . . . . . . . . . .
41
59
aligning tab stops . . . . . . . . . . . . . . . 37 dry runs for patch . . . . . . . . . . . . . . 56
alternate file names . . . . . . . . . . . . . 19
E
B ed script output format . . . . . . . . . . 21
backup file names . . . . . . . . . . . . . . . 60 EDITOR . . . . . . . . . . . . . . . . . . . . . . . . . 52
backup file strategy . . . . . . . . . . . . . 60 empty files, removing . . . . . . . . . . . . 57
binary file diff . . . . . . . . . . . . . . . . . . . . 8 exabyte, definition of . . . . . . . . . . . . 73
blank and tab difference suppression exbibyte, definition of . . . . . . . . . . . 73
............................ 6
blank line difference suppression . . . 7
brief difference reports . . . . . . . . . . . . 8 F
bug reports . . . . . . . . . . . . . . . . . . . . . 99 FDL, GNU Free Documentation
License . . . . . . . . . . . . . . . . . . . . 101
C file name alternates . . . . . . . . . . . . . 19
format of diff output . . . . . . . . . . . 11
C function headings . . . . . . . . . . . . . 19 format of diff3 output . . . . . . . . . . 41
C if-then-else output format . . . . . 24 formats for if-then-else line groups
case difference suppression . . . . . . . . 7 . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
ClearCase . . . . . . . . . . . . . . . . . . . . . . 54 forward ed script output format . . 23
cmp invocation . . . . . . . . . . . . . . . . . . 71 full lines . . . . . . . . . . . . . . . . . . . . . . . . 33
cmp options . . . . . . . . . . . . . . . . . . . . . 71 function headings, C . . . . . . . . . . . . 19
columnar output . . . . . . . . . . . . . . . . 19 fuzz factor when patching . . . . . . . . 55
112 Comparing and Merging Files with GNU diff and patch
G M
gibibyte, definition of . . . . . . . . . . . . 72 mebibyte, definition of . . . . . . . . . . . 72
gigabyte, definition of . . . . . . . . . . . 72 megabyte, definition of . . . . . . . . . . 72
merge commands . . . . . . . . . . . . . . . . 52
merged diff3 format . . . . . . . . . . . . 48
H merged output format . . . . . . . . . . .
merging from a common ancestor
24
headings . . . . . . . . . . . . . . . . . . . . . . . . 18 ........................... 45
hunks . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 merging interactively . . . . . . . . . . . . 52
hunks for diff3 . . . . . . . . . . . . . . . . . 42 messages from patch . . . . . . . . . . . . 61
multiple patches . . . . . . . . . . . . . . . . 58
I N
if-then-else output format . . . . . . . . 24
newline treatment by diff . . . . . . . 33
ifdef output format . . . . . . . . . . . . 24
normal output format . . . . . . . . . . . 12
imperfect patch application . . . . . . 54
incomplete line merging . . . . . . . . . 48
incomplete lines . . . . . . . . . . . . . . . . . 33 O
inexact patches . . . . . . . . . . . . . . . . . 55 options for cmp . . . . . . . . . . . . . . . . . . 71
inhibit messages from patch . . . . . 62 options for diff . . . . . . . . . . . . . . . . . 75
interactive merging . . . . . . . . . . . . . . 51 options for diff3. . . . . . . . . . . . . . . . 83
introduction . . . . . . . . . . . . . . . . . . . . . 5 options for patch. . . . . . . . . . . . . . . . 87
intuiting file names from patches options for sdiff. . . . . . . . . . . . . . . . 93
. . . . . . . . . . . . . . . . . . . . . . . . . . . 58 output formats . . . . . . . . . . . . . . . . . . 11
invoking cmp . . . . . . . . . . . . . . . . . . . . 71 overlap . . . . . . . . . . . . . . . . . . . . . . . . . 45
invoking diff . . . . . . . . . . . . . . . . . . . 75 overlapping change, selection of . . 45
invoking diff3 . . . . . . . . . . . . . . . . . . 83 overview of diff and patch . . . . . . . 3
invoking patch . . . . . . . . . . . . . . . . . . 87
invoking sdiff . . . . . . . . . . . . . . . . . . 93
P
paginating diff output . . . . . . . . . . 37
K patch consumer tips . . . . . . . . . . . . . 67
patch input format . . . . . . . . . . . . . . 53
keyboard input to patch . . . . . . . . . 62 patch invocation . . . . . . . . . . . . . . . . 87
kibibyte, definition of . . . . . . . . . . . . 72 patch messages and questions . . . . 61
kilobyte, definition of . . . . . . . . . . . . 72 patch options . . . . . . . . . . . . . . . . . . . 87
patch producer tips . . . . . . . . . . . . . 67
patch, common mistakes . . . . . . . . . 68
L PATCH_GET . . . . . . . . . . . . . . . . . . . . . . 54
PATCH_VERSION_CONTROL . . . . . . . . . 60
LC_COLLATE . . . . . . . . . . . . . . . . . . . . . 35 patches, shrinking . . . . . . . . . . . . . . . 69
LC_NUMERIC . . . . . . . . . . . . . . . . . . . . . 27 patching directories . . . . . . . . . . . . . 59
LC_TIME . . . . . . . . . . . . . . . . . . . . . . . . 14 pebibyte, definition of . . . . . . . . . . . 72
line formats . . . . . . . . . . . . . . . . . . . . . 27 performance of diff . . . . . . . . . . . . . 39
line group formats . . . . . . . . . . . . . . . 24 petabyte, definition of . . . . . . . . . . . 72
Appendix B: Index 113
posix . . . . . . . . . . . . . . . . . . . . . . . 63, 97 T
POSIXLY_CORRECT . . . . . . . . . . . . 63, 97 tab and blank difference suppression
............................ 6
Q tab stop alignment . . . . . . . . . . . . . . 37
tebibyte, definition of . . . . . . . . . . . . 72
quoting style . . . . . . . . . . . . . . . . . . . . 62 terabyte, definition of. . . . . . . . . . . . 72
QUOTING_STYLE. . . . . . . . . . . . . . . . . . 63 testing patch . . . . . . . . . . . . . . . . . . . 56
text versus binary diff . . . . . . . . . . . . 8
time stamp format, context diffs . . 14
R time stamp format, unified diffs . . 17
rcs . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54 time stamps on patched files . . . . . 57
rcs script output format. . . . . . . . . 23 traditional patch . . . . . . . . . . . . . . . . 63
regular expression matching headings two-column output . . . . . . . . . . . . . . 19
. . . . . . . . . . . . . . . . . . . . . . . . . . . 18
regular expression suppression . . . . . 7
reject file names . . . . . . . . . . . . . . . . . 61 U
removing empty files . . . . . . . . . . . . 57 unified output format. . . . . . . . . . . . 16
reporting bugs . . . . . . . . . . . . . . . . . . 99 unmerged change. . . . . . . . . . . . . . . . 45
reversed patches . . . . . . . . . . . . . . . . 55
revision control . . . . . . . . . . . . . . . . . 54
V
S verbose messages from patch . . . . . 62
version control . . . . . . . . . . . . . . . . . . 54
sample input for diff . . . . . . . . . . . 11 VERSION_CONTROL . . . . . . . . . . . . 54, 60
sample input for diff3 . . . . . . . . . . 41
sccs . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
script output formats . . . . . . . . . . . . 21 W
sdiff invocation . . . . . . . . . . . . . . . . 93
white space in patches . . . . . . . . . . . 54
sdiff options . . . . . . . . . . . . . . . . . . . 93
sdiff output format . . . . . . . . . . . . 51
section headings. . . . . . . . . . . . . . . . . 18
side by side . . . . . . . . . . . . . . . . . . . . . 19
Y
side by side format . . . . . . . . . . . . . . 20 yottabyte, definition of . . . . . . . . . . 73
SIMPLE_BACKUP_SUFFIX . . . . . . . . . . 60
specified headings . . . . . . . . . . . . . . . 18
summarizing which files differ . . . . . 8 Z
System V diff3 compatibility . . . . 49 zettabyte, definition of . . . . . . . . . . . 73