Imported Upstream version 3.3
[platform/upstream/diffutils.git] / doc / diffutils.texi
1 \input texinfo @c -*-texinfo-*-
2 @comment %**start of header
3 @setfilename diffutils.info
4 @include version.texi
5 @settitle Comparing and Merging Files
6 @syncodeindex vr cp
7 @setchapternewpage odd
8 @comment %**end of header
9 @copying
10 This manual is for GNU Diffutils
11 (version @value{VERSION}, @value{UPDATED}),
12 and documents the @acronym{GNU} @command{diff}, @command{diff3},
13 @command{sdiff}, and @command{cmp} commands for showing the
14 differences between files and the @acronym{GNU} @command{patch} command for
15 using their output to update files.
16
17 Copyright @copyright{} 1992-1994, 1998, 2001-2002, 2004, 2006, 2009-2013 Free
18 Software Foundation, Inc.
19
20 @quotation
21 Permission is granted to copy, distribute and/or modify this document
22 under the terms of the GNU Free Documentation License, Version 1.3 or
23 any later version published by the Free Software Foundation; with no
24 Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts.
25 A copy of the license is included in the section entitled
26 ``@acronym{GNU} Free Documentation License.''
27 @end quotation
28 @end copying
29
30 @c Debian install-info (up through at least version 1.9.20) uses only the
31 @c first dircategory.  Put this one first, as it is more useful in practice.
32 @dircategory Individual utilities
33 @direntry
34 * cmp: (diffutils)Invoking cmp.                 Compare 2 files byte by byte.
35 * diff: (diffutils)Invoking diff.               Compare 2 files line by line.
36 * diff3: (diffutils)Invoking diff3.             Compare 3 files line by line.
37 * patch: (diffutils)Invoking patch.             Apply a patch to a file.
38 * sdiff: (diffutils)Invoking sdiff.             Merge 2 files side-by-side.
39 @end direntry
40
41 @dircategory Text creation and manipulation
42 @direntry
43 * Diffutils: (diffutils).       Comparing and merging files.
44 @end direntry
45
46 @titlepage
47 @title Comparing and Merging Files
48 @subtitle for Diffutils @value{VERSION} and @code{patch} 2.5.4
49 @subtitle @value{UPDATED}
50 @author David MacKenzie, Paul Eggert, and Richard Stallman
51 @page
52 @vskip 0pt plus 1filll
53 @insertcopying
54 @end titlepage
55
56 @shortcontents
57 @contents
58
59 @ifnottex
60 @node Top
61 @top Comparing and Merging Files
62
63 @insertcopying
64 @end ifnottex
65
66 @menu
67 * Overview::              Preliminary information.
68 * Comparison::            What file comparison means.
69
70 * Output Formats::        Formats for two-way difference reports.
71 * Incomplete Lines::      Lines that lack trailing newlines.
72 * Comparing Directories:: Comparing files and directories.
73 * Adjusting Output::      Making @command{diff} output prettier.
74 * diff Performance::      Making @command{diff} smarter or faster.
75
76 * Comparing Three Files:: Formats for three-way difference reports.
77 * diff3 Merging::         Merging from a common ancestor.
78
79 * Interactive Merging::   Interactive merging with @command{sdiff}.
80
81 * Merging with patch::    Using @command{patch} to change old files into new ones.
82 * Making Patches::        Tips for making and using patch distributions.
83
84 * Invoking cmp::          Compare two files byte by byte.
85 * Invoking diff::         Compare two files line by line.
86 * Invoking diff3::        Compare three files line by line.
87 * Invoking patch::        Apply a diff file to an original.
88 * Invoking sdiff::        Side-by-side merge of file differences.
89
90 * Standards conformance:: Conformance to the @acronym{POSIX} standard.
91 * Projects::              If you've found a bug or other shortcoming.
92
93 * Copying This Manual::   How to make copies of this manual.
94 * Translations::          Available translations of this manual.
95 * Index::                 Index.
96 @end menu
97
98 @node Overview
99 @unnumbered Overview
100 @cindex overview of @command{diff} and @command{patch}
101
102 Computer users often find occasion to ask how two files differ.  Perhaps
103 one file is a newer version of the other file.  Or maybe the two files
104 started out as identical copies but were changed by different people.
105
106 You can use the @command{diff} command to show differences between two
107 files, or each corresponding file in two directories.  @command{diff}
108 outputs differences between files line by line in any of several
109 formats, selectable by command line options.  This set of differences is
110 often called a @dfn{diff} or @dfn{patch}.  For files that are identical,
111 @command{diff} normally produces no output; for binary (non-text) files,
112 @command{diff} normally reports only that they are different.
113
114 You can use the @command{cmp} command to show the byte and line numbers
115 where two files differ.  @command{cmp} can also show all the bytes
116 that differ between the two files, side by side.  A way to compare
117 two files character by character is the Emacs command @kbd{M-x
118 compare-windows}.  @xref{Other Window, , Other Window, emacs, The @acronym{GNU}
119 Emacs Manual}, for more information on that command.
120
121 You can use the @command{diff3} command to show differences among three
122 files.  When two people have made independent changes to a common
123 original, @command{diff3} can report the differences between the original
124 and the two changed versions, and can produce a merged file that
125 contains both persons' changes together with warnings about conflicts.
126
127 You can use the @command{sdiff} command to merge two files interactively.
128
129 You can use the set of differences produced by @command{diff} to distribute
130 updates to text files (such as program source code) to other people.
131 This method is especially useful when the differences are small compared
132 to the complete files.  Given @command{diff} output, you can use the
133 @command{patch} program to update, or @dfn{patch}, a copy of the file.  If you
134 think of @command{diff} as subtracting one file from another to produce
135 their difference, you can think of @command{patch} as adding the difference
136 to one file to reproduce the other.
137
138 This manual first concentrates on making diffs, and later shows how to
139 use diffs to update files.
140
141 @acronym{GNU} @command{diff} was written by Paul Eggert, Mike Haertel,
142 David Hayes, Richard Stallman, and Len Tower.  Wayne Davison designed and
143 implemented the unified output format.  The basic algorithm is described
144 by Eugene W. Myers in ``An O(ND) Difference Algorithm and its Variations'',
145 @cite{Algorithmica} Vol.@: 1 No.@: 2, 1986, pp.@: 251--266; and in ``A File
146 Comparison Program'', Webb Miller and Eugene W. Myers,
147 @cite{Software---Practice and Experience} Vol.@: 15 No.@: 11, 1985,
148 pp.@: 1025--1040.
149 @c From: "Gene Myers" <gene@cs.arizona.edu>
150 @c They are about the same basic algorithm; the Algorithmica
151 @c paper gives a rigorous treatment and the sub-algorithm for
152 @c delivering scripts and should be the primary reference, but
153 @c both should be mentioned.
154 The algorithm was independently discovered as described by E. Ukkonen in
155 ``Algorithms for Approximate String Matching'',
156 @cite{Information and Control} Vol.@: 64, 1985, pp.@: 100--118.
157 @c From: "Gene Myers" <gene@cs.arizona.edu>
158 @c Date: Wed, 29 Sep 1993 08:27:55 MST
159 @c Ukkonen should be given credit for also discovering the algorithm used
160 @c in GNU diff.
161 Unless the @option{--minimal} option is used, @command{diff} uses a
162 heuristic by Paul Eggert that limits the cost to @math{O(N^1.5 log N)}
163 at the price of producing suboptimal output for large inputs with many
164 differences.  Related algorithms are surveyed by Alfred V. Aho in
165 section 6.3 of ``Algorithms for Finding Patterns in Strings'',
166 @cite{Handbook of Theoretical Computer Science} (Jan Van Leeuwen,
167 ed.), Vol.@: A, @cite{Algorithms and Complexity}, Elsevier/MIT Press,
168 1990, pp.@: 255--300.
169
170 @acronym{GNU} @command{diff3} was written by Randy Smith.  @acronym{GNU}
171 @command{sdiff} was written by Thomas Lord.  @acronym{GNU} @command{cmp}
172 was written by Torbj@"orn Granlund and David MacKenzie.
173
174 @acronym{GNU} @command{patch} was written mainly by Larry Wall and Paul Eggert;
175 several @acronym{GNU} enhancements were contributed by Wayne Davison and
176 David MacKenzie.  Parts of this manual are adapted from a manual page
177 written by Larry Wall, with his permission.
178
179 @node Comparison
180 @chapter What Comparison Means
181 @cindex introduction
182
183 There are several ways to think about the differences between two files.
184 One way to think of the differences is as a series of lines that were
185 deleted from, inserted in, or changed in one file to produce the other
186 file.  @command{diff} compares two files line by line, finds groups of
187 lines that differ, and reports each group of differing lines.  It can
188 report the differing lines in several formats, which have different
189 purposes.
190
191 @acronym{GNU} @command{diff} can show whether files are different
192 without detailing the differences.  It also provides ways to suppress
193 certain kinds of differences that are not important to you.  Most
194 commonly, such differences are changes in the amount of white space
195 between words or lines.  @command{diff} also provides ways to suppress
196 differences in alphabetic case or in lines that match a regular
197 expression that you provide.  These options can accumulate; for
198 example, you can ignore changes in both white space and alphabetic
199 case.
200
201 Another way to think of the differences between two files is as a
202 sequence of pairs of bytes that can be either identical or
203 different.  @command{cmp} reports the differences between two files
204 byte by byte, instead of line by line.  As a result, it is often
205 more useful than @command{diff} for comparing binary files.  For text
206 files, @command{cmp} is useful mainly when you want to know only whether
207 two files are identical, or whether one file is a prefix of the other.
208
209 To illustrate the effect that considering changes byte by byte
210 can have compared with considering them line by line, think of what
211 happens if a single newline character is added to the beginning of a
212 file.  If that file is then compared with an otherwise identical file
213 that lacks the newline at the beginning, @command{diff} will report that a
214 blank line has been added to the file, while @command{cmp} will report that
215 almost every byte of the two files differs.
216
217 @command{diff3} normally compares three input files line by line, finds
218 groups of lines that differ, and reports each group of differing lines.
219 Its output is designed to make it easy to inspect two different sets of
220 changes to the same file.
221
222 @menu
223 * Hunks::             Groups of differing lines.
224 * White Space::       Suppressing differences in white space.
225 * Blank Lines::       Suppressing differences whose lines are all blank.
226 * Specified Lines::   Suppressing differences whose lines all match a pattern.
227 * Case Folding::      Suppressing differences in alphabetic case.
228 * Brief::             Summarizing which files are different.
229 * Binary::            Comparing binary files or forcing text comparisons.
230 @end menu
231
232 @node Hunks
233 @section Hunks
234 @cindex hunks
235
236 When comparing two files, @command{diff} finds sequences of lines common to
237 both files, interspersed with groups of differing lines called
238 @dfn{hunks}.  Comparing two identical files yields one sequence of
239 common lines and no hunks, because no lines differ.  Comparing two
240 entirely different files yields no common lines and one large hunk that
241 contains all lines of both files.  In general, there are many ways to
242 match up lines between two given files.  @command{diff} tries to minimize
243 the total hunk size by finding large sequences of common lines
244 interspersed with small hunks of differing lines.
245
246 For example, suppose the file @file{F} contains the three lines
247 @samp{a}, @samp{b}, @samp{c}, and the file @file{G} contains the same
248 three lines in reverse order @samp{c}, @samp{b}, @samp{a}.  If
249 @command{diff} finds the line @samp{c} as common, then the command
250 @samp{diff F G} produces this output:
251
252 @example
253 1,2d0
254 < a
255 < b
256 3a2,3
257 > b
258 > a
259 @end example
260
261 @noindent
262 But if @command{diff} notices the common line @samp{b} instead, it produces
263 this output:
264
265 @example
266 1c1
267 < a
268 ---
269 > c
270 3c3
271 < c
272 ---
273 > a
274 @end example
275
276 @noindent
277 It is also possible to find @samp{a} as the common line.  @command{diff}
278 does not always find an optimal matching between the files; it takes
279 shortcuts to run faster.  But its output is usually close to the
280 shortest possible.  You can adjust this tradeoff with the
281 @option{--minimal} (@option{-d}) option (@pxref{diff Performance}).
282
283 @node White Space
284 @section Suppressing Differences in Blank and Tab Spacing
285 @cindex blank and tab difference suppression
286 @cindex tab and blank difference suppression
287
288 The @option{--ignore-tab-expansion} (@option{-E}) option ignores the
289 distinction between tabs and spaces on input.  A tab is considered to be
290 equivalent to the number of spaces to the next tab stop (@pxref{Tabs}).
291
292 The @option{--ignore-trailing-space} (@option{-Z}) option ignores white
293 space at line end.
294
295 The @option{--ignore-space-change} (@option{-b}) option is stronger than
296 @option{-E} and @option{-Z} combined.
297 It ignores white space at line end, and considers all other sequences of
298 one or more white space characters within a line to be equivalent.  With this
299 option, @command{diff} considers the following two lines to be equivalent,
300 where @samp{$} denotes the line end:
301
302 @example
303 Here lyeth  muche rychnesse  in lytell space.   -- John Heywood$
304 Here lyeth muche rychnesse in lytell space. -- John Heywood   $
305 @end example
306
307 The @option{--ignore-all-space} (@option{-w}) option is stronger still.
308 It ignores differences even if one line has white space where
309 the other line has none.  @dfn{White space} characters include
310 tab, vertical tab, form feed, carriage return, and space;
311 some locales may define additional characters to be white space.
312 With this option, @command{diff} considers the
313 following two lines to be equivalent, where @samp{$} denotes the line
314 end and @samp{^M} denotes a carriage return:
315
316 @example
317 Here lyeth  muche  rychnesse in lytell space.--  John Heywood$
318   He relyeth much erychnes  seinly tells pace.  --John Heywood   ^M$
319 @end example
320
321 For many other programs newline is also a white space character, but
322 @command{diff} is a line-oriented program and a newline character
323 always ends a line.  Hence the @option{-w} or
324 @option{--ignore-all-space} option does not ignore newline-related
325 changes; it ignores only other white space changes.
326
327 @node Blank Lines
328 @section Suppressing Differences Whose Lines Are All Blank
329 @cindex blank line difference suppression
330
331 The @option{--ignore-blank-lines} (@option{-B}) option ignores changes
332 that consist entirely of blank lines.  With this option, for example, a
333 file containing
334 @example
335 1.  A point is that which has no part.
336
337 2.  A line is breadthless length.
338 -- Euclid, The Elements, I
339 @end example
340 @noindent
341 is considered identical to a file containing
342 @example
343 1.  A point is that which has no part.
344 2.  A line is breadthless length.
345
346
347 -- Euclid, The Elements, I
348 @end example
349
350 Normally this option affects only lines that are completely empty, but
351 if you also specify an option that ignores trailing spaces,
352 lines are also affected if they look empty but contain white space.
353 In other words, @option{-B} is equivalent to @samp{-I '^$'} by
354 default, but it is equivalent to @option{-I '^[[:space:]]*$'} if
355 @option{-b}, @option{-w} or @option{-Z} is also specified.
356
357 @node Specified Lines
358 @section Suppressing Differences Whose Lines All Match a Regular Expression
359 @cindex regular expression suppression
360
361 To ignore insertions and deletions of lines that match a
362 @command{grep}-style regular expression, use the
363 @option{--ignore-matching-lines=@var{regexp}} (@option{-I @var{regexp}}) option.
364 You should escape
365 regular expressions that contain shell metacharacters to prevent the
366 shell from expanding them.  For example, @samp{diff -I '^[[:digit:]]'} ignores
367 all changes to lines beginning with a digit.
368
369 However, @option{-I} only ignores the insertion or deletion of lines that
370 contain the regular expression if every changed line in the hunk---every
371 insertion and every deletion---matches the regular expression.  In other
372 words, for each nonignorable change, @command{diff} prints the complete set
373 of changes in its vicinity, including the ignorable ones.
374
375 You can specify more than one regular expression for lines to ignore by
376 using more than one @option{-I} option.  @command{diff} tries to match each
377 line against each regular expression.
378
379 @node Case Folding
380 @section Suppressing Case Differences
381 @cindex case difference suppression
382
383 @acronym{GNU} @command{diff} can treat lower case letters as
384 equivalent to their upper case counterparts, so that, for example, it
385 considers @samp{Funky Stuff}, @samp{funky STUFF}, and @samp{fUNKy
386 stuFf} to all be the same.  To request this, use the @option{-i} or
387 @option{--ignore-case} option.
388
389 @node Brief
390 @section Summarizing Which Files Differ
391 @cindex summarizing which files differ
392 @cindex brief difference reports
393
394 When you only want to find out whether files are different, and you
395 don't care what the differences are, you can use the summary output
396 format.  In this format, instead of showing the differences between the
397 files, @command{diff} simply reports whether files differ.  The
398 @option{--brief} (@option{-q}) option selects this output format.
399
400 This format is especially useful when comparing the contents of two
401 directories.  It is also much faster than doing the normal line by line
402 comparisons, because @command{diff} can stop analyzing the files as soon as
403 it knows that there are any differences.
404
405 You can also get a brief indication of whether two files differ by using
406 @command{cmp}.  For files that are identical, @command{cmp} produces no
407 output.  When the files differ, by default, @command{cmp} outputs the byte
408 and line number where the first difference occurs, or reports that one
409 file is a prefix of the other.  You can use
410 the @option{-s}, @option{--quiet}, or @option{--silent} option to
411 suppress that information, so that @command{cmp}
412 produces no output and reports whether the files differ using only its
413 exit status (@pxref{Invoking cmp}).
414
415 @c Fix this.
416 Unlike @command{diff}, @command{cmp} cannot compare directories; it can only
417 compare two files.
418
419 @node Binary
420 @section Binary Files and Forcing Text Comparisons
421 @cindex binary file diff
422 @cindex text versus binary diff
423
424 If @command{diff} thinks that either of the two files it is comparing is
425 binary (a non-text file), it normally treats that pair of files much as
426 if the summary output format had been selected (@pxref{Brief}), and
427 reports only that the binary files are different.  This is because line
428 by line comparisons are usually not meaningful for binary files.
429
430 @command{diff} determines whether a file is text or binary by checking the
431 first few bytes in the file; the exact number of bytes is system
432 dependent, but it is typically several thousand.  If every byte in
433 that part of the file is non-null, @command{diff} considers the file to be
434 text; otherwise it considers the file to be binary.
435
436 Sometimes you might want to force @command{diff} to consider files to be
437 text.  For example, you might be comparing text files that contain
438 null characters; @command{diff} would erroneously decide that those are
439 non-text files.  Or you might be comparing documents that are in a
440 format used by a word processing system that uses null characters to
441 indicate special formatting.  You can force @command{diff} to consider all
442 files to be text files, and compare them line by line, by using the
443 @option{--text} (@option{-a}) option.  If the files you compare using this
444 option do not in fact contain text, they will probably contain few
445 newline characters, and the @command{diff} output will consist of hunks
446 showing differences between long lines of whatever characters the files
447 contain.
448
449 You can also force @command{diff} to report only whether files differ
450 (but not how).  Use the @option{--brief} (@option{-q}) option for
451 this.
452
453 Normally, differing binary files count as trouble because the
454 resulting @command{diff} output does not capture all the differences.
455 This trouble causes @command{diff} to exit with status 2.  However,
456 this trouble cannot occur with the @option{--text} (@option{-a})
457 option, or with the @option{--brief} (@option{-q}) option, as these
458 options both cause @command{diff} to generate a form of output that
459 represents differences as requested.
460
461 In operating systems that distinguish between text and binary files,
462 @command{diff} normally reads and writes all data as text.  Use the
463 @option{--binary} option to force @command{diff} to read and write binary
464 data instead.  This option has no effect on a @acronym{POSIX}-compliant system
465 like @acronym{GNU} or traditional Unix.  However, many personal computer
466 operating systems represent the end of a line with a carriage return
467 followed by a newline.  On such systems, @command{diff} normally ignores
468 these carriage returns on input and generates them at the end of each
469 output line, but with the @option{--binary} option @command{diff} treats
470 each carriage return as just another input character, and does not
471 generate a carriage return at the end of each output line.  This can be
472 useful when dealing with non-text files that are meant to be
473 interchanged with @acronym{POSIX}-compliant systems.
474
475 The @option{--strip-trailing-cr} causes @command{diff} to treat input
476 lines that end in carriage return followed by newline as if they end
477 in plain newline.  This can be useful when comparing text that is
478 imperfectly imported from many personal computer operating systems.
479 This option affects how lines are read, which in turn affects how they
480 are compared and output.
481
482 If you want to compare two files byte by byte, you can use the
483 @command{cmp} program with the @option{--verbose} (@option{-l})
484 option to show the values of each differing byte in the two files.
485 With @acronym{GNU} @command{cmp}, you can also use the @option{-b} or
486 @option{--print-bytes} option to show the @acronym{ASCII} representation of
487 those bytes.  @xref{Invoking cmp}, for more information.
488
489 If @command{diff3} thinks that any of the files it is comparing is binary
490 (a non-text file), it normally reports an error, because such
491 comparisons are usually not useful.  @command{diff3} uses the same test as
492 @command{diff} to decide whether a file is binary.  As with @command{diff}, if
493 the input files contain a few non-text bytes but otherwise are like
494 text files, you can force @command{diff3} to consider all files to be text
495 files and compare them line by line by using the @option{-a} or
496 @option{--text} option.
497
498 @node Output Formats
499 @chapter @command{diff} Output Formats
500 @cindex output formats
501 @cindex format of @command{diff} output
502
503 @command{diff} has several mutually exclusive options for output format.
504 The following sections describe each format, illustrating how
505 @command{diff} reports the differences between two sample input files.
506
507 @menu
508 * Sample diff Input:: Sample @command{diff} input files for examples.
509 * Context::           Showing differences with the surrounding text.
510 * Side by Side::      Showing differences in two columns.
511 * Normal::            Showing differences without surrounding text.
512 * Scripts::           Generating scripts for other programs.
513 * If-then-else::      Merging files with if-then-else.
514 @end menu
515
516 @node Sample diff Input
517 @section Two Sample Input Files
518 @cindex @command{diff} sample input
519 @cindex sample input for @command{diff}
520
521 Here are two sample files that we will use in numerous examples to
522 illustrate the output of @command{diff} and how various options can change
523 it.
524
525 This is the file @file{lao}:
526
527 @example
528 The Way that can be told of is not the eternal Way;
529 The name that can be named is not the eternal name.
530 The Nameless is the origin of Heaven and Earth;
531 The Named is the mother of all things.
532 Therefore let there always be non-being,
533   so we may see their subtlety,
534 And let there always be being,
535   so we may see their outcome.
536 The two are the same,
537 But after they are produced,
538   they have different names.
539 @end example
540
541 This is the file @file{tzu}:
542
543 @example
544 The Nameless is the origin of Heaven and Earth;
545 The named is the mother of all things.
546
547 Therefore let there always be non-being,
548   so we may see their subtlety,
549 And let there always be being,
550   so we may see their outcome.
551 The two are the same,
552 But after they are produced,
553   they have different names.
554 They both may be called deep and profound.
555 Deeper and more profound,
556 The door of all subtleties!
557 @end example
558
559 In this example, the first hunk contains just the first two lines of
560 @file{lao}, the second hunk contains the fourth line of @file{lao}
561 opposing the second and third lines of @file{tzu}, and the last hunk
562 contains just the last three lines of @file{tzu}.
563
564 @node Context
565 @section Showing Differences in Their Context
566 @cindex context output format
567 @cindex @samp{!} output format
568
569 Usually, when you are looking at the differences between files, you will
570 also want to see the parts of the files near the lines that differ, to
571 help you understand exactly what has changed.  These nearby parts of the
572 files are called the @dfn{context}.
573
574 @acronym{GNU} @command{diff} provides two output formats that show context
575 around the differing lines: @dfn{context format} and @dfn{unified
576 format}.  It can optionally show in which function or section of the
577 file the differing lines are found.
578
579 If you are distributing new versions of files to other people in the
580 form of @command{diff} output, you should use one of the output formats
581 that show context so that they can apply the diffs even if they have
582 made small changes of their own to the files.  @command{patch} can apply
583 the diffs in this case by searching in the files for the lines of
584 context around the differing lines; if those lines are actually a few
585 lines away from where the diff says they are, @command{patch} can adjust
586 the line numbers accordingly and still apply the diff correctly.
587 @xref{Imperfect}, for more information on using @command{patch} to apply
588 imperfect diffs.
589
590 @menu
591 * Context Format::  An output format that shows surrounding lines.
592 * Unified Format::  A more compact output format that shows context.
593 * Sections::        Showing which sections of the files differences are in.
594 * Alternate Names:: Showing alternate file names in context headers.
595 @end menu
596
597 @node Context Format
598 @subsection Context Format
599
600 The context output format shows several lines of context around the
601 lines that differ.  It is the standard format for distributing updates
602 to source code.
603
604 To select this output format, use the
605 @option{--context@r{[}=@var{lines}@r{]}} (@option{-C @var{lines}})
606 or @option{-c} option.  The
607 argument @var{lines} that some of these options take is the number of
608 lines of context to show.  If you do not specify @var{lines}, it
609 defaults to three.  For proper operation, @command{patch} typically needs
610 at least two lines of context.
611
612 @menu
613 * Example Context::  Sample output in context format.
614 * Less Context::     Another sample with less context.
615 * Detailed Context:: A detailed description of the context output format.
616 @end menu
617
618 @node Example Context
619 @subsubsection An Example of Context Format
620
621 Here is the output of @samp{diff -c lao tzu} (@pxref{Sample diff Input},
622 for the complete contents of the two files).  Notice that up to three
623 lines that are not different are shown around each line that is
624 different; they are the context lines.  Also notice that the first two
625 hunks have run together, because their contents overlap.
626
627 @example
628 *** lao 2002-02-21 23:30:39.942229878 -0800
629 --- tzu 2002-02-21 23:30:50.442260588 -0800
630 ***************
631 *** 1,7 ****
632 - The Way that can be told of is not the eternal Way;
633 - The name that can be named is not the eternal name.
634   The Nameless is the origin of Heaven and Earth;
635 ! The Named is the mother of all things.
636   Therefore let there always be non-being,
637     so we may see their subtlety,
638   And let there always be being,
639 --- 1,6 ----
640   The Nameless is the origin of Heaven and Earth;
641 ! The named is the mother of all things.
642 ! @-
643   Therefore let there always be non-being,
644     so we may see their subtlety,
645   And let there always be being,
646 ***************
647 *** 9,11 ****
648 --- 8,13 ----
649   The two are the same,
650   But after they are produced,
651     they have different names.
652 + They both may be called deep and profound.
653 + Deeper and more profound,
654 + The door of all subtleties!
655 @end example
656
657 @node Less Context
658 @subsubsection An Example of Context Format with Less Context
659
660 Here is the output of @samp{diff -C 1 lao tzu} (@pxref{Sample diff
661 Input}, for the complete contents of the two files).  Notice that at
662 most one context line is reported here.
663
664 @example
665 *** lao 2002-02-21 23:30:39.942229878 -0800
666 --- tzu 2002-02-21 23:30:50.442260588 -0800
667 ***************
668 *** 1,5 ****
669 - The Way that can be told of is not the eternal Way;
670 - The name that can be named is not the eternal name.
671   The Nameless is the origin of Heaven and Earth;
672 ! The Named is the mother of all things.
673   Therefore let there always be non-being,
674 --- 1,4 ----
675   The Nameless is the origin of Heaven and Earth;
676 ! The named is the mother of all things.
677 ! @-
678   Therefore let there always be non-being,
679 ***************
680 *** 11 ****
681 --- 10,13 ----
682     they have different names.
683 + They both may be called deep and profound.
684 + Deeper and more profound,
685 + The door of all subtleties!
686 @end example
687
688 @node Detailed Context
689 @subsubsection Detailed Description of Context Format
690
691 The context output format starts with a two-line header, which looks
692 like this:
693
694 @example
695 *** @var{from-file} @var{from-file-modification-time}
696 --- @var{to-file} @var{to-file-modification time}
697 @end example
698
699 @noindent
700 @vindex LC_TIME
701 @cindex time stamp format, context diffs
702 The time stamp normally looks like @samp{2002-02-21 23:30:39.942229878
703 -0800} to indicate the date, time with fractional seconds, and time
704 zone in @uref{ftp://ftp.isi.edu/in-notes/rfc2822.txt, Internet RFC
705 2822 format}.  (The fractional seconds are omitted on hosts that do
706 not support fractional time stamps.)  However, a traditional time
707 stamp like @samp{Thu Feb 21 23:30:39 2002} is used if the
708 @env{LC_TIME} locale category is either @samp{C} or @samp{POSIX}.
709
710 You can change the header's content with the
711 @option{--label=@var{label}} option; see @ref{Alternate Names}.
712
713 Next come one or more hunks of differences; each hunk shows one area
714 where the files differ.  Context format hunks look like this:
715
716 @example
717 ***************
718 *** @var{from-file-line-numbers} ****
719   @var{from-file-line}
720   @var{from-file-line}@dots{}
721 --- @var{to-file-line-numbers} ----
722   @var{to-file-line}
723   @var{to-file-line}@dots{}
724 @end example
725
726 If a hunk contains two or more lines, its line numbers look like
727 @samp{@var{start},@var{end}}.  Otherwise only its end line number
728 appears.  An empty hunk is considered to end at the line that precedes
729 the hunk.
730
731 The lines of context around the lines that differ start with two space
732 characters.  The lines that differ between the two files start with one
733 of the following indicator characters, followed by a space character:
734
735 @table @samp
736 @item !
737 A line that is part of a group of one or more lines that changed between
738 the two files.  There is a corresponding group of lines marked with
739 @samp{!} in the part of this hunk for the other file.
740
741 @item +
742 An ``inserted'' line in the second file that corresponds to nothing in
743 the first file.
744
745 @item -
746 A ``deleted'' line in the first file that corresponds to nothing in the
747 second file.
748 @end table
749
750 If all of the changes in a hunk are insertions, the lines of
751 @var{from-file} are omitted.  If all of the changes are deletions, the
752 lines of @var{to-file} are omitted.
753
754 @node Unified Format
755 @subsection Unified Format
756 @cindex unified output format
757 @cindex @samp{+-} output format
758
759 The unified output format is a variation on the context format that is
760 more compact because it omits redundant context lines.  To select this
761 output format, use the
762 @option{--unified@r{[}=@var{lines}@r{]}} (@option{-U @var{lines}}),
763 or @option{-u} option.
764 The argument @var{lines} is the number of lines of context to show.
765 When it is not given, it defaults to three.
766
767 At present, only @acronym{GNU} @command{diff} can produce this format and
768 only @acronym{GNU} @command{patch} can automatically apply diffs in this
769 format.  For proper operation, @command{patch} typically needs at
770 least three lines of context.
771
772 @menu
773 * Example Unified::  Sample output in unified format.
774 * Detailed Unified:: A detailed description of unified format.
775 @end menu
776
777 @node Example Unified
778 @subsubsection An Example of Unified Format
779
780 Here is the output of the command @samp{diff -u lao tzu}
781 (@pxref{Sample diff Input}, for the complete contents of the two files):
782
783 @example
784 --- lao 2002-02-21 23:30:39.942229878 -0800
785 +++ tzu 2002-02-21 23:30:50.442260588 -0800
786 @@@@ -1,7 +1,6 @@@@
787 -The Way that can be told of is not the eternal Way;
788 -The name that can be named is not the eternal name.
789  The Nameless is the origin of Heaven and Earth;
790 -The Named is the mother of all things.
791 +The named is the mother of all things.
792 +
793  Therefore let there always be non-being,
794    so we may see their subtlety,
795  And let there always be being,
796 @@@@ -9,3 +8,6 @@@@
797  The two are the same,
798  But after they are produced,
799    they have different names.
800 +They both may be called deep and profound.
801 +Deeper and more profound,
802 +The door of all subtleties!
803 @end example
804
805 @node Detailed Unified
806 @subsubsection Detailed Description of Unified Format
807
808 The unified output format starts with a two-line header, which looks
809 like this:
810
811 @example
812 --- @var{from-file} @var{from-file-modification-time}
813 +++ @var{to-file} @var{to-file-modification-time}
814 @end example
815
816 @noindent
817 @cindex time stamp format, unified diffs
818 The time stamp looks like @samp{2002-02-21 23:30:39.942229878 -0800}
819 to indicate the date, time with fractional seconds, and time zone.
820 The fractional seconds are omitted on hosts that do not support
821 fractional time stamps.
822
823 You can change the header's content with the
824 @option{--label=@var{label}} option. @xref{Alternate Names}.
825
826 Next come one or more hunks of differences; each hunk shows one area
827 where the files differ.  Unified format hunks look like this:
828
829 @example
830 @@@@ @var{from-file-line-numbers} @var{to-file-line-numbers} @@@@
831  @var{line-from-either-file}
832  @var{line-from-either-file}@dots{}
833 @end example
834
835 If a hunk contains just one line, only its start line number appears.
836 Otherwise its line numbers look like @samp{@var{start},@var{count}}.
837 An empty hunk is considered to start at the line that follows the hunk.
838
839 If a hunk and its context contain two or more lines, its
840 line numbers look like @samp{@var{start},@var{count}}.  Otherwise only
841 its end line number appears.  An empty hunk is considered to end at
842 the line that precedes the hunk.
843
844 The lines common to both files begin with a space character.  The lines
845 that actually differ between the two files have one of the following
846 indicator characters in the left print column:
847
848 @table @samp
849 @item +
850 A line was added here to the first file.
851
852 @item -
853 A line was removed here from the first file.
854 @end table
855
856 @node Sections
857 @subsection Showing Which Sections Differences Are in
858 @cindex headings
859 @cindex section headings
860
861 Sometimes you might want to know which part of the files each change
862 falls in.  If the files are source code, this could mean which
863 function was changed.  If the files are documents, it could mean which
864 chapter or appendix was changed.  @acronym{GNU} @command{diff} can
865 show this by displaying the nearest section heading line that precedes
866 the differing lines.  Which lines are ``section headings'' is
867 determined by a regular expression.
868
869 @menu
870 * Specified Headings::  Showing headings that match regular expressions.
871 * C Function Headings:: Showing headings of C functions.
872 @end menu
873
874 @node Specified Headings
875 @subsubsection Showing Lines That Match Regular Expressions
876 @cindex specified headings
877 @cindex regular expression matching headings
878
879 To show in which sections differences occur for files that are not
880 source code for C or similar languages, use the
881 @option{--show-function-line=@var{regexp}} (@option{-F @var{regexp}}) option.
882 @command{diff}
883 considers lines that match the @command{grep}-style regular expression
884 @var{regexp} to be the beginning
885 of a section of the file.  Here are suggested regular expressions for
886 some common languages:
887
888 @c Please add to this list, e.g. Fortran, Pascal, Perl, Python.
889 @table @samp
890 @item ^[[:alpha:]$_]
891 C, C++, Prolog
892 @item ^(
893 Lisp
894 @item ^@@node
895 Texinfo
896 @end table
897
898 This option does not automatically select an output format; in order to
899 use it, you must select the context format (@pxref{Context Format}) or
900 unified format (@pxref{Unified Format}).  In other output formats it
901 has no effect.
902
903 The @option{--show-function-line} (@option{-F}) option finds the nearest
904 unchanged line that precedes each hunk of differences and matches the
905 given regular expression.  Then it adds that line to the end of the
906 line of asterisks in the context format, or to the @samp{@@@@} line in
907 unified format.  If no matching line exists, this option leaves the output for
908 that hunk unchanged.  If that line is more than 40 characters long, it
909 outputs only the first 40 characters.  You can specify more than one
910 regular expression for such lines; @command{diff} tries to match each line
911 against each regular expression, starting with the last one given.  This
912 means that you can use @option{-p} and @option{-F} together, if you wish.
913
914 @node C Function Headings
915 @subsubsection Showing C Function Headings
916 @cindex C function headings
917 @cindex function headings, C
918
919 To show in which functions differences occur for C and similar
920 languages, you can use the @option{--show-c-function} (@option{-p}) option.
921 This option automatically defaults to the context output format
922 (@pxref{Context Format}), with the default number of lines of context.
923 You can override that number with @option{-C @var{lines}} elsewhere in the
924 command line.  You can override both the format and the number with
925 @option{-U @var{lines}} elsewhere in the command line.
926
927 The @option{--show-c-function} (@option{-p}) option is equivalent to
928 @option{-F '^[[:alpha:]$_]'} if the unified format is specified, otherwise
929 @option{-c -F '^[[:alpha:]$_]'} (@pxref{Specified Headings}).  @acronym{GNU}
930 @command{diff} provides this option for the sake of convenience.
931
932 @node Alternate Names
933 @subsection Showing Alternate File Names
934 @cindex alternate file names
935 @cindex file name alternates
936
937 If you are comparing two files that have meaningless or uninformative
938 names, you might want @command{diff} to show alternate names in the header
939 of the context and unified output formats.  To do this, use the
940 @option{--label=@var{label}} option.  The first time
941 you give this option, its argument replaces the name and date of the
942 first file in the header; the second time, its argument replaces the
943 name and date of the second file.  If you give this option more than
944 twice, @command{diff} reports an error.  The @option{--label} option does not
945 affect the file names in the @command{pr} header when the @option{-l} or
946 @option{--paginate} option is used (@pxref{Pagination}).
947
948 Here are the first two lines of the output from @samp{diff -C 2
949 --label=original --label=modified lao tzu}:
950
951 @example
952 *** original
953 --- modified
954 @end example
955
956 @node Side by Side
957 @section Showing Differences Side by Side
958 @cindex side by side
959 @cindex two-column output
960 @cindex columnar output
961
962 @command{diff} can produce a side by side difference listing of two files.
963 The files are listed in two columns with a gutter between them.  The
964 gutter contains one of the following markers:
965
966 @table @asis
967 @item white space
968 The corresponding lines are in common.  That is, either the lines are
969 identical, or the difference is ignored because of one of the
970 @option{--ignore} options (@pxref{White Space}).
971
972 @item @samp{|}
973 The corresponding lines differ, and they are either both complete
974 or both incomplete.
975
976 @item @samp{<}
977 The files differ and only the first file contains the line.
978
979 @item @samp{>}
980 The files differ and only the second file contains the line.
981
982 @item @samp{(}
983 Only the first file contains the line, but the difference is ignored.
984
985 @item @samp{)}
986 Only the second file contains the line, but the difference is ignored.
987
988 @item @samp{\}
989 The corresponding lines differ, and only the first line is incomplete.
990
991 @item @samp{/}
992 The corresponding lines differ, and only the second line is incomplete.
993 @end table
994
995 Normally, an output line is incomplete if and only if the lines that it
996 contains are incomplete.  @xref{Incomplete Lines}.  However, when an
997 output line represents two differing lines, one might be incomplete
998 while the other is not.  In this case, the output line is complete,
999 but its the gutter is marked @samp{\} if the first line is incomplete,
1000 @samp{/} if the second line is.
1001
1002 Side by side format is sometimes easiest to read, but it has limitations.
1003 It generates much wider output than usual, and truncates lines that are
1004 too long to fit.  Also, it relies on lining up output more heavily than
1005 usual, so its output looks particularly bad if you use varying
1006 width fonts, nonstandard tab stops, or nonprinting characters.
1007
1008 You can use the @command{sdiff} command to interactively merge side by side
1009 differences.  @xref{Interactive Merging}, for more information on merging files.
1010
1011 @menu
1012 * Side by Side Format::  Controlling side by side output format.
1013 * Example Side by Side:: Sample side by side output.
1014 @end menu
1015
1016 @node Side by Side Format
1017 @subsection Controlling Side by Side Format
1018 @cindex side by side format
1019
1020 The @option{--side-by-side} (@option{-y}) option selects side by side
1021 format.  Because side by side output lines contain two input lines, the
1022 output is wider than usual: normally 130 print columns, which can fit
1023 onto a traditional printer line.  You can set the width of the output
1024 with the @option{--width=@var{columns}} (@option{-W @var{columns}})
1025 option.  The output is split into two halves of equal width, separated by a
1026 small gutter to mark differences; the right half is aligned to a tab
1027 stop so that tabs line up.  Input lines that are too long to fit in half
1028 of an output line are truncated for output.
1029
1030 The @option{--left-column} option prints only the left column of two
1031 common lines.  The @option{--suppress-common-lines} option suppresses
1032 common lines entirely.
1033
1034 @node Example Side by Side
1035 @subsection An Example of Side by Side Format
1036
1037 Here is the output of the command @samp{diff -y -W 72 lao tzu}
1038 (@pxref{Sample diff Input}, for the complete contents of the two files).
1039
1040 @example
1041 The Way that can be told of is n   <
1042 The name that can be named is no   <
1043 The Nameless is the origin of He        The Nameless is the origin of He
1044 The Named is the mother of all t   |    The named is the mother of all t
1045                                    >
1046 Therefore let there always be no        Therefore let there always be no
1047   so we may see their subtlety,           so we may see their subtlety,
1048 And let there always be being,          And let there always be being,
1049   so we may see their outcome.            so we may see their outcome.
1050 The two are the same,                   The two are the same,
1051 But after they are produced,            But after they are produced,
1052   they have different names.              they have different names.
1053                                    >    They both may be called deep and
1054                                    >    Deeper and more profound,
1055                                    >    The door of all subtleties!
1056 @end example
1057
1058 @node Normal
1059 @section Showing Differences Without Context
1060 @cindex normal output format
1061 @cindex @samp{<} output format
1062
1063 The ``normal'' @command{diff} output format shows each hunk of differences
1064 without any surrounding context.  Sometimes such output is the clearest
1065 way to see how lines have changed, without the clutter of nearby
1066 unchanged lines (although you can get similar results with the context
1067 or unified formats by using 0 lines of context).  However, this format
1068 is no longer widely used for sending out patches; for that purpose, the
1069 context format (@pxref{Context Format}) and the unified format
1070 (@pxref{Unified Format}) are superior.  Normal format is the default for
1071 compatibility with older versions of @command{diff} and the @acronym{POSIX}
1072 standard.  Use the @option{--normal} option to select this output
1073 format explicitly.
1074
1075 @menu
1076 * Example Normal::  Sample output in the normal format.
1077 * Detailed Normal:: A detailed description of normal output format.
1078 @end menu
1079
1080 @node Example Normal
1081 @subsection An Example of Normal Format
1082
1083 Here is the output of the command @samp{diff lao tzu}
1084 (@pxref{Sample diff Input}, for the complete contents of the two files).
1085 Notice that it shows only the lines that are different between the two
1086 files.
1087
1088 @example
1089 1,2d0
1090 < The Way that can be told of is not the eternal Way;
1091 < The name that can be named is not the eternal name.
1092 4c2,3
1093 < The Named is the mother of all things.
1094 ---
1095 > The named is the mother of all things.
1096 > @-
1097 11a11,13
1098 > They both may be called deep and profound.
1099 > Deeper and more profound,
1100 > The door of all subtleties!
1101 @end example
1102
1103 @node Detailed Normal
1104 @subsection Detailed Description of Normal Format
1105
1106 The normal output format consists of one or more hunks of differences;
1107 each hunk shows one area where the files differ.  Normal format hunks
1108 look like this:
1109
1110 @example
1111 @var{change-command}
1112 < @var{from-file-line}
1113 < @var{from-file-line}@dots{}
1114 ---
1115 > @var{to-file-line}
1116 > @var{to-file-line}@dots{}
1117 @end example
1118
1119 There are three types of change commands.  Each consists of a line
1120 number or comma-separated range of lines in the first file, a single
1121 character indicating the kind of change to make, and a line number or
1122 comma-separated range of lines in the second file.  All line numbers are
1123 the original line numbers in each file.  The types of change commands
1124 are:
1125
1126 @table @samp
1127 @item @var{l}a@var{r}
1128 Add the lines in range @var{r} of the second file after line @var{l} of
1129 the first file.  For example, @samp{8a12,15} means append lines 12--15
1130 of file 2 after line 8 of file 1; or, if changing file 2 into file 1,
1131 delete lines 12--15 of file 2.
1132
1133 @item @var{f}c@var{t}
1134 Replace the lines in range @var{f} of the first file with lines in range
1135 @var{t} of the second file.  This is like a combined add and delete, but
1136 more compact.  For example, @samp{5,7c8,10} means change lines 5--7 of
1137 file 1 to read as lines 8--10 of file 2; or, if changing file 2 into
1138 file 1, change lines 8--10 of file 2 to read as lines 5--7 of file 1.
1139
1140 @item @var{r}d@var{l}
1141 Delete the lines in range @var{r} from the first file; line @var{l} is where
1142 they would have appeared in the second file had they not been deleted.
1143 For example, @samp{5,7d3} means delete lines 5--7 of file 1; or, if
1144 changing file 2 into file 1, append lines 5--7 of file 1 after line 3 of
1145 file 2.
1146 @end table
1147
1148 @node Scripts
1149 @section Making Edit Scripts
1150 @cindex script output formats
1151
1152 Several output modes produce command scripts for editing @var{from-file}
1153 to produce @var{to-file}.
1154
1155 @menu
1156 * ed Scripts:: Using @command{diff} to produce commands for @command{ed}.
1157 * Forward ed:: Making forward @command{ed} scripts.
1158 * RCS::        A special @command{diff} output format used by @acronym{RCS}.
1159 @end menu
1160
1161 @node ed Scripts
1162 @subsection @command{ed} Scripts
1163 @cindex @command{ed} script output format
1164
1165 @command{diff} can produce commands that direct the @command{ed} text editor
1166 to change the first file into the second file.  Long ago, this was the
1167 only output mode that was suitable for editing one file into another
1168 automatically; today, with @command{patch}, it is almost obsolete.  Use the
1169 @option{--ed} (@option{-e}) option to select this output format.
1170
1171 Like the normal format (@pxref{Normal}), this output format does not
1172 show any context; unlike the normal format, it does not include the
1173 information necessary to apply the diff in reverse (to produce the first
1174 file if all you have is the second file and the diff).
1175
1176 If the file @file{d} contains the output of @samp{diff -e old new}, then
1177 the command @samp{(cat d && echo w) | ed - old} edits @file{old} to make
1178 it a copy of @file{new}.  More generally, if @file{d1}, @file{d2},
1179 @dots{}, @file{dN} contain the outputs of @samp{diff -e old new1},
1180 @samp{diff -e new1 new2}, @dots{}, @samp{diff -e newN-1 newN},
1181 respectively, then the command @samp{(cat d1 d2 @dots{} dN && echo w) |
1182 ed - old} edits @file{old} to make it a copy of @file{newN}.
1183
1184 @menu
1185 * Example ed::  A sample @command{ed} script.
1186 * Detailed ed:: A detailed description of @command{ed} format.
1187 @end menu
1188
1189 @node Example ed
1190 @subsubsection Example @command{ed} Script
1191
1192 Here is the output of @samp{diff -e lao tzu} (@pxref{Sample
1193 diff Input}, for the complete contents of the two files):
1194
1195 @example
1196 11a
1197 They both may be called deep and profound.
1198 Deeper and more profound,
1199 The door of all subtleties!
1200 .
1201 4c
1202 The named is the mother of all things.
1203
1204 .
1205 1,2d
1206 @end example
1207
1208 @node Detailed ed
1209 @subsubsection Detailed Description of @command{ed} Format
1210
1211 The @command{ed} output format consists of one or more hunks of
1212 differences.  The changes closest to the ends of the files come first so
1213 that commands that change the number of lines do not affect how
1214 @command{ed} interprets line numbers in succeeding commands.  @command{ed}
1215 format hunks look like this:
1216
1217 @example
1218 @var{change-command}
1219 @var{to-file-line}
1220 @var{to-file-line}@dots{}
1221 .
1222 @end example
1223
1224 Because @command{ed} uses a single period on a line to indicate the
1225 end of input, @acronym{GNU} @command{diff} protects lines of changes
1226 that contain a single period on a line by writing two periods instead,
1227 then writing a subsequent @command{ed} command to change the two
1228 periods into one.  The @command{ed} format cannot represent an
1229 incomplete line, so if the second file ends in a changed incomplete
1230 line, @command{diff} reports an error and then pretends that a newline
1231 was appended.
1232
1233 There are three types of change commands.  Each consists of a line
1234 number or comma-separated range of lines in the first file and a single
1235 character indicating the kind of change to make.  All line numbers are
1236 the original line numbers in the file.  The types of change commands
1237 are:
1238
1239 @table @samp
1240 @item @var{l}a
1241 Add text from the second file after line @var{l} in the first file.  For
1242 example, @samp{8a} means to add the following lines after line 8 of file
1243 1.
1244
1245 @item @var{r}c
1246 Replace the lines in range @var{r} in the first file with the following
1247 lines.  Like a combined add and delete, but more compact.  For example,
1248 @samp{5,7c} means change lines 5--7 of file 1 to read as the text file
1249 2.
1250
1251 @item @var{r}d
1252 Delete the lines in range @var{r} from the first file.  For example,
1253 @samp{5,7d} means delete lines 5--7 of file 1.
1254 @end table
1255
1256 @node Forward ed
1257 @subsection Forward @command{ed} Scripts
1258 @cindex forward @command{ed} script output format
1259
1260 @command{diff} can produce output that is like an @command{ed} script, but
1261 with hunks in forward (front to back) order.  The format of the commands
1262 is also changed slightly: command characters precede the lines they
1263 modify, spaces separate line numbers in ranges, and no attempt is made
1264 to disambiguate hunk lines consisting of a single period.  Like
1265 @command{ed} format, forward @command{ed} format cannot represent incomplete
1266 lines.
1267
1268 Forward @command{ed} format is not very useful, because neither @command{ed}
1269 nor @command{patch} can apply diffs in this format.  It exists mainly for
1270 compatibility with older versions of @command{diff}.  Use the @option{-f} or
1271 @option{--forward-ed} option to select it.
1272
1273 @node RCS
1274 @subsection @acronym{RCS} Scripts
1275 @cindex @acronym{RCS} script output format
1276
1277 The @acronym{RCS} output format is designed specifically for use by
1278 the Revision Control System, which is a set of free programs used for
1279 organizing different versions and systems of files.  Use the
1280 @option{--rcs} (@option{-n}) option to select this output format.  It
1281 is like the forward @command{ed} format (@pxref{Forward ed}), but it
1282 can represent arbitrary changes to the contents of a file because it
1283 avoids the forward @command{ed} format's problems with lines
1284 consisting of a single period and with incomplete lines.  Instead of
1285 ending text sections with a line consisting of a single period, each
1286 command specifies the number of lines it affects; a combination of the
1287 @samp{a} and @samp{d} commands are used instead of @samp{c}.  Also, if
1288 the second file ends in a changed incomplete line, then the output
1289 also ends in an incomplete line.
1290
1291 Here is the output of @samp{diff -n lao tzu} (@pxref{Sample
1292 diff Input}, for the complete contents of the two files):
1293
1294 @example
1295 d1 2
1296 d4 1
1297 a4 2
1298 The named is the mother of all things.
1299
1300 a11 3
1301 They both may be called deep and profound.
1302 Deeper and more profound,
1303 The door of all subtleties!
1304 @end example
1305
1306 @node If-then-else
1307 @section Merging Files with If-then-else
1308 @cindex merged output format
1309 @cindex if-then-else output format
1310 @cindex C if-then-else output format
1311 @cindex @command{ifdef} output format
1312
1313 You can use @command{diff} to merge two files of C source code.  The output
1314 of @command{diff} in this format contains all the lines of both files.
1315 Lines common to both files are output just once; the differing parts are
1316 separated by the C preprocessor directives @code{#ifdef @var{name}} or
1317 @code{#ifndef @var{name}}, @code{#else}, and @code{#endif}.  When
1318 compiling the output, you select which version to use by either defining
1319 or leaving undefined the macro @var{name}.
1320
1321 To merge two files, use @command{diff} with the @option{-D @var{name}} or
1322 @option{--ifdef=@var{name}} option.  The argument @var{name} is the C
1323 preprocessor identifier to use in the @code{#ifdef} and @code{#ifndef}
1324 directives.
1325
1326 For example, if you change an instance of @code{wait (&s)} to
1327 @code{waitpid (-1, &s, 0)} and then merge the old and new files with
1328 the @option{--ifdef=HAVE_WAITPID} option, then the affected part of your code
1329 might look like this:
1330
1331 @example
1332     do @{
1333 #ifndef HAVE_WAITPID
1334         if ((w = wait (&s)) < 0  &&  errno != EINTR)
1335 #else /* HAVE_WAITPID */
1336         if ((w = waitpid (-1, &s, 0)) < 0  &&  errno != EINTR)
1337 #endif /* HAVE_WAITPID */
1338             return w;
1339     @} while (w != child);
1340 @end example
1341
1342 You can specify formats for languages other than C by using line group
1343 formats and line formats, as described in the next sections.
1344
1345 @menu
1346 * Line Group Formats::    Formats for general if-then-else line groups.
1347 * Line Formats::          Formats for each line in a line group.
1348 * Example If-then-else::  Sample if-then-else format output.
1349 * Detailed If-then-else:: A detailed description of if-then-else format.
1350 @end menu
1351
1352 @node Line Group Formats
1353 @subsection Line Group Formats
1354 @cindex line group formats
1355 @cindex formats for if-then-else line groups
1356
1357 Line group formats let you specify formats suitable for many
1358 applications that allow if-then-else input, including programming
1359 languages and text formatting languages.  A line group format specifies
1360 the output format for a contiguous group of similar lines.
1361
1362 For example, the following command compares the TeX files @file{old}
1363 and @file{new}, and outputs a merged file in which old regions are
1364 surrounded by @samp{\begin@{em@}}-@samp{\end@{em@}} lines, and new
1365 regions are surrounded by @samp{\begin@{bf@}}-@samp{\end@{bf@}} lines.
1366
1367 @example
1368 diff \
1369    --old-group-format='\begin@{em@}
1370 %<\end@{em@}
1371 ' \
1372    --new-group-format='\begin@{bf@}
1373 %>\end@{bf@}
1374 ' \
1375    old new
1376 @end example
1377
1378 The following command is equivalent to the above example, but it is a
1379 little more verbose, because it spells out the default line group formats.
1380
1381 @example
1382 diff \
1383    --old-group-format='\begin@{em@}
1384 %<\end@{em@}
1385 ' \
1386    --new-group-format='\begin@{bf@}
1387 %>\end@{bf@}
1388 ' \
1389    --unchanged-group-format='%=' \
1390    --changed-group-format='\begin@{em@}
1391 %<\end@{em@}
1392 \begin@{bf@}
1393 %>\end@{bf@}
1394 ' \
1395    old new
1396 @end example
1397
1398 Here is a more advanced example, which outputs a diff listing with
1399 headers containing line numbers in a ``plain English'' style.
1400
1401 @example
1402 diff \
1403    --unchanged-group-format='' \
1404    --old-group-format='-------- %dn line%(n=1?:s) deleted at %df:
1405 %<' \
1406    --new-group-format='-------- %dN line%(N=1?:s) added after %de:
1407 %>' \
1408    --changed-group-format='-------- %dn line%(n=1?:s) changed at %df:
1409 %<-------- to:
1410 %>' \
1411    old new
1412 @end example
1413
1414 To specify a line group format, use @command{diff} with one of the options
1415 listed below.  You can specify up to four line group formats, one for
1416 each kind of line group.  You should quote @var{format}, because it
1417 typically contains shell metacharacters.
1418
1419 @table @option
1420 @item --old-group-format=@var{format}
1421 These line groups are hunks containing only lines from the first file.
1422 The default old group format is the same as the changed group format if
1423 it is specified; otherwise it is a format that outputs the line group as-is.
1424
1425 @item --new-group-format=@var{format}
1426 These line groups are hunks containing only lines from the second
1427 file.  The default new group format is same as the changed group
1428 format if it is specified; otherwise it is a format that outputs the
1429 line group as-is.
1430
1431 @item --changed-group-format=@var{format}
1432 These line groups are hunks containing lines from both files.  The
1433 default changed group format is the concatenation of the old and new
1434 group formats.
1435
1436 @item --unchanged-group-format=@var{format}
1437 These line groups contain lines common to both files.  The default
1438 unchanged group format is a format that outputs the line group as-is.
1439 @end table
1440
1441 In a line group format, ordinary characters represent themselves;
1442 conversion specifications start with @samp{%} and have one of the
1443 following forms.
1444
1445 @table @samp
1446 @item %<
1447 stands for the lines from the first file, including the trailing newline.
1448 Each line is formatted according to the old line format (@pxref{Line Formats}).
1449
1450 @item %>
1451 stands for the lines from the second file, including the trailing newline.
1452 Each line is formatted according to the new line format.
1453
1454 @item %=
1455 stands for the lines common to both files, including the trailing newline.
1456 Each line is formatted according to the unchanged line format.
1457
1458 @item %%
1459 stands for @samp{%}.
1460
1461 @item %c'@var{C}'
1462 where @var{C} is a single character, stands for @var{C}.
1463 @var{C} may not be a backslash or an apostrophe.
1464 For example, @samp{%c':'} stands for a colon, even inside
1465 the then-part of an if-then-else format, which a colon would
1466 normally terminate.
1467
1468 @item %c'\@var{O}'
1469 where @var{O} is a string of 1, 2, or 3 octal digits,
1470 stands for the character with octal code @var{O}.
1471 For example, @samp{%c'\0'} stands for a null character.
1472
1473 @item @var{F}@var{n}
1474 where @var{F} is a @code{printf} conversion specification and @var{n} is one
1475 of the following letters, stands for @var{n}'s value formatted with @var{F}.
1476
1477 @table @samp
1478 @item e
1479 The line number of the line just before the group in the old file.
1480
1481 @item f
1482 The line number of the first line in the group in the old file;
1483 equals @var{e} + 1.
1484
1485 @item l
1486 The line number of the last line in the group in the old file.
1487
1488 @item m
1489 The line number of the line just after the group in the old file;
1490 equals @var{l} + 1.
1491
1492 @item n
1493 The number of lines in the group in the old file; equals @var{l} - @var{f} + 1.
1494
1495 @item E, F, L, M, N
1496 Likewise, for lines in the new file.
1497
1498 @end table
1499
1500 @vindex LC_NUMERIC
1501 The @code{printf} conversion specification can be @samp{%d},
1502 @samp{%o}, @samp{%x}, or @samp{%X}, specifying decimal, octal,
1503 lower case hexadecimal, or upper case hexadecimal output
1504 respectively.  After the @samp{%} the following options can appear in
1505 sequence: a series of zero or more flags; an integer
1506 specifying the minimum field width; and a period followed by an
1507 optional integer specifying the minimum number of digits.
1508 The flags are @samp{-} for left-justification, @samp{'} for separating
1509 the digit into groups as specified by the @env{LC_NUMERIC} locale category,
1510 and @samp{0} for padding with zeros instead of spaces.
1511 For example, @samp{%5dN} prints the number of new lines in the group
1512 in a field of width 5 characters, using the @code{printf} format @code{"%5d"}.
1513
1514 @item (@var{A}=@var{B}?@var{T}:@var{E})
1515 If @var{A} equals @var{B} then @var{T} else @var{E}.
1516 @var{A} and @var{B} are each either a decimal constant
1517 or a single letter interpreted as above.
1518 This format spec is equivalent to @var{T} if
1519 @var{A}'s value equals @var{B}'s; otherwise it is equivalent to @var{E}.
1520
1521 For example, @samp{%(N=0?no:%dN) line%(N=1?:s)} is equivalent to
1522 @samp{no lines} if @var{N} (the number of lines in the group in the
1523 new file) is 0, to @samp{1 line} if @var{N} is 1, and to @samp{%dN lines}
1524 otherwise.
1525 @end table
1526
1527 @node Line Formats
1528 @subsection Line Formats
1529 @cindex line formats
1530
1531 Line formats control how each line taken from an input file is
1532 output as part of a line group in if-then-else format.
1533
1534 For example, the following command outputs text with a one-character
1535 change indicator to the left of the text.  The first character of output
1536 is @samp{-} for deleted lines, @samp{|} for added lines, and a space for
1537 unchanged lines.  The formats contain newline characters where newlines
1538 are desired on output.
1539
1540 @example
1541 diff \
1542    --old-line-format='-%l
1543 ' \
1544    --new-line-format='|%l
1545 ' \
1546    --unchanged-line-format=' %l
1547 ' \
1548    old new
1549 @end example
1550
1551 To specify a line format, use one of the following options.  You should
1552 quote @var{format}, since it often contains shell metacharacters.
1553
1554 @table @option
1555 @item --old-line-format=@var{format}
1556 formats lines just from the first file.
1557
1558 @item --new-line-format=@var{format}
1559 formats lines just from the second file.
1560
1561 @item --unchanged-line-format=@var{format}
1562 formats lines common to both files.
1563
1564 @item --line-format=@var{format}
1565 formats all lines; in effect, it sets all three above options simultaneously.
1566 @end table
1567
1568 In a line format, ordinary characters represent themselves;
1569 conversion specifications start with @samp{%} and have one of the
1570 following forms.
1571
1572 @table @samp
1573 @item %l
1574 stands for the contents of the line, not counting its trailing
1575 newline (if any).  This format ignores whether the line is incomplete;
1576 @xref{Incomplete Lines}.
1577
1578 @item %L
1579 stands for the contents of the line, including its trailing newline
1580 (if any).  If a line is incomplete, this format preserves its
1581 incompleteness.
1582
1583 @item %%
1584 stands for @samp{%}.
1585
1586 @item %c'@var{C}'
1587 where @var{C} is a single character, stands for @var{C}.
1588 @var{C} may not be a backslash or an apostrophe.
1589 For example, @samp{%c':'} stands for a colon.
1590
1591 @item %c'\@var{O}'
1592 where @var{O} is a string of 1, 2, or 3 octal digits,
1593 stands for the character with octal code @var{O}.
1594 For example, @samp{%c'\0'} stands for a null character.
1595
1596 @item @var{F}n
1597 where @var{F} is a @code{printf} conversion specification,
1598 stands for the line number formatted with @var{F}.
1599 For example, @samp{%.5dn} prints the line number using the
1600 @code{printf} format @code{"%.5d"}.  @xref{Line Group Formats}, for
1601 more about printf conversion specifications.
1602
1603 @end table
1604
1605 The default line format is @samp{%l} followed by a newline character.
1606
1607 If the input contains tab characters and it is important that they line
1608 up on output, you should ensure that @samp{%l} or @samp{%L} in a line
1609 format is just after a tab stop (e.g.@: by preceding @samp{%l} or
1610 @samp{%L} with a tab character), or you should use the @option{-t} or
1611 @option{--expand-tabs} option.
1612
1613 Taken together, the line and line group formats let you specify many
1614 different formats.  For example, the following command uses a format
1615 similar to normal @command{diff} format.  You can tailor this command
1616 to get fine control over @command{diff} output.
1617
1618 @example
1619 diff \
1620    --old-line-format='< %l
1621 ' \
1622    --new-line-format='> %l
1623 ' \
1624    --old-group-format='%df%(f=l?:,%dl)d%dE
1625 %<' \
1626    --new-group-format='%dea%dF%(F=L?:,%dL)
1627 %>' \
1628    --changed-group-format='%df%(f=l?:,%dl)c%dF%(F=L?:,%dL)
1629 %<---
1630 %>' \
1631    --unchanged-group-format='' \
1632    old new
1633 @end example
1634
1635 @node Example If-then-else
1636 @subsection An Example of If-then-else Format
1637
1638 Here is the output of @samp{diff -DTWO lao tzu} (@pxref{Sample
1639 diff Input}, for the complete contents of the two files):
1640
1641 @example
1642 #ifndef TWO
1643 The Way that can be told of is not the eternal Way;
1644 The name that can be named is not the eternal name.
1645 #endif /* ! TWO */
1646 The Nameless is the origin of Heaven and Earth;
1647 #ifndef TWO
1648 The Named is the mother of all things.
1649 #else /* TWO */
1650 The named is the mother of all things.
1651
1652 #endif /* TWO */
1653 Therefore let there always be non-being,
1654   so we may see their subtlety,
1655 And let there always be being,
1656   so we may see their outcome.
1657 The two are the same,
1658 But after they are produced,
1659   they have different names.
1660 #ifdef TWO
1661 They both may be called deep and profound.
1662 Deeper and more profound,
1663 The door of all subtleties!
1664 #endif /* TWO */
1665 @end example
1666
1667 @node Detailed If-then-else
1668 @subsection Detailed Description of If-then-else Format
1669
1670 For lines common to both files, @command{diff} uses the unchanged line
1671 group format.  For each hunk of differences in the merged output
1672 format, if the hunk contains only lines from the first file,
1673 @command{diff} uses the old line group format; if the hunk contains only
1674 lines from the second file, @command{diff} uses the new group format;
1675 otherwise, @command{diff} uses the changed group format.
1676
1677 The old, new, and unchanged line formats specify the output format of
1678 lines from the first file, lines from the second file, and lines common
1679 to both files, respectively.
1680
1681 The option @option{--ifdef=@var{name}} is equivalent to
1682 the following sequence of options using shell syntax:
1683
1684 @example
1685 --old-group-format='#ifndef @var{name}
1686 %<#endif /* ! @var{name} */
1687 ' \
1688 --new-group-format='#ifdef @var{name}
1689 %>#endif /* @var{name} */
1690 ' \
1691 --unchanged-group-format='%=' \
1692 --changed-group-format='#ifndef @var{name}
1693 %<#else /* @var{name} */
1694 %>#endif /* @var{name} */
1695 '
1696 @end example
1697
1698 You should carefully check the @command{diff} output for proper nesting.
1699 For example, when using the @option{-D @var{name}} or
1700 @option{--ifdef=@var{name}} option, you should check that if the
1701 differing lines contain any of the C preprocessor directives
1702 @samp{#ifdef}, @samp{#ifndef}, @samp{#else}, @samp{#elif}, or
1703 @samp{#endif}, they are nested properly and match.  If they don't, you
1704 must make corrections manually.  It is a good idea to carefully check
1705 the resulting code anyway to make sure that it really does what you
1706 want it to; depending on how the input files were produced, the output
1707 might contain duplicate or otherwise incorrect code.
1708
1709 The @command{patch} @option{-D @var{name}} option behaves like
1710 the @command{diff} @option{-D @var{name}} option, except it operates on
1711 a file and a diff to produce a merged file.  @xref{patch Options}.
1712
1713 @node Incomplete Lines
1714 @chapter Incomplete Lines
1715 @cindex incomplete lines
1716 @cindex full lines
1717 @cindex newline treatment by @command{diff}
1718
1719 When an input file ends in a non-newline character, its last line is
1720 called an @dfn{incomplete line} because its last character is not a
1721 newline.  All other lines are called @dfn{full lines} and end in a
1722 newline character.  Incomplete lines do not match full lines unless
1723 differences in white space are ignored (@pxref{White Space}).
1724
1725 An incomplete line is normally distinguished on output from a full
1726 line by a following line that starts with @samp{\}.  However, the
1727 @acronym{RCS} format (@pxref{RCS}) outputs the incomplete line as-is,
1728 without any trailing newline or following line.  The side by side
1729 format normally represents incomplete lines as-is, but in some cases
1730 uses a @samp{\} or @samp{/} gutter marker.  @xref{Side by Side}.  The
1731 if-then-else line format preserves a line's incompleteness with
1732 @samp{%L}, and discards the newline with @samp{%l}.  @xref{Line Formats}.
1733 Finally, with the @command{ed} and forward @command{ed}
1734 output formats (@pxref{Output Formats}) @command{diff} cannot
1735 represent an incomplete line, so it pretends there was a newline and
1736 reports an error.
1737
1738 For example, suppose @file{F} and @file{G} are one-byte files that
1739 contain just @samp{f} and @samp{g}, respectively.  Then @samp{diff F G}
1740 outputs
1741
1742 @example
1743 1c1
1744 < f
1745 \ No newline at end of file
1746 ---
1747 > g
1748 \ No newline at end of file
1749 @end example
1750
1751 @noindent
1752 (The exact message may differ in non-English locales.)
1753 @samp{diff -n F G} outputs the following without a trailing newline:
1754
1755 @example
1756 d1 1
1757 a1 1
1758 g
1759 @end example
1760
1761 @noindent
1762 @samp{diff -e F G} reports two errors and outputs the following:
1763
1764 @example
1765 1c
1766 g
1767 .
1768 @end example
1769
1770 @node Comparing Directories
1771 @chapter Comparing Directories
1772
1773 @vindex LC_COLLATE
1774 You can use @command{diff} to compare some or all of the files in two
1775 directory trees.  When both file name arguments to @command{diff} are
1776 directories, it compares each file that is contained in both
1777 directories, examining file names in alphabetical order as specified by
1778 the @env{LC_COLLATE} locale category.  Normally
1779 @command{diff} is silent about pairs of files that contain no differences,
1780 but if you use the @option{--report-identical-files} (@option{-s}) option,
1781 it reports pairs of identical files.  Normally @command{diff} reports
1782 subdirectories common to both directories without comparing
1783 subdirectories' files, but if you use the @option{-r} or
1784 @option{--recursive} option, it compares every corresponding pair of files
1785 in the directory trees, as many levels deep as they go.
1786
1787 If only one file exists, @command{diff} normally does not show its
1788 contents; it merely reports that one file exists but the other does
1789 not.  You can make @command{diff} act as though the missing file is
1790 empty, so that it outputs the entire contents of the file that
1791 actually exists.  (It is output as either an insertion or a
1792 deletion, depending on whether the missing file is in the first or the
1793 second position.)  To do this, use the @option{--new-file}
1794 (@option{-N}) option.  This option affects command-line arguments as
1795 well as files found via directory traversal; for example, @samp{diff
1796 -N a b} treats @file{a} as empty if @file{a} does not exist but
1797 @file{b} does, and similarly @samp{diff -N - b} treats standard input
1798 as empty if it is closed but @file{b} exists.
1799
1800 If the older directory contains large files that are not in
1801 the newer directory, you can make the patch smaller by using the
1802 @option{--unidirectional-new-file} option instead of @option{-N}.
1803 This option is like @option{-N} except that it inserts the contents only
1804 of files that appear in the second directory but not the first (that is,
1805 files that were added).  At the top of the patch, write instructions for
1806 the user applying the patch to remove the files that were deleted before
1807 applying the patch.  @xref{Making Patches}, for more discussion of
1808 making patches for distribution.
1809
1810 To ignore some files while comparing directories, use the
1811 @option{--exclude=@var{pattern}} (@option{-x @var{pattern}}) option.
1812 This option
1813 ignores any files or subdirectories whose base names match the shell
1814 pattern @var{pattern}.  Unlike in the shell, a period at the start of
1815 the base of a file name matches a wildcard at the start of a pattern.
1816 You should enclose @var{pattern} in quotes so that the shell does not
1817 expand it.  For example, the option @option{-x '*.[ao]'} ignores any file
1818 whose name ends with @samp{.a} or @samp{.o}.
1819
1820 This option accumulates if you specify it more than once.  For example,
1821 using the options @option{-x 'RCS' -x '*,v'} ignores any file or
1822 subdirectory whose base name is @samp{RCS} or ends with @samp{,v}.
1823
1824 If you need to give this option many times, you can instead put the
1825 patterns in a file, one pattern per line, and use the
1826 @option{--exclude-from=@var{file}} (@option{-X @var{file}}) option.
1827 Trailing white space and empty lines are ignored in the pattern file.
1828
1829 If you have been comparing two directories and stopped partway through,
1830 later you might want to continue where you left off.  You can do this by
1831 using the @option{--starting-file=@var{file}} (@option{-S @var{file}})
1832 option.  This compares only the file @var{file} and all alphabetically
1833 later files in the topmost directory level.
1834
1835 If two directories differ only in that file names are lower case in
1836 one directory and upper case in the upper, @command{diff} normally
1837 reports many differences because it compares file names in a
1838 case sensitive way.  With the @option{--ignore-file-name-case} option,
1839 @command{diff} ignores case differences in file names, so that for example
1840 the contents of the file @file{Tao} in one directory are compared to
1841 the contents of the file @file{TAO} in the other.  The
1842 @option{--no-ignore-file-name-case} option cancels the effect of the
1843 @option{--ignore-file-name-case} option, reverting to the default
1844 behavior.
1845
1846 If an @option{--exclude=@var{pattern}} (@option{-x @var{pattern}}) option,
1847 or an @option{--exclude-from=@var{file}} (@option{-X @var{file}}) option,
1848 is specified while the @option{--ignore-file-name-case} option is in
1849 effect, case is ignored when excluding file names matching the
1850 specified patterns.
1851
1852 To avoid that @command{diff} follows symbolic links, use the
1853 @c later: @option{--no-dereference} (@option{-P}).
1854 @option{--no-dereference}.
1855 When this option is in use,
1856 symbolic links will be treated like a special kind of files, rather than
1857 comparing the target of each symbolic link.
1858
1859 @node Adjusting Output
1860 @chapter Making @command{diff} Output Prettier
1861
1862 @command{diff} provides several ways to adjust the appearance of its output.
1863 These adjustments can be applied to any output format.
1864
1865 @menu
1866 * Tabs::            Preserving the alignment of tab stops.
1867 * Trailing Blanks:: Suppressing blanks before empty output lines.
1868 * Pagination::      Page numbering and time-stamping @command{diff} output.
1869 @end menu
1870
1871 @node Tabs
1872 @section Preserving Tab Stop Alignment
1873 @cindex tab stop alignment
1874 @cindex aligning tab stops
1875
1876 The lines of text in some of the @command{diff} output formats are
1877 preceded by one or two characters that indicate whether the text is
1878 inserted, deleted, or changed.  The addition of those characters can
1879 cause tabs to move to the next tab stop, throwing off the alignment of
1880 columns in the line.  @acronym{GNU} @command{diff} provides two ways
1881 to make tab-aligned columns line up correctly.
1882
1883 The first way is to have @command{diff} convert all tabs into the correct
1884 number of spaces before outputting them; select this method with the
1885 @option{--expand-tabs} (@option{-t}) option.  To use this form of output with
1886 @command{patch}, you must give @command{patch} the @option{-l} or
1887 @option{--ignore-white-space} option (@pxref{Changed White Space}, for more
1888 information).  @command{diff} normally assumes that tab stops are set
1889 every 8 print columns, but this can be altered by the
1890 @option{--tabsize=@var{columns}} option.
1891
1892 The other method for making tabs line up correctly is to add a tab
1893 character instead of a space after the indicator character at the
1894 beginning of the line.  This ensures that all following tab characters
1895 are in the same position relative to tab stops that they were in the
1896 original files, so that the output is aligned correctly.  Its
1897 disadvantage is that it can make long lines too long to fit on one line
1898 of the screen or the paper.  It also does not work with the unified
1899 output format, which does not have a space character after the change
1900 type indicator character.  Select this method with the @option{-T} or
1901 @option{--initial-tab} option.
1902
1903 @node Trailing Blanks
1904 @section Omitting trailing blanks
1905 @cindex trailing blanks
1906 When outputting lines in normal or context format, or outputting an
1907 unchanged line in unified format, @command{diff} normally outputs a
1908 blank just before each line.  If the line is empty, the output of
1909 @command{diff} therefore contains trailing blanks even though the
1910 input does not contain them.  For example, when outputting an
1911 unchanged empty line in context format, @command{diff} normally
1912 outputs a line with two leading spaces.
1913
1914 Some text editors and email agents routinely delete trailing blanks,
1915 so it can be a problem to deal with diff output files that contain
1916 them.  You can avoid this problem with the
1917 @option{--suppress-blank-empty} option.  It causes @command{diff} to
1918 omit trailing blanks at the end of output lines in normal, context,
1919 and unified format, unless the trailing blanks were already present in
1920 the input.  This changes the output format slightly, so that output
1921 lines are guaranteed to never end in a blank unless an input line ends
1922 in a blank.  This format is less likely to be munged by text editors
1923 or by transmission via email.  It is accepted by @acronym{GNU}
1924 @command{patch} as well.
1925
1926 @node Pagination
1927 @section Paginating @command{diff} Output
1928 @cindex paginating @command{diff} output
1929
1930 It can be convenient to have long output page-numbered and time-stamped.
1931 The @option{--paginate} (@option{-l}) option does this by sending the
1932 @command{diff} output through the @command{pr} program.  Here is what the page
1933 header might look like for @samp{diff -lc lao tzu}:
1934
1935 @example
1936 2002-02-22 14:20                 diff -lc lao tzu                 Page 1
1937 @end example
1938
1939 @node diff Performance
1940 @chapter @command{diff} Performance Tradeoffs
1941 @cindex performance of @command{diff}
1942
1943 @acronym{GNU} @command{diff} runs quite efficiently; however, in some
1944 circumstances you can cause it to run faster or produce a more compact
1945 set of changes.
1946
1947 One way to improve @command{diff} performance is to use hard or
1948 symbolic links to files instead of copies.  This improves performance
1949 because @command{diff} normally does not need to read two hard or
1950 symbolic links to the same file, since their contents must be
1951 identical.  For example, suppose you copy a large directory hierarchy,
1952 make a few changes to the copy, and then often use @samp{diff -r} to
1953 compare the original to the copy.  If the original files are
1954 read-only, you can greatly improve performance by creating the copy
1955 using hard or symbolic links (e.g., with @acronym{GNU} @samp{cp -lR} or
1956 @samp{cp -sR}).  Before editing a file in the copy for the first time,
1957 you should break the link and replace it with a regular copy.
1958
1959 You can also affect the performance of @acronym{GNU} @command{diff} by
1960 giving it options that change the way it compares files.
1961 Performance has more than one dimension.  These options improve one
1962 aspect of performance at the cost of another, or they improve
1963 performance in some cases while hurting it in others.
1964
1965 The way that @acronym{GNU} @command{diff} determines which lines have
1966 changed always comes up with a near-minimal set of differences.
1967 Usually it is good enough for practical purposes.  If the
1968 @command{diff} output is large, you might want @command{diff} to use a
1969 modified algorithm that sometimes produces a smaller set of
1970 differences.  The @option{--minimal} (@option{-d}) option does this;
1971 however, it can also cause @command{diff} to run more slowly than
1972 usual, so it is not the default behavior.
1973
1974 When the files you are comparing are large and have small groups of
1975 changes scattered throughout them, you can use the
1976 @option{--speed-large-files} option to make a different modification to
1977 the algorithm that @command{diff} uses.  If the input files have a constant
1978 small density of changes, this option speeds up the comparisons without
1979 changing the output.  If not, @command{diff} might produce a larger set of
1980 differences; however, the output will still be correct.
1981
1982 Normally @command{diff} discards the prefix and suffix that is common to
1983 both files before it attempts to find a minimal set of differences.
1984 This makes @command{diff} run faster, but occasionally it may produce
1985 non-minimal output.  The @option{--horizon-lines=@var{lines}} option
1986 prevents @command{diff} from discarding the last @var{lines} lines of the
1987 prefix and the first @var{lines} lines of the suffix.  This gives
1988 @command{diff} further opportunities to find a minimal output.
1989
1990 Suppose a run of changed lines includes a sequence of lines at one end
1991 and there is an identical sequence of lines just outside the other end.
1992 The @command{diff} command is free to choose which identical sequence is
1993 included in the hunk.  In this case, @command{diff} normally shifts the
1994 hunk's boundaries when this merges adjacent hunks, or shifts a hunk's
1995 lines towards the end of the file.  Merging hunks can make the output
1996 look nicer in some cases.
1997
1998 @node Comparing Three Files
1999 @chapter Comparing Three Files
2000 @cindex comparing three files
2001 @cindex format of @command{diff3} output
2002
2003 Use the program @command{diff3} to compare three files and show any
2004 differences among them.  (@command{diff3} can also merge files; see
2005 @ref{diff3 Merging}).
2006
2007 The ``normal'' @command{diff3} output format shows each hunk of
2008 differences without surrounding context.  Hunks are labeled depending
2009 on whether they are two-way or three-way, and lines are annotated by
2010 their location in the input files.
2011
2012 @xref{Invoking diff3}, for more information on how to run @command{diff3}.
2013
2014 @menu
2015 * Sample diff3 Input::    Sample @command{diff3} input for examples.
2016 * Example diff3 Normal::  Sample output in the normal format.
2017 * Detailed diff3 Normal:: A detailed description of normal output format.
2018 * diff3 Hunks::           The format of normal output format.
2019 @end menu
2020
2021 @node Sample diff3 Input
2022 @section A Third Sample Input File
2023 @cindex @command{diff3} sample input
2024 @cindex sample input for @command{diff3}
2025
2026 Here is a third sample file that will be used in examples to illustrate
2027 the output of @command{diff3} and how various options can change it.  The
2028 first two files are the same that we used for @command{diff} (@pxref{Sample
2029 diff Input}).  This is the third sample file, called @file{tao}:
2030
2031 @example
2032 The Way that can be told of is not the eternal Way;
2033 The name that can be named is not the eternal name.
2034 The Nameless is the origin of Heaven and Earth;
2035 The named is the mother of all things.
2036
2037 Therefore let there always be non-being,
2038   so we may see their subtlety,
2039 And let there always be being,
2040   so we may see their result.
2041 The two are the same,
2042 But after they are produced,
2043   they have different names.
2044
2045   -- The Way of Lao-Tzu, tr. Wing-tsit Chan
2046 @end example
2047
2048 @node Example diff3 Normal
2049 @section An Example of @command{diff3} Normal Format
2050
2051 Here is the output of the command @samp{diff3 lao tzu tao}
2052 (@pxref{Sample diff3 Input}, for the complete contents of the files).
2053 Notice that it shows only the lines that are different among the three
2054 files.
2055
2056 @example
2057 ====2
2058 1:1,2c
2059 3:1,2c
2060   The Way that can be told of is not the eternal Way;
2061   The name that can be named is not the eternal name.
2062 2:0a
2063 ====1
2064 1:4c
2065   The Named is the mother of all things.
2066 2:2,3c
2067 3:4,5c
2068   The named is the mother of all things.
2069   @-
2070 ====3
2071 1:8c
2072 2:7c
2073     so we may see their outcome.
2074 3:9c
2075     so we may see their result.
2076 ====
2077 1:11a
2078 2:11,13c
2079   They both may be called deep and profound.
2080   Deeper and more profound,
2081   The door of all subtleties!
2082 3:13,14c
2083   @-
2084     -- The Way of Lao-Tzu, tr. Wing-tsit Chan
2085 @end example
2086
2087 @node Detailed diff3 Normal
2088 @section Detailed Description of @command{diff3} Normal Format
2089
2090 Each hunk begins with a line marked @samp{====}.  Three-way hunks have
2091 plain @samp{====} lines, and two-way hunks have @samp{1}, @samp{2}, or
2092 @samp{3} appended to specify which of the three input files differ in
2093 that hunk.  The hunks contain copies of two or three sets of input
2094 lines each preceded by one or two commands identifying where the lines
2095 came from.
2096
2097 Normally, two spaces precede each copy of an input line to distinguish
2098 it from the commands.  But with the @option{--initial-tab} (@option{-T})
2099 option, @command{diff3} uses a tab instead of two spaces; this lines up
2100 tabs correctly.  @xref{Tabs}, for more information.
2101
2102 Commands take the following forms:
2103
2104 @table @samp
2105 @item @var{file}:@var{l}a
2106 This hunk appears after line @var{l} of file @var{file}, and
2107 contains no lines in that file.  To edit this file to yield the other
2108 files, one must append hunk lines taken from the other files.  For
2109 example, @samp{1:11a} means that the hunk follows line 11 in the first
2110 file and contains no lines from that file.
2111
2112 @item @var{file}:@var{r}c
2113 This hunk contains the lines in the range @var{r} of file @var{file}.
2114 The range @var{r} is a comma-separated pair of line numbers, or just one
2115 number if there is only one line.  To edit this file to yield the
2116 other files, one must change the specified lines to be the lines taken
2117 from the other files.  For example, @samp{2:11,13c} means that the hunk
2118 contains lines 11 through 13 from the second file.
2119 @end table
2120
2121 If the last line in a set of input lines is incomplete
2122 (@pxref{Incomplete Lines}), it is distinguished on output from a full
2123 line by a following line that starts with @samp{\}.
2124
2125 @node diff3 Hunks
2126 @section @command{diff3} Hunks
2127 @cindex hunks for @command{diff3}
2128 @cindex @command{diff3} hunks
2129
2130 Groups of lines that differ in two or three of the input files are
2131 called @dfn{diff3 hunks}, by analogy with @command{diff} hunks
2132 (@pxref{Hunks}).  If all three input files differ in a @command{diff3}
2133 hunk, the hunk is called a @dfn{three-way hunk}; if just two input files
2134 differ, it is a @dfn{two-way hunk}.
2135
2136 As with @command{diff}, several solutions are possible.  When comparing the
2137 files @samp{A}, @samp{B}, and @samp{C}, @command{diff3} normally finds
2138 @command{diff3} hunks by merging the two-way hunks output by the two
2139 commands @samp{diff A B} and @samp{diff A C}.  This does not necessarily
2140 minimize the size of the output, but exceptions should be rare.
2141
2142 For example, suppose @file{F} contains the three lines @samp{a},
2143 @samp{b}, @samp{f}, @file{G} contains the lines @samp{g}, @samp{b},
2144 @samp{g}, and @file{H} contains the lines @samp{a}, @samp{b},
2145 @samp{h}.  @samp{diff3 F G H} might output the following:
2146
2147 @example
2148 ====2
2149 1:1c
2150 3:1c
2151   a
2152 2:1c
2153   g
2154 ====
2155 1:3c
2156   f
2157 2:3c
2158   g
2159 3:3c
2160   h
2161 @end example
2162
2163 @noindent
2164 because it found a two-way hunk containing @samp{a} in the first and
2165 third files and @samp{g} in the second file, then the single line
2166 @samp{b} common to all three files, then a three-way hunk containing
2167 the last line of each file.
2168
2169 @node diff3 Merging
2170 @chapter Merging From a Common Ancestor
2171 @cindex merging from a common ancestor
2172
2173 When two people have made changes to copies of the same file,
2174 @command{diff3} can produce a merged output that contains both sets of
2175 changes together with warnings about conflicts.
2176
2177 One might imagine programs with names like @command{diff4} and @command{diff5}
2178 to compare more than three files simultaneously, but in practice the
2179 need rarely arises.  You can use @command{diff3} to merge three or more
2180 sets of changes to a file by merging two change sets at a time.
2181
2182 @command{diff3} can incorporate changes from two modified versions into a
2183 common preceding version.  This lets you merge the sets of changes
2184 represented by the two newer files.  Specify the common ancestor version
2185 as the second argument and the two newer versions as the first and third
2186 arguments, like this:
2187
2188 @example
2189 diff3 @var{mine} @var{older} @var{yours}
2190 @end example
2191
2192 @noindent
2193 You can remember the order of the arguments by noting that they are in
2194 alphabetical order.
2195
2196 @cindex conflict
2197 @cindex overlap
2198 You can think of this as subtracting @var{older} from @var{yours} and
2199 adding the result to @var{mine}, or as merging into @var{mine} the
2200 changes that would turn @var{older} into @var{yours}.  This merging is
2201 well-defined as long as @var{mine} and @var{older} match in the
2202 neighborhood of each such change.  This fails to be true when all three
2203 input files differ or when only @var{older} differs; we call this
2204 a @dfn{conflict}.  When all three input files differ, we call the
2205 conflict an @dfn{overlap}.
2206
2207 @command{diff3} gives you several ways to handle overlaps and conflicts.
2208 You can omit overlaps or conflicts, or select only overlaps,
2209 or mark conflicts with special @samp{<<<<<<<} and @samp{>>>>>>>} lines.
2210
2211 @command{diff3} can output the merge results as an @command{ed} script that
2212 that can be applied to the first file to yield the merged output.
2213 However, it is usually better to have @command{diff3} generate the merged
2214 output directly; this bypasses some problems with @command{ed}.
2215
2216 @menu
2217 * Which Changes::            Selecting changes to incorporate.
2218 * Marking Conflicts::        Marking conflicts.
2219 * Bypassing ed::             Generating merged output directly.
2220 * Merging Incomplete Lines:: How @command{diff3} merges incomplete lines.
2221 * Saving the Changed File::  Emulating System V behavior.
2222 @end menu
2223
2224 @node Which Changes
2225 @section Selecting Which Changes to Incorporate
2226 @cindex overlapping change, selection of
2227 @cindex unmerged change
2228
2229 You can select all unmerged changes from @var{older} to @var{yours} for merging
2230 into @var{mine} with the @option{--ed} (@option{-e}) option.  You can
2231 select only the nonoverlapping unmerged changes with
2232 @option{--easy-only} (@option{-3}),
2233 and you can select only the overlapping changes with
2234 @option{--overlap-only} (@option{-x}).
2235
2236 The @option{-e}, @option{-3} and @option{-x} options select only
2237 @dfn{unmerged changes}, i.e.@: changes where @var{mine} and @var{yours}
2238 differ; they ignore changes from @var{older} to @var{yours} where
2239 @var{mine} and @var{yours} are identical, because they assume that such
2240 changes have already been merged.  If this assumption is not a safe
2241 one, you can use the @option{--show-all} (@option{-A}) option
2242 (@pxref{Marking Conflicts}).
2243
2244 Here is the output of the command @command{diff3} with each of these three
2245 options (@pxref{Sample diff3 Input}, for the complete contents of the files).
2246 Notice that @option{-e} outputs the union of the disjoint sets of changes
2247 output by @option{-3} and @option{-x}.
2248
2249 Output of @samp{diff3 -e lao tzu tao}:
2250 @example
2251 11a
2252
2253   -- The Way of Lao-Tzu, tr. Wing-tsit Chan
2254 .
2255 8c
2256   so we may see their result.
2257 .
2258 @end example
2259
2260 Output of @samp{diff3 -3 lao tzu tao}:
2261 @example
2262 8c
2263   so we may see their result.
2264 .
2265 @end example
2266
2267 Output of @samp{diff3 -x lao tzu tao}:
2268 @example
2269 11a
2270
2271   -- The Way of Lao-Tzu, tr. Wing-tsit Chan
2272 .
2273 @end example
2274
2275 @node Marking Conflicts
2276 @section Marking Conflicts
2277 @cindex conflict marking
2278 @cindex @samp{<<<<<<<} for marking conflicts
2279
2280 @command{diff3} can mark conflicts in the merged output by
2281 bracketing them with special marker lines.  A conflict
2282 that comes from two files @var{A} and @var{B} is marked as follows:
2283
2284 @example
2285 <<<<<<< @var{A}
2286 @r{lines from @var{A}}
2287 =======
2288 @r{lines from @var{B}}
2289 >>>>>>> @var{B}
2290 @end example
2291
2292 A conflict that comes from three files @var{A}, @var{B} and @var{C} is
2293 marked as follows:
2294
2295 @example
2296 <<<<<<< @var{A}
2297 @r{lines from @var{A}}
2298 ||||||| @var{B}
2299 @r{lines from @var{B}}
2300 =======
2301 @r{lines from @var{C}}
2302 >>>>>>> @var{C}
2303 @end example
2304
2305 The @option{--show-all} (@option{-A}) option acts like the @option{-e}
2306 option, except that it brackets conflicts, and it outputs all changes
2307 from @var{older} to @var{yours}, not just the unmerged changes.  Thus,
2308 given the sample input files (@pxref{Sample diff3 Input}), @samp{diff3
2309 -A lao tzu tao} puts brackets around the conflict where only @file{tzu}
2310 differs:
2311
2312 @example
2313 <<<<<<< tzu
2314 =======
2315 The Way that can be told of is not the eternal Way;
2316 The name that can be named is not the eternal name.
2317 >>>>>>> tao
2318 @end example
2319
2320 And it outputs the three-way conflict as follows:
2321
2322 @example
2323 <<<<<<< lao
2324 ||||||| tzu
2325 They both may be called deep and profound.
2326 Deeper and more profound,
2327 The door of all subtleties!
2328 =======
2329
2330   -- The Way of Lao-Tzu, tr. Wing-tsit Chan
2331 >>>>>>> tao
2332 @end example
2333
2334 The @option{--show-overlap} (@option{-E}) option outputs less information
2335 than the @option{--show-all} (@option{-A}) option, because it outputs only
2336 unmerged changes, and it never outputs the contents of the second
2337 file.  Thus the @option{-E} option acts like the @option{-e} option,
2338 except that it brackets the first and third files from three-way
2339 overlapping changes.  Similarly, @option{-X} acts like @option{-x}, except
2340 it brackets all its (necessarily overlapping) changes.  For example,
2341 for the three-way overlapping change above, the @option{-E} and @option{-X}
2342 options output the following:
2343
2344 @example
2345 <<<<<<< lao
2346 =======
2347
2348   -- The Way of Lao-Tzu, tr. Wing-tsit Chan
2349 >>>>>>> tao
2350 @end example
2351
2352 If you are comparing files that have meaningless or uninformative names,
2353 you can use the @option{--label=@var{label}}
2354 option to show alternate names in the @samp{<<<<<<<}, @samp{|||||||}
2355 and @samp{>>>>>>>} brackets.  This option can be given up to three
2356 times, once for each input file.  Thus @samp{diff3 -A --label X
2357 --label Y --label Z A
2358 B C} acts like @samp{diff3 -A A B C}, except that the output looks like
2359 it came from files named @samp{X}, @samp{Y} and @samp{Z} rather than
2360 from files named @samp{A}, @samp{B} and @samp{C}.
2361
2362 @node Bypassing ed
2363 @section Generating the Merged Output Directly
2364 @cindex merged @command{diff3} format
2365
2366 With the @option{--merge} (@option{-m}) option, @command{diff3} outputs the
2367 merged file directly.  This is more efficient than using @command{ed} to
2368 generate it, and works even with non-text files that @command{ed} would
2369 reject.  If you specify @option{-m} without an @command{ed} script option,
2370 @option{-A} is assumed.
2371
2372 For example, the command @samp{diff3 -m lao tzu tao}
2373 (@pxref{Sample diff3 Input} for a copy of the input files) would output
2374 the following:
2375
2376 @example
2377 <<<<<<< tzu
2378 =======
2379 The Way that can be told of is not the eternal Way;
2380 The name that can be named is not the eternal name.
2381 >>>>>>> tao
2382 The Nameless is the origin of Heaven and Earth;
2383 The Named is the mother of all things.
2384 Therefore let there always be non-being,
2385   so we may see their subtlety,
2386 And let there always be being,
2387   so we may see their result.
2388 The two are the same,
2389 But after they are produced,
2390   they have different names.
2391 <<<<<<< lao
2392 ||||||| tzu
2393 They both may be called deep and profound.
2394 Deeper and more profound,
2395 The door of all subtleties!
2396 =======
2397
2398   -- The Way of Lao-Tzu, tr. Wing-tsit Chan
2399 >>>>>>> tao
2400 @end example
2401
2402 @node Merging Incomplete Lines
2403 @section How @command{diff3} Merges Incomplete Lines
2404 @cindex incomplete line merging
2405
2406 With @option{-m}, incomplete lines (@pxref{Incomplete Lines}) are simply
2407 copied to the output as they are found; if the merged output ends in an
2408 conflict and one of the input files ends in an incomplete
2409 line, succeeding @samp{|||||||}, @samp{=======} or @samp{>>>>>>>}
2410 brackets appear somewhere other than the start of a line because
2411 they are appended to the incomplete line.
2412
2413 Without @option{-m}, if an @command{ed} script option is specified and an
2414 incomplete line is found, @command{diff3} generates a warning and acts as
2415 if a newline had been present.
2416
2417 @node Saving the Changed File
2418 @section Saving the Changed File
2419 @cindex System V @command{diff3} compatibility
2420
2421 Traditional Unix @command{diff3} generates an @command{ed} script without the
2422 trailing @samp{w} and @samp{q} commands that save the changes.
2423 System V @command{diff3} generates these extra commands.  @acronym{GNU}
2424 @command{diff3} normally behaves like traditional Unix
2425 @command{diff3}, but with the @option{-i} option it behaves like
2426 System V @command{diff3} and appends the @samp{w} and @samp{q}
2427 commands.
2428
2429 The @option{-i} option requires one of the @command{ed} script options
2430 @option{-AeExX3}, and is incompatible with the merged output option
2431 @option{-m}.
2432
2433 @node Interactive Merging
2434 @chapter Interactive Merging with @command{sdiff}
2435 @cindex diff merging
2436 @cindex interactive merging
2437
2438 With @command{sdiff}, you can merge two files interactively based on a
2439 side-by-side @option{-y} format comparison (@pxref{Side by Side}).  Use
2440 @option{--output=@var{file}} (@option{-o @var{file}}) to specify where to
2441 put the merged text.  @xref{Invoking sdiff}, for more details on the
2442 options to @command{sdiff}.
2443
2444 Another way to merge files interactively is to use the Emacs Lisp
2445 package @command{emerge}.  @xref{emerge, , emerge, emacs, The
2446 @acronym{GNU} Emacs Manual}, for more information.
2447
2448 @menu
2449 * sdiff Option Summary:: Summary of @command{sdiff} options.
2450 * Merge Commands::       Merging two files interactively.
2451 @end menu
2452
2453 @node sdiff Option Summary
2454 @section Specifying @command{diff} Options to @command{sdiff}
2455 @cindex @command{sdiff} output format
2456
2457 The following @command{sdiff} options have the same meaning as for
2458 @command{diff}.  @xref{diff Options}, for the use of these options.
2459
2460 @example
2461 -a -b -d -i -t -v
2462 -B -E -I @var{regexp} -Z
2463
2464 --expand-tabs
2465 --ignore-blank-lines  --ignore-case
2466 --ignore-matching-lines=@var{regexp}  --ignore-space-change
2467 --ignore-tab-expansion  --ignore-trailing-space
2468 --left-column  --minimal  --speed-large-files
2469 --strip-trailing-cr  --suppress-common-lines
2470 --tabsize=@var{columns}  --text  --version  --width=@var{columns}
2471 @end example
2472
2473 For historical reasons, @command{sdiff} has alternate names for some
2474 options.  The @option{-l} option is equivalent to the
2475 @option{--left-column} option, and similarly @option{-s} is equivalent
2476 to @option{--suppress-common-lines}.  The meaning of the @command{sdiff}
2477 @option{-w} and @option{-W} options is interchanged from that of
2478 @command{diff}: with @command{sdiff}, @option{-w @var{columns}} is
2479 equivalent to @option{--width=@var{columns}}, and @option{-W} is
2480 equivalent to @option{--ignore-all-space}.  @command{sdiff} without the
2481 @option{-o} option is equivalent to @command{diff} with the
2482 @option{--side-by-side} (@option{-y}) option (@pxref{Side by Side}).
2483
2484 @node Merge Commands
2485 @section Merge Commands
2486 @cindex merge commands
2487 @cindex merging interactively
2488
2489 Groups of common lines, with a blank gutter, are copied from the first
2490 file to the output.  After each group of differing lines, @command{sdiff}
2491 prompts with @samp{%} and pauses, waiting for one of the following
2492 commands.  Follow each command with @key{RET}.
2493
2494 @table @samp
2495 @item e
2496 Discard both versions.
2497 Invoke a text editor on an empty temporary file,
2498 then copy the resulting file to the output.
2499
2500 @item eb
2501 Concatenate the two versions, edit the result in a temporary file,
2502 then copy the edited result to the output.
2503
2504 @item ed
2505 Like @samp{eb}, except precede each version with a header that
2506 shows what file and lines the version came from.
2507
2508 @item el
2509 @itemx e1
2510 Edit a copy of the left version, then copy the result to the output.
2511
2512 @item er
2513 @itemx e2
2514 Edit a copy of the right version, then copy the result to the output.
2515
2516 @item l
2517 @itemx 1
2518 Copy the left version to the output.
2519
2520 @item q
2521 Quit.
2522
2523 @item r
2524 @itemx 2
2525 Copy the right version to the output.
2526
2527 @item s
2528 Silently copy common lines.
2529
2530 @item v
2531 Verbosely copy common lines.  This is the default.
2532 @end table
2533
2534 @vindex EDITOR
2535 The text editor invoked is specified by the @env{EDITOR} environment
2536 variable if it is set.  The default is system-dependent.
2537
2538 @node Merging with patch
2539 @chapter Merging with @command{patch}
2540
2541 @command{patch} takes comparison output produced by @command{diff} and applies
2542 the differences to a copy of the original file, producing a patched
2543 version.  With @command{patch}, you can distribute just the changes to a
2544 set of files instead of distributing the entire file set; your
2545 correspondents can apply @command{patch} to update their copy of the files
2546 with your changes.  @command{patch} automatically determines the diff
2547 format, skips any leading or trailing headers, and uses the headers to
2548 determine which file to patch.  This lets your correspondents feed a
2549 mail message containing a difference listing directly to
2550 @command{patch}.
2551
2552 @command{patch} detects and warns about common problems like forward
2553 patches.  It saves any patches that it could not apply.  It can also maintain a
2554 @code{patchlevel.h} file to ensure that your correspondents apply
2555 diffs in the proper order.
2556
2557 @command{patch} accepts a series of diffs in its standard input, usually
2558 separated by headers that specify which file to patch.  It applies
2559 @command{diff} hunks (@pxref{Hunks}) one by one.  If a hunk does not
2560 exactly match the original file, @command{patch} uses heuristics to try to
2561 patch the file as well as it can.  If no approximate match can be found,
2562 @command{patch} rejects the hunk and skips to the next hunk.  @command{patch}
2563 normally replaces each file @var{f} with its new version, putting reject
2564 hunks (if any) into @samp{@var{f}.rej}.
2565
2566 @xref{Invoking patch}, for detailed information on the options to
2567 @command{patch}.
2568
2569 @menu
2570 * patch Input::            Selecting the type of @command{patch} input.
2571 * Revision Control::       Getting files from @acronym{RCS}, @acronym{SCCS}, etc.
2572 * Imperfect::              Dealing with imperfect patches.
2573 * Creating and Removing::  Creating and removing files with a patch.
2574 * Patching Time Stamps::   Updating time stamps on patched files.
2575 * Multiple Patches::       Handling multiple patches in a file.
2576 * patch Directories::      Changing directory and stripping directories.
2577 * Backups::                Whether backup files are made.
2578 * Backup Names::           Backup file names.
2579 * Reject Names::           Reject file names.
2580 * patch Messages::         Messages and questions @command{patch} can produce.
2581 * patch and POSIX::        Conformance to the @acronym{POSIX} standard.
2582 * patch and Tradition::    @acronym{GNU} versus traditional @command{patch}.
2583 @end menu
2584
2585 @node patch Input
2586 @section Selecting the @command{patch} Input Format
2587 @cindex @command{patch} input format
2588
2589 @command{patch} normally determines which @command{diff} format the patch
2590 file uses by examining its contents.  For patch files that contain
2591 particularly confusing leading text, you might need to use one of the
2592 following options to force @command{patch} to interpret the patch file as a
2593 certain format of diff.  The output formats listed here are the only
2594 ones that @command{patch} can understand.
2595
2596 @table @option
2597 @item -c
2598 @itemx --context
2599 context diff.
2600
2601 @item -e
2602 @itemx --ed
2603 @command{ed} script.
2604
2605 @item -n
2606 @itemx --normal
2607 normal diff.
2608
2609 @item -u
2610 @itemx --unified
2611 unified diff.
2612 @end table
2613
2614 @node Revision Control
2615 @section Revision Control
2616 @cindex revision control
2617 @cindex version control
2618 @cindex @acronym{RCS}
2619 @cindex ClearCase
2620 @cindex @acronym{SCCS}
2621
2622 If a nonexistent input file is under a revision control system
2623 supported by @command{patch}, @command{patch} normally asks the user
2624 whether to get (or check out) the file from the revision control
2625 system.  Patch currently supports @acronym{RCS}, ClearCase and
2626 @acronym{SCCS}.  Under @acronym{RCS} and @acronym{SCCS},
2627 @command{patch} also asks when the input file is read-only and matches
2628 the default version in the revision control system.
2629
2630 @vindex PATCH_GET
2631 The @option{--get=@var{num}} (@option{-g @var{num}}) option affects access
2632 to files under supported revision control systems.  If @var{num} is
2633 positive, @command{patch} gets the file without asking the user; if
2634 zero, @command{patch} neither asks the user nor gets the file; and if
2635 negative, @command{patch} asks the user before getting the file.  The
2636 default value of @var{num} is given by the value of the
2637 @env{PATCH_GET} environment variable if it is set; if not, the default
2638 value is zero if @command{patch} is conforming to @acronym{POSIX}, negative
2639 otherwise.  @xref{patch and POSIX}.
2640
2641 @vindex VERSION_CONTROL
2642 The choice of revision control system is unaffected by the
2643 @env{VERSION_CONTROL} environment variable (@pxref{Backup Names}).
2644
2645 @node Imperfect
2646 @section Applying Imperfect Patches
2647 @cindex imperfect patch application
2648
2649 @command{patch} tries to skip any leading text in the patch file,
2650 apply the diff, and then skip any trailing text.  Thus you can feed a
2651 mail message directly to @command{patch}, and it should work.  If the
2652 entire diff is indented by a constant amount of white space,
2653 @command{patch} automatically ignores the indentation.  If a context
2654 diff contains trailing carriage return on each line, @command{patch}
2655 automatically ignores the carriage return.  If a context diff has been
2656 encapsulated by prepending @w{@samp{- }} to lines beginning with @samp{-}
2657 as per @uref{ftp://ftp.isi.edu/in-notes/rfc934.txt, Internet RFC 934},
2658 @command{patch} automatically unencapsulates the input.
2659
2660 However, certain other types of imperfect input require user
2661 intervention or testing.
2662
2663 @menu
2664 * Changed White Space:: When tabs and spaces don't match exactly.
2665 * Reversed Patches::    Applying reversed patches correctly.
2666 * Inexact::             Helping @command{patch} find close matches.
2667 * Dry Runs::            Predicting what @command{patch} will do.
2668 @end menu
2669
2670 @node Changed White Space
2671 @subsection Applying Patches with Changed White Space
2672 @cindex white space in patches
2673
2674 Sometimes mailers, editors, or other programs change spaces into tabs,
2675 or vice versa.  If this happens to a patch file or an input file, the
2676 files might look the same, but @command{patch} will not be able to match
2677 them properly.  If this problem occurs, use the @option{-l} or
2678 @option{--ignore-white-space} option, which makes @command{patch} compare
2679 blank characters (i.e.@: spaces and tabs) loosely so that any nonempty
2680 sequence of blanks in the patch file matches any nonempty sequence of
2681 blanks in the input files.  Non-blank
2682 characters must still match exactly.  Each line of the context must
2683 still match a line in the input file.
2684
2685 @node Reversed Patches
2686 @subsection Applying Reversed Patches
2687 @cindex reversed patches
2688
2689 Sometimes people run @command{diff} with the new file first instead of
2690 second.  This creates a diff that is ``reversed''.  To apply such
2691 patches, give @command{patch} the @option{--reverse} (@option{-R}) option.
2692 @command{patch} then attempts to swap each hunk around before applying it.
2693 Rejects come out in the swapped format.
2694
2695 Often @command{patch} can guess that the patch is reversed.  If the first
2696 hunk of a patch fails, @command{patch} reverses the hunk to see if it can
2697 apply it that way.  If it can, @command{patch} asks you if you want to have
2698 the @option{-R} option set; if it can't, @command{patch} continues to apply
2699 the patch normally.  This method cannot detect a reversed patch if it is
2700 a normal diff and the first command is an append (which should have been
2701 a delete) since appends always succeed, because a null context matches
2702 anywhere.  But most patches add or change lines rather than delete them,
2703 so most reversed normal diffs begin with a delete, which fails, and
2704 @command{patch} notices.
2705
2706 If you apply a patch that you have already applied, @command{patch} thinks
2707 it is a reversed patch and offers to un-apply the patch.  This could be
2708 construed as a feature.  If you did this inadvertently and you don't
2709 want to un-apply the patch, just answer @samp{n} to this offer and to
2710 the subsequent ``apply anyway'' question---or type @kbd{C-c} to kill the
2711 @command{patch} process.
2712
2713 @node Inexact
2714 @subsection Helping @command{patch} Find Inexact Matches
2715 @cindex inexact patches
2716 @cindex fuzz factor when patching
2717
2718 For context diffs, and to a lesser extent normal diffs, @command{patch} can
2719 detect when the line numbers mentioned in the patch are incorrect, and
2720 it attempts to find the correct place to apply each hunk of the patch.
2721 As a first guess, it takes the line number mentioned in the hunk, plus
2722 or minus any offset used in applying the previous hunk.  If that is not
2723 the correct place, @command{patch} scans both forward and backward for a
2724 set of lines matching the context given in the hunk.
2725
2726 First @command{patch} looks for a place where all lines of the context
2727 match.  If it cannot find such a place, and it is reading a context or
2728 unified diff, and the maximum fuzz factor is set to 1 or more, then
2729 @command{patch} makes another scan, ignoring the first and last line of
2730 context.  If that fails, and the maximum fuzz factor is set to 2 or
2731 more, it makes another scan, ignoring the first two and last two lines
2732 of context are ignored.  It continues similarly if the maximum fuzz
2733 factor is larger.
2734
2735 The @option{--fuzz=@var{lines}} (@option{-F @var{lines}}) option sets the
2736 maximum fuzz factor to @var{lines}.  This option only applies to context
2737 and unified diffs; it ignores up to @var{lines} lines while looking for
2738 the place to install a hunk.  Note that a larger fuzz factor increases
2739 the odds of making a faulty patch.  The default fuzz factor is 2; there
2740 is no point to setting it to more than the number of lines of context
2741 in the diff, ordinarily 3.
2742
2743 If @command{patch} cannot find a place to install a hunk of the patch, it
2744 writes the hunk out to a reject file (@pxref{Reject Names}, for information
2745 on how reject files are named).  It writes out rejected hunks in context
2746 format no matter what form the input patch is in.  If the input is a
2747 normal or @command{ed} diff, many of the contexts are simply null.  The
2748 line numbers on the hunks in the reject file may be different from those
2749 in the patch file: they show the approximate location where @command{patch}
2750 thinks the failed hunks belong in the new file rather than in the old
2751 one.
2752
2753 If the @option{--verbose} option is given, then
2754 as it completes each hunk @command{patch} tells you whether the hunk
2755 succeeded or failed, and if it failed, on which line (in the new file)
2756 @command{patch} thinks the hunk should go.  If this is different from the
2757 line number specified in the diff, it tells you the offset.  A single
2758 large offset @emph{may} indicate that @command{patch} installed a hunk in
2759 the wrong place.  @command{patch} also tells you if it used a fuzz factor
2760 to make the match, in which case you should also be slightly suspicious.
2761
2762 @command{patch} cannot tell if the line numbers are off in an @command{ed}
2763 script, and can only detect wrong line numbers in a normal diff when it
2764 finds a change or delete command.  It may have the same problem with a
2765 context diff using a fuzz factor equal to or greater than the number of
2766 lines of context shown in the diff (typically 3).  In these cases, you
2767 should probably look at a context diff between your original and patched
2768 input files to see if the changes make sense.  Compiling without errors
2769 is a pretty good indication that the patch worked, but not a guarantee.
2770
2771 A patch against an empty file applies to a nonexistent file, and vice
2772 versa.  @xref{Creating and Removing}.
2773
2774 @command{patch} usually produces the correct results, even when it must
2775 make many guesses.  However, the results are guaranteed only when
2776 the patch is applied to an exact copy of the file that the patch was
2777 generated from.
2778
2779 @node Dry Runs
2780 @subsection Predicting what @command{patch} will do
2781 @cindex testing @command{patch}
2782 @cindex dry runs for @command{patch}
2783
2784 It may not be obvious in advance what @command{patch} will do with a
2785 complicated or poorly formatted patch.  If you are concerned that the
2786 input might cause @command{patch} to modify the wrong files, you can
2787 use the @option{--dry-run} option, which causes @command{patch} to
2788 print the results of applying patches without actually changing any
2789 files.  You can then inspect the diagnostics generated by the dry run
2790 to see whether @command{patch} will modify the files that you expect.
2791 If the patch does not do what you want, you can modify the patch (or
2792 the other options to @command{patch}) and try another dry run.  Once
2793 you are satisfied with the proposed patch you can apply it by invoking
2794 @command{patch} as before, but this time without the
2795 @option{--dry-run} option.
2796
2797 @node Creating and Removing
2798 @section Creating and Removing Files
2799 @cindex creating files
2800 @cindex empty files, removing
2801 @cindex removing empty files
2802
2803 Sometimes when comparing two directories, a file may exist in one
2804 directory but not the other.  If you give @command{diff} the
2805 @option{--new-file} (@option{-N}) option, or if you supply an old or
2806 new file that is named @file{/dev/null} or is empty and is dated the
2807 Epoch (1970-01-01 00:00:00 UTC), @command{diff} outputs a patch that
2808 adds or deletes the contents of this file.  When given such a patch,
2809 @command{patch} normally creates a new file or removes the old file.
2810 However, when conforming to @acronym{POSIX} (@pxref{patch and POSIX}),
2811 @command{patch} does not remove the old file, but leaves it empty.
2812 The @option{--remove-empty-files} (@option{-E}) option causes
2813 @command{patch} to remove output files that are empty after applying a
2814 patch, even if the patch does not appear to be one that removed the
2815 file.
2816
2817 If the patch appears to create a file that already exists,
2818 @command{patch} asks for confirmation before applying the patch.
2819
2820 @node Patching Time Stamps
2821 @section Updating Time Stamps on Patched Files
2822 @cindex time stamps on patched files
2823
2824 When @command{patch} updates a file, it normally sets the file's
2825 last-modified time stamp to the current time of day.  If you are using
2826 @command{patch} to track a software distribution, this can cause
2827 @command{make} to incorrectly conclude that a patched file is out of
2828 date.  For example, if @file{syntax.c} depends on @file{syntax.y}, and
2829 @command{patch} updates @file{syntax.c} and then @file{syntax.y}, then
2830 @file{syntax.c} will normally appear to be out of date with respect to
2831 @file{syntax.y} even though its contents are actually up to date.
2832
2833 The @option{--set-utc} (@option{-Z}) option causes @command{patch} to
2834 set a patched file's modification and access times to the time stamps
2835 given in context diff headers.  If the context diff headers do not
2836 specify a time zone, they are assumed to use Coordinated Universal
2837 Time (@acronym{UTC}, often known as @acronym{GMT}).
2838
2839 The @option{--set-time} (@option{-T}) option acts like @option{-Z} or
2840 @option{--set-utc}, except that it assumes that the context diff
2841 headers' time stamps use local time instead of @acronym{UTC}.  This option
2842 is not recommended, because patches using local time cannot easily be
2843 used by people in other time zones, and because local time stamps are
2844 ambiguous when local clocks move backwards during daylight-saving time
2845 adjustments.  If the context diff headers specify a time zone, this
2846 option is equivalent to @option{--set-utc} (@option{-Z}).
2847
2848 @command{patch} normally refrains from setting a file's time stamps if
2849 the file's original last-modified time stamp does not match the time
2850 given in the diff header, of if the file's contents do not exactly
2851 match the patch.  However, if the @option{--force} (@option{-f})
2852 option is given, the file's time stamps are set regardless.
2853
2854 Due to the limitations of the current @command{diff} format,
2855 @command{patch} cannot update the times of files whose contents have
2856 not changed.  Also, if you set file time stamps to values other than
2857 the current time of day, you should also remove (e.g., with @samp{make
2858 clean}) all files that depend on the patched files, so that later
2859 invocations of @command{make} do not get confused by the patched
2860 files' times.
2861
2862 @node Multiple Patches
2863 @section Multiple Patches in a File
2864 @cindex multiple patches
2865 @cindex intuiting file names from patches
2866
2867 If the patch file contains more than one patch, and if you do not
2868 specify an input file on the command line, @command{patch} tries to
2869 apply each patch as if they came from separate patch files.  This
2870 means that it determines the name of the file to patch for each patch,
2871 and that it examines the leading text before each patch for file names
2872 and prerequisite revision level (@pxref{Making Patches}, for more on
2873 that topic).
2874
2875 @command{patch} uses the following rules to intuit a file name from
2876 the leading text before a patch.  First, @command{patch} takes an
2877 ordered list of candidate file names as follows:
2878
2879 @itemize @bullet
2880 @item
2881 If the header is that of a context diff, @command{patch} takes the old
2882 and new file names in the header.  A name is ignored if it does not
2883 have enough slashes to satisfy the @option{-p@var{num}} or
2884 @option{--strip=@var{num}} option.  The name @file{/dev/null} is also
2885 ignored.
2886
2887 @item
2888 If there is an @samp{Index:} line in the leading garbage and if either
2889 the old and new names are both absent or if @command{patch} is
2890 conforming to @acronym{POSIX}, @command{patch} takes the name in the
2891 @samp{Index:} line.
2892
2893 @item
2894 For the purpose of the following rules, the candidate file names are
2895 considered to be in the order (old, new, index), regardless of the
2896 order that they appear in the header.
2897 @end itemize
2898
2899 @noindent
2900 Then @command{patch} selects a file name from the candidate list as
2901 follows:
2902
2903 @itemize @bullet
2904 @item
2905 If some of the named files exist, @command{patch} selects the first
2906 name if conforming to @acronym{POSIX}, and the best name otherwise.
2907
2908 @item
2909 If @command{patch} is not ignoring @acronym{RCS}, ClearCase, and @acronym{SCCS}
2910 (@pxref{Revision Control}), and no named files exist but an @acronym{RCS},
2911 ClearCase, or @acronym{SCCS} master is found, @command{patch} selects the
2912 first named file with an @acronym{RCS}, ClearCase, or @acronym{SCCS} master.
2913
2914 @item
2915 If no named files exist, no @acronym{RCS}, ClearCase, or @acronym{SCCS} master
2916 was found, some names are given, @command{patch} is not conforming to
2917 @acronym{POSIX}, and the patch appears to create a file, @command{patch}
2918 selects the best name requiring the creation of the fewest
2919 directories.
2920
2921 @item
2922 If no file name results from the above heuristics, you are asked for
2923 the name of the file to patch, and @command{patch} selects that name.
2924 @end itemize
2925
2926 To determine the @dfn{best} of a nonempty list of file names,
2927 @command{patch} first takes all the names with the fewest path name
2928 components; of those, it then takes all the names with the shortest
2929 basename; of those, it then takes all the shortest names; finally, it
2930 takes the first remaining name.
2931
2932 @xref{patch and POSIX}, to see whether @command{patch} is conforming
2933 to @acronym{POSIX}.
2934
2935 @node patch Directories
2936 @section Applying Patches in Other Directories
2937 @cindex directories and patch
2938 @cindex patching directories
2939
2940 The @option{--directory=@var{directory}} (@option{-d @var{directory}})
2941 option to @command{patch} makes directory @var{directory} the current
2942 directory for interpreting both file names in the patch file, and file
2943 names given as arguments to other options (such as @option{-B} and
2944 @option{-o}).  For example, while in a mail reading program, you can patch
2945 a file in the @file{/usr/src/emacs} directory directly from a message
2946 containing the patch like this:
2947
2948 @example
2949 | patch -d /usr/src/emacs
2950 @end example
2951
2952 Sometimes the file names given in a patch contain leading directories,
2953 but you keep your files in a directory different from the one given in
2954 the patch.  In those cases, you can use the
2955 @option{--strip=@var{number}} (@option{-p@var{number}})
2956 option to set the file name strip count to @var{number}.  The strip
2957 count tells @command{patch} how many slashes, along with the directory
2958 names between them, to strip from the front of file names.  A sequence
2959 of one or more adjacent slashes is counted as a single slash.  By
2960 default, @command{patch} strips off all leading directories, leaving
2961 just the base file names.
2962
2963 For example, suppose the file name in the patch file is
2964 @file{/gnu/src/emacs/etc/NEWS}.  Using @option{-p0} gives the
2965 entire file name unmodified, @option{-p1} gives
2966 @file{gnu/src/emacs/etc/NEWS} (no leading slash), @option{-p4} gives
2967 @file{etc/NEWS}, and not specifying @option{-p} at all gives @file{NEWS}.
2968
2969 @command{patch} looks for each file (after any slashes have been stripped)
2970 in the current directory, or if you used the @option{-d @var{directory}}
2971 option, in that directory.
2972
2973 @node Backups
2974 @section Backup Files
2975 @cindex backup file strategy
2976
2977 Normally, @command{patch} creates a backup file if the patch does not
2978 exactly match the original input file, because in that case the
2979 original data might not be recovered if you undo the patch with
2980 @samp{patch -R} (@pxref{Reversed Patches}).  However, when conforming
2981 to @acronym{POSIX}, @command{patch} does not create backup files by
2982 default.  @xref{patch and POSIX}.
2983
2984 The @option{--backup} (@option{-b}) option causes @command{patch} to
2985 make a backup file regardless of whether the patch matches the
2986 original input.  The @option{--backup-if-mismatch} option causes
2987 @command{patch} to create backup files for mismatches files; this is
2988 the default when not conforming to @acronym{POSIX}.  The
2989 @option{--no-backup-if-mismatch} option causes @command{patch} to not
2990 create backup files, even for mismatched patches; this is the default
2991 when conforming to @acronym{POSIX}.
2992
2993 When backing up a file that does not exist, an empty, unreadable
2994 backup file is created as a placeholder to represent the nonexistent
2995 file.
2996
2997 @node Backup Names
2998 @section Backup File Names
2999 @cindex backup file names
3000
3001 Normally, @command{patch} renames an original input file into a backup
3002 file by appending to its name the extension @samp{.orig}, or @samp{~}
3003 if using @samp{.orig} would make the backup file name too
3004 long.@footnote{A coding error in @acronym{GNU} @command{patch} version
3005 2.5.4 causes it to always use @samp{~}, but this should be fixed in
3006 the next release.}  The @option{-z @var{backup-suffix}} or
3007 @option{--suffix=@var{backup-suffix}} option causes @command{patch} to
3008 use @var{backup-suffix} as the backup extension instead.
3009
3010 @vindex SIMPLE_BACKUP_SUFFIX
3011 Alternately, you can specify the extension for backup files with the
3012 @env{SIMPLE_BACKUP_SUFFIX} environment variable, which the options
3013 override.
3014
3015 @command{patch} can also create numbered backup files the way
3016 @acronym{GNU} Emacs does.  With this method, instead of having a
3017 single backup of each file, @command{patch} makes a new backup file
3018 name each time it patches a file.  For example, the backups of a file
3019 named @file{sink} would be called, successively, @file{sink.~1~},
3020 @file{sink.~2~}, @file{sink.~3~}, etc.
3021
3022 @vindex PATCH_VERSION_CONTROL
3023 @vindex VERSION_CONTROL
3024 The @option{-V @var{backup-style}} or
3025 @option{--version-control=@var{backup-style}} option takes as an
3026 argument a method for creating backup file names.  You can alternately
3027 control the type of backups that @command{patch} makes with the
3028 @env{PATCH_VERSION_CONTROL} environment variable, which the
3029 @option{-V} option overrides.  If @env{PATCH_VERSION_CONTROL} is not
3030 set, the @env{VERSION_CONTROL} environment variable is used instead.
3031 Please note that these options and variables control backup file
3032 names; they do not affect the choice of revision control system
3033 (@pxref{Revision Control}).
3034
3035 The values of these environment variables and the argument to the
3036 @option{-V} option are like the @acronym{GNU} Emacs @code{version-control}
3037 variable (@pxref{Backup Names, , , emacs, The @acronym{GNU} Emacs Manual},
3038 for more information on backup versions in Emacs).  They also
3039 recognize synonyms that are more descriptive.  The valid values are
3040 listed below; unique abbreviations are acceptable.
3041
3042 @table @option
3043 @item t
3044 @itemx numbered
3045 Always make numbered backups.
3046
3047 @item nil
3048 @itemx existing
3049 Make numbered backups of files that already have them, simple backups of
3050 the others.  This is the default.
3051
3052 @item never
3053 @itemx simple
3054 Always make simple backups.
3055 @end table
3056
3057 You can also tell @command{patch} to prepend a prefix, such as a
3058 directory name, to produce backup file names.  The
3059 @option{--prefix=@var{prefix}} (@option{-B @var{prefix}}) option makes backup
3060 files by prepending @var{prefix} to them.  The
3061 @option{--basename-prefix=@var{prefix}} (@option{-Y @var{prefix}}) prepends
3062 @var{prefix} to the last file name component of backup file names
3063 instead; for example, @option{-Y ~} causes the backup name for
3064 @file{dir/file.c} to be @file{dir/~file.c}.  If you use either of
3065 these prefix options, the suffix-based options are ignored.
3066
3067 If you specify the output file with the @option{-o} option, that file is
3068 the one that is backed up, not the input file.
3069
3070 Options that affect the names of backup files do not affect whether
3071 backups are made.  For example, if you specify the
3072 @option{--no-backup-if-mismatch} option, none of the options described
3073 in this section have any affect, because no backups are made.
3074
3075 @node Reject Names
3076 @section Reject File Names
3077 @cindex reject file names
3078
3079 The names for reject files (files containing patches that
3080 @command{patch} could not find a place to apply) are normally the name
3081 of the output file with @samp{.rej} appended (or @samp{#} if using
3082 @samp{.rej} would make the backup file name too long).
3083
3084 Alternatively, you can tell @command{patch} to place all of the rejected
3085 patches in a single file.  The @option{-r @var{reject-file}} or
3086 @option{--reject-file=@var{reject-file}} option uses @var{reject-file} as
3087 the reject file name.
3088
3089 @node patch Messages
3090 @section Messages and Questions from @command{patch}
3091 @cindex @command{patch} messages and questions
3092 @cindex diagnostics from @command{patch}
3093 @cindex messages from @command{patch}
3094
3095 @command{patch} can produce a variety of messages, especially if it
3096 has trouble decoding its input.  In a few situations where it's not
3097 sure how to proceed, @command{patch} normally prompts you for more
3098 information from the keyboard.  There are options to produce more or
3099 fewer messages, to have it not ask for keyboard input, and to
3100 affect the way that file names are quoted in messages.
3101
3102 @menu
3103 * More or Fewer Messages::    Controlling the verbosity of @command{patch}.
3104 * patch and Keyboard Input::  Inhibiting keyboard input.
3105 * patch Quoting Style::       Quoting file names in diagnostics.
3106 @end menu
3107
3108 @command{patch} exits with status 0 if all hunks are applied successfully,
3109 1 if some hunks cannot be applied, and 2 if there is more serious trouble.
3110 When applying a set of patches in a loop, you should check the
3111 exit status, so you don't apply a later patch to a partially patched
3112 file.
3113
3114 @node More or Fewer Messages
3115 @subsection Controlling the Verbosity of @command{patch}
3116 @cindex verbose messages from @command{patch}
3117 @cindex inhibit messages from @command{patch}
3118
3119 You can cause @command{patch} to produce more messages by using the
3120 @option{--verbose} option.  For example, when you give this option,
3121 the message @samp{Hmm...} indicates that @command{patch} is reading text in
3122 the patch file, attempting to determine whether there is a patch in that
3123 text, and if so, what kind of patch it is.
3124
3125 You can inhibit all terminal output from @command{patch}, unless an error
3126 occurs, by using the @option{-s}, @option{--quiet}, or @option{--silent}
3127 option.
3128
3129 @node patch and Keyboard Input
3130 @subsection Inhibiting Keyboard Input
3131 @cindex keyboard input to @command{patch}
3132
3133 There are two ways you can prevent @command{patch} from asking you any
3134 questions.  The @option{--force} (@option{-f}) option assumes that you know
3135 what you are doing.  It causes @command{patch} to do the following:
3136
3137 @itemize @bullet
3138 @item
3139 Skip patches that do not contain file names in their headers.
3140
3141 @item
3142 Patch files even though they have the wrong version for the
3143 @samp{Prereq:} line in the patch;
3144
3145 @item
3146 Assume that patches are not reversed even if they look like they are.
3147 @end itemize
3148
3149 @noindent
3150 The @option{--batch} (@option{-t}) option is similar to @option{-f}, in that
3151 it suppresses questions, but it makes somewhat different assumptions:
3152
3153 @itemize @bullet
3154 @item
3155 Skip patches that do not contain file names in their headers
3156 (the same as @option{-f}).
3157
3158 @item
3159 Skip patches for which the file has the wrong version for the
3160 @samp{Prereq:} line in the patch;
3161
3162 @item
3163 Assume that patches are reversed if they look like they are.
3164 @end itemize
3165
3166 @node patch Quoting Style
3167 @subsection @command{patch} Quoting Style
3168 @cindex quoting style
3169
3170 When @command{patch} outputs a file name in a diagnostic message, it
3171 can format the name in any of several ways.  This can be useful to
3172 output file names unambiguously, even if they contain punctuation or
3173 special characters like newlines.  The
3174 @option{--quoting-style=@var{word}} option controls how names are
3175 output.  The @var{word} should be one of the following:
3176
3177 @table @samp
3178 @item literal
3179 Output names as-is.
3180 @item shell
3181 Quote names for the shell if they contain shell metacharacters or would
3182 cause ambiguous output.
3183 @item shell-always
3184 Quote names for the shell, even if they would normally not require quoting.
3185 @item c
3186 Quote names as for a C language string.
3187 @item escape
3188 Quote as with @samp{c} except omit the surrounding double-quote
3189 characters.
3190 @c The following are not yet implemented in patch 2.5.4.
3191 @c @item clocale
3192 @c Quote as with @samp{c} except use quotation marks appropriate for the
3193 @c locale.
3194 @c @item locale
3195 @c @c Use @t instead of @samp to avoid duplicate quoting in some output styles.
3196 @c Like @samp{clocale}, but quote @t{'like this'} instead of @t{"like
3197 @c this"} in the default C locale.  This looks nicer on many displays.
3198 @end table
3199
3200 @vindex QUOTING_STYLE
3201 You can specify the default value of the @option{--quoting-style}
3202 option with the environment variable @env{QUOTING_STYLE}.  If that
3203 environment variable is not set, the default value is @samp{shell},
3204 but this default may change in a future version of @command{patch}.
3205
3206 @node patch and POSIX
3207 @section @command{patch} and the @acronym{POSIX} Standard
3208 @cindex @acronym{POSIX}
3209
3210 @vindex POSIXLY_CORRECT
3211 If you specify the @option{--posix} option, or set the
3212 @env{POSIXLY_CORRECT} environment variable, @command{patch} conforms
3213 more strictly to the @acronym{POSIX} standard, as follows:
3214
3215 @itemize @bullet
3216 @item
3217 Take the first existing file from the list (old, new, index)
3218 when intuiting file names from diff headers.  @xref{Multiple Patches}.
3219
3220 @item
3221 Do not remove files that are removed by a diff.
3222 @xref{Creating and Removing}.
3223
3224 @item
3225 Do not ask whether to get files from @acronym{RCS}, ClearCase, or
3226 @acronym{SCCS}.  @xref{Revision Control}.
3227
3228 @item
3229 Require that all options precede the files in the command line.
3230
3231 @item
3232 Do not backup files, even when there is a mismatch.  @xref{Backups}.
3233
3234 @end itemize
3235
3236 @node patch and Tradition
3237 @section @acronym{GNU} @command{patch} and Traditional @command{patch}
3238 @cindex traditional @command{patch}
3239
3240 The current version of @acronym{GNU} @command{patch} normally follows the
3241 @acronym{POSIX} standard.  @xref{patch and POSIX}, for the few exceptions
3242 to this general rule.
3243
3244 Unfortunately, @acronym{POSIX} redefined the behavior of @command{patch} in
3245 several important ways.  You should be aware of the following
3246 differences if you must interoperate with traditional @command{patch},
3247 or with @acronym{GNU} @command{patch} version 2.1 and earlier.
3248
3249 @itemize @bullet
3250 @item
3251 In traditional @command{patch}, the @option{-p} option's operand was
3252 optional, and a bare @option{-p} was equivalent to @option{-p0}.  The
3253 @option{-p} option now requires an operand, and @option{-p@ 0} is now
3254 equivalent to @option{-p0}.  For maximum compatibility, use options
3255 like @option{-p0} and @option{-p1}.
3256
3257 Also, traditional @command{patch} simply counted slashes when
3258 stripping path prefixes; @command{patch} now counts pathname
3259 components.  That is, a sequence of one or more adjacent slashes now
3260 counts as a single slash.  For maximum portability, avoid sending
3261 patches containing @file{//} in file names.
3262
3263 @item
3264 In traditional @command{patch}, backups were enabled by default.  This
3265 behavior is now enabled with the @option{--backup} (@option{-b})
3266 option.
3267
3268 Conversely, in @acronym{POSIX} @command{patch}, backups are never made,
3269 even when there is a mismatch.  In @acronym{GNU} @command{patch}, this
3270 behavior is enabled with the @option{--no-backup-if-mismatch} option,
3271 or by conforming to @acronym{POSIX}.
3272
3273 The @option{-b@ @var{suffix}} option of traditional @command{patch} is
3274 equivalent to the @samp{-b -z@ @var{suffix}} options of @acronym{GNU}
3275 @command{patch}.
3276
3277 @item
3278 Traditional @command{patch} used a complicated (and incompletely
3279 documented) method to intuit the name of the file to be patched from
3280 the patch header.  This method did not conform to @acronym{POSIX}, and had
3281 a few gotchas.  Now @command{patch} uses a different, equally
3282 complicated (but better documented) method that is optionally
3283 @acronym{POSIX}-conforming; we hope it has fewer gotchas.  The two methods
3284 are compatible if the file names in the context diff header and the
3285 @samp{Index:} line are all identical after prefix-stripping.  Your
3286 patch is normally compatible if each header's file names all contain
3287 the same number of slashes.
3288
3289 @item
3290 When traditional @command{patch} asked the user a question, it sent
3291 the question to standard error and looked for an answer from the first
3292 file in the following list that was a terminal: standard error,
3293 standard output, @file{/dev/tty}, and standard input.  Now
3294 @command{patch} sends questions to standard output and gets answers
3295 from @file{/dev/tty}.  Defaults for some answers have been changed so
3296 that @command{patch} never goes into an infinite loop when using
3297 default answers.
3298
3299 @item
3300 Traditional @command{patch} exited with a status value that counted
3301 the number of bad hunks, or with status 1 if there was real trouble.
3302 Now @command{patch} exits with status 1 if some hunks failed, or with
3303 2 if there was real trouble.
3304
3305 @item
3306 Limit yourself to the following options when sending instructions
3307 meant to be executed by anyone running @acronym{GNU} @command{patch},
3308 traditional @command{patch}, or a @command{patch} that conforms to
3309 @acronym{POSIX}.  Spaces are significant in the following list, and
3310 operands are required.
3311
3312 @example
3313 @option{-c}
3314 @option{-d @var{dir}}
3315 @option{-D @var{define}}
3316 @option{-e}
3317 @option{-l}
3318 @option{-n}
3319 @option{-N}
3320 @option{-o @var{outfile}}
3321 @option{-p@var{num}}
3322 @option{-R}
3323 @option{-r @var{rejectfile}}
3324 @end example
3325
3326 @end itemize
3327
3328 @node Making Patches
3329 @chapter Tips for Making and Using Patches
3330
3331 Use some common sense when making and using patches.  For example,
3332 when sending bug fixes to a program's maintainer, send several small
3333 patches, one per independent subject, instead of one large,
3334 harder-to-digest patch that covers all the subjects.
3335
3336 Here are some other things you should keep in mind if you are going to
3337 distribute patches for updating a software package.
3338
3339 @menu
3340 * Tips for Patch Producers::    Advice for making patches.
3341 * Tips for Patch Consumers::    Advice for using patches.
3342 * Avoiding Common Mistakes::    Avoiding common mistakes when using @command{patch}.
3343 * Generating Smaller Patches::  How to generate smaller patches.
3344 @end menu
3345
3346 @node Tips for Patch Producers
3347 @section Tips for Patch Producers
3348 @cindex patch producer tips
3349
3350 To create a patch that changes an older version of a package into a
3351 newer version, first make a copy of the older and newer versions in
3352 adjacent subdirectories.  It is common to do that by unpacking
3353 @command{tar} archives of the two versions.
3354
3355 To generate the patch, use the command @samp{diff -Naur @var{old}
3356 @var{new}} where @var{old} and @var{new} identify the old and new
3357 directories.  The names @var{old} and @var{new} should not contain any
3358 slashes.  The @option{-N} option lets the patch create and remove
3359 files; @option{-a} lets the patch update non-text files; @option{-u}
3360 generates useful time stamps and enough context; and @option{-r} lets
3361 the patch update subdirectories.  Here is an example command, using
3362 Bourne shell syntax:
3363
3364 @example
3365 diff -Naur gcc-3.0.3 gcc-3.0.4
3366 @end example
3367
3368 Tell your recipients how to apply the patches.  This should include
3369 which working directory to use, and which @command{patch} options to
3370 use; the option @samp{-p1} is recommended.  Test your procedure by
3371 pretending to be a recipient and applying your patches to a copy of
3372 the original files.
3373
3374 @xref{Avoiding Common Mistakes}, for how to avoid common mistakes when
3375 generating a patch.
3376
3377 @node Tips for Patch Consumers
3378 @section Tips for Patch Consumers
3379 @cindex patch consumer tips
3380
3381 A patch producer should tell recipients how to apply the patches, so
3382 the first rule of thumb for a patch consumer is to follow the
3383 instructions supplied with the patch.
3384
3385 @acronym{GNU} @command{diff} can analyze files with arbitrarily long lines
3386 and files that end in incomplete lines.  However, older versions of
3387 @command{patch} cannot patch such files.  If you are having trouble
3388 applying such patches, try upgrading to a recent version of @acronym{GNU}
3389 @command{patch}.
3390
3391 @node Avoiding Common Mistakes
3392 @section Avoiding Common Mistakes
3393 @cindex common mistakes with patches
3394 @cindex patch, common mistakes
3395
3396 When producing a patch for multiple files, apply @command{diff} to
3397 directories whose names do not have slashes.  This reduces confusion
3398 when the patch consumer specifies the @option{-p@var{number}} option,
3399 since this option can have surprising results when the old and new
3400 file names have different numbers of slashes.  For example, do not
3401 send a patch with a header that looks like this:
3402
3403 @example
3404 diff -Naur v2.0.29/prog/README prog/README
3405 --- v2.0.29/prog/README 2002-03-10 23:30:39.942229878 -0800
3406 +++ prog/README 2002-03-17 20:49:32.442260588 -0800
3407 @end example
3408
3409 @noindent
3410 because the two file names have different numbers of slashes, and
3411 different versions of @command{patch} interpret the file names
3412 differently.  To avoid confusion, send output that looks like this
3413 instead:
3414
3415 @example
3416 diff -Naur v2.0.29/prog/README v2.0.30/prog/README
3417 --- v2.0.29/prog/README 2002-03-10 23:30:39.942229878 -0800
3418 +++ v2.0.30/prog/README 2002-03-17 20:49:32.442260588 -0800
3419 @end example
3420
3421 Make sure you have specified the file names correctly, either in a
3422 context diff header or with an @samp{Index:} line.  Take care to not send out
3423 reversed patches, since these make people wonder whether they have
3424 already applied the patch.
3425
3426 Avoid sending patches that compare backup file names like
3427 @file{README.orig} or @file{README~}, since this might confuse
3428 @command{patch} into patching a backup file instead of the real file.
3429 Instead, send patches that compare the same base file names in
3430 different directories, e.g.@: @file{old/README} and @file{new/README}.
3431
3432 To save people from partially applying a patch before other patches that
3433 should have gone before it, you can make the first patch in the patch
3434 file update a file with a name like @file{patchlevel.h} or
3435 @file{version.c}, which contains a patch level or version number.  If
3436 the input file contains the wrong version number, @command{patch} will
3437 complain immediately.
3438
3439 An even clearer way to prevent this problem is to put a @samp{Prereq:}
3440 line before the patch.  If the leading text in the patch file contains a
3441 line that starts with @samp{Prereq:}, @command{patch} takes the next word
3442 from that line (normally a version number) and checks whether the next
3443 input file contains that word, preceded and followed by either
3444 white space or a newline.  If not, @command{patch} prompts you for
3445 confirmation before proceeding.  This makes it difficult to accidentally
3446 apply patches in the wrong order.
3447
3448 @node Generating Smaller Patches
3449 @section Generating Smaller Patches
3450 @cindex patches, shrinking
3451
3452 The simplest way to generate a patch is to use @samp{diff -Naur}
3453 (@pxref{Tips for Patch Producers}), but you might be able to reduce
3454 the size of the patch by renaming or removing some files before making
3455 the patch.  If the older version of the package contains any files
3456 that the newer version does not, or if any files have been renamed
3457 between the two versions, make a list of @command{rm} and @command{mv}
3458 commands for the user to execute in the old version directory before
3459 applying the patch.  Then run those commands yourself in the scratch
3460 directory.
3461
3462 If there are any files that you don't need to include in the patch
3463 because they can easily be rebuilt from other files (for example,
3464 @file{TAGS} and output from @command{yacc} and @command{makeinfo}),
3465 exclude them from the patch by giving @command{diff} the @option{-x
3466 @var{pattern}} option (@pxref{Comparing Directories}).  If you want
3467 your patch to modify a derived file because your recipients lack tools
3468 to build it, make sure that the patch for the derived file follows any
3469 patches for files that it depends on, so that the recipients' time
3470 stamps will not confuse @command{make}.
3471
3472 Now you can create the patch using @samp{diff -Naur}.  Make sure to
3473 specify the scratch directory first and the newer directory second.
3474
3475 Add to the top of the patch a note telling the user any @command{rm} and
3476 @command{mv} commands to run before applying the patch.  Then you can
3477 remove the scratch directory.
3478
3479 You can also shrink the patch size by using fewer lines of context,
3480 but bear in mind that @command{patch} typically needs at least two
3481 lines for proper operation when patches do not exactly match the input
3482 files.
3483
3484 @node Invoking cmp
3485 @chapter Invoking @command{cmp}
3486 @cindex invoking @command{cmp}
3487 @cindex @command{cmp} invocation
3488
3489 The @command{cmp} command compares two files, and if they differ,
3490 tells the first byte and line number where they differ or reports
3491 that one file is a prefix of the other.  Bytes and
3492 lines are numbered starting with 1.  The arguments of @command{cmp}
3493 are as follows:
3494
3495 @example
3496 cmp @var{options}@dots{} @var{from-file} @r{[}@var{to-file} @r{[}@var{from-skip} @r{[}@var{to-skip}@r{]}@r{]}@r{]}
3497 @end example
3498
3499 The file name @file{-} is always the standard input.  @command{cmp}
3500 also uses the standard input if one file name is omitted.  The
3501 @var{from-skip} and @var{to-skip} operands specify how many bytes to
3502 ignore at the start of each file; they are equivalent to the
3503 @option{--ignore-initial=@var{from-skip}:@var{to-skip}} option.
3504
3505 By default, @command{cmp} outputs nothing if the two files have the
3506 same contents.  If one file is a prefix of the other, @command{cmp}
3507 prints to standard error a message of the following form:
3508
3509 @example
3510 cmp: EOF on @var{shorter-file}
3511 @end example
3512
3513 Otherwise, @command{cmp} prints to standard output a message of the
3514 following form:
3515
3516 @example
3517 @var{from-file} @var{to-file} differ: char @var{byte-number}, line @var{line-number}
3518 @end example
3519
3520 The message formats can differ outside the @acronym{POSIX} locale.
3521 Also, @acronym{POSIX} allows the @acronym{EOF} message to be followed
3522 by a blank and some additional information.
3523
3524 An exit status of 0 means no differences were found, 1 means some
3525 differences were found, and 2 means trouble.
3526
3527 @menu
3528 * cmp Options:: Summary of options to @command{cmp}.
3529 @end menu
3530
3531 @node cmp Options
3532 @section Options to @command{cmp}
3533 @cindex @command{cmp} options
3534 @cindex options for @command{cmp}
3535
3536 Below is a summary of all of the options that @acronym{GNU}
3537 @command{cmp} accepts.  Most options have two equivalent names, one of
3538 which is a single letter preceded by @samp{-}, and the other of which
3539 is a long name preceded by @samp{--}.  Multiple single letter options
3540 (unless they take an argument) can be combined into a single command
3541 line word: @option{-bl} is equivalent to @option{-b -l}.
3542
3543 @table @option
3544 @item -b
3545 @itemx --print-bytes
3546 Print the differing bytes.  Display control bytes as a
3547 @samp{^} followed by a letter of the alphabet and precede bytes
3548 that have the high bit set with @samp{M-} (which stands for ``meta'').
3549
3550 @item --help
3551 Output a summary of usage and then exit.
3552
3553 @item -i @var{skip}
3554 @itemx --ignore-initial=@var{skip}
3555 Ignore any differences in the first @var{skip} bytes of the input
3556 files.  Treat files with fewer than @var{skip} bytes as if they are
3557 empty.  If @var{skip} is of the form
3558 @option{@var{from-skip}:@var{to-skip}}, skip the first @var{from-skip}
3559 bytes of the first input file and the first @var{to-skip} bytes of the
3560 second.
3561
3562 @item -l
3563 @itemx --verbose
3564 Output the (decimal) byte numbers and (octal) values of all differing bytes,
3565 instead of the default standard output.
3566 Each output line contains a differing byte's number relative to the
3567 start of the input, followed by the differing byte values.
3568 Byte numbers start at 1.
3569 Also, output the @acronym{EOF} message if one file is shorter than the other.
3570
3571 @item -n @var{count}
3572 @itemx --bytes=@var{count}
3573 Compare at most @var{count} input bytes.
3574
3575 @item -s
3576 @itemx --quiet
3577 @itemx --silent
3578 Do not print anything; only return an exit status indicating whether
3579 the files differ.
3580
3581 @item -v
3582 @itemx --version
3583 Output version information and then exit.
3584 @end table
3585
3586 In the above table, operands that are byte counts are normally
3587 decimal, but may be preceded by @samp{0} for octal and @samp{0x} for
3588 hexadecimal.
3589
3590 A byte count can be followed by a suffix to specify a multiple of that
3591 count; in this case an omitted integer is understood to be 1.  A bare
3592 size letter, or one followed by @samp{iB}, specifies a multiple using
3593 powers of 1024.  A size letter followed by @samp{B} specifies powers
3594 of 1000 instead.  For example, @option{-n 4M} and @option{-n 4MiB} are
3595 equivalent to @option{-n 4194304}, whereas @option{-n 4MB} is
3596 equivalent to @option{-n 4000000}.  This notation is upward compatible
3597 with the @uref{http://www.bipm.fr/enus/3_SI/si-prefixes.html, SI
3598 prefixes} for decimal multiples and with the
3599 @uref{http://physics.nist.gov/cuu/Units/binary.html, IEC 60027-2
3600 prefixes for binary multiples}.
3601
3602 The following suffixes are defined.  Large sizes like @code{1Y} may be
3603 rejected by your computer due to limitations of its arithmetic.
3604
3605 @table @samp
3606 @item kB
3607 @cindex kilobyte, definition of
3608 kilobyte: @math{10^3 = 1000}.
3609 @item k
3610 @itemx K
3611 @itemx KiB
3612 @cindex kibibyte, definition of
3613 kibibyte: @math{2^10 = 1024}.  @samp{K} is special: the SI prefix is
3614 @samp{k} and the IEC 60027-2 prefix is @samp{Ki}, but tradition and
3615 @acronym{POSIX} use @samp{k} to mean @samp{KiB}.
3616 @item MB
3617 @cindex megabyte, definition of
3618 megabyte: @math{10^6 = 1,000,000}.
3619 @item M
3620 @itemx MiB
3621 @cindex mebibyte, definition of
3622 mebibyte: @math{2^20 = 1,048,576}.
3623 @item GB
3624 @cindex gigabyte, definition of
3625 gigabyte: @math{10^9 = 1,000,000,000}.
3626 @item G
3627 @itemx GiB
3628 @cindex gibibyte, definition of
3629 gibibyte: @math{2^30 = 1,073,741,824}.
3630 @item TB
3631 @cindex terabyte, definition of
3632 terabyte:  @math{10^12 = 1,000,000,000,000}.
3633 @item T
3634 @itemx TiB
3635 @cindex tebibyte, definition of
3636 tebibyte: @math{2^40 = 1,099,511,627,776}.
3637 @item PB
3638 @cindex petabyte, definition of
3639 petabyte: @math{10^15 = 1,000,000,000,000,000}.
3640 @item P
3641 @itemx PiB
3642 @cindex pebibyte, definition of
3643 pebibyte: @math{2^50 = 1,125,899,906,842,624}.
3644 @item EB
3645 @cindex exabyte, definition of
3646 exabyte: @math{10^18 = 1,000,000,000,000,000,000}.
3647 @item E
3648 @itemx EiB
3649 @cindex exbibyte, definition of
3650 exbibyte: @math{2^60 = 1,152,921,504,606,846,976}.
3651 @item ZB
3652 @cindex zettabyte, definition of
3653 zettabyte: @math{10^21 = 1,000,000,000,000,000,000,000}
3654 @item Z
3655 @itemx ZiB
3656 @math{2^70 = 1,180,591,620,717,411,303,424}.
3657 (@samp{Zi} is a GNU extension to IEC 60027-2.)
3658 @item YB
3659 @cindex yottabyte, definition of
3660 yottabyte: @math{10^24 = 1,000,000,000,000,000,000,000,000}.
3661 @item Y
3662 @itemx YiB
3663 @math{2^80 = 1,208,925,819,614,629,174,706,176}.
3664 (@samp{Yi} is a GNU extension to IEC 60027-2.)
3665 @end table
3666
3667 @node Invoking diff
3668 @chapter Invoking @command{diff}
3669 @cindex invoking @command{diff}
3670 @cindex @command{diff} invocation
3671
3672 The format for running the @command{diff} command is:
3673
3674 @example
3675 diff @var{options}@dots{} @var{files}@dots{}
3676 @end example
3677
3678 In the simplest case, two file names @var{from-file} and
3679 @var{to-file} are given, and @command{diff} compares the contents of
3680 @var{from-file} and @var{to-file}.  A file name of @file{-} stands for
3681 text read from the standard input.  As a special case, @samp{diff - -}
3682 compares a copy of standard input to itself.
3683
3684 If one file is a directory and the other is not, @command{diff} compares
3685 the file in the directory whose name is that of the non-directory.
3686 The non-directory file must not be @file{-}.
3687
3688 If two file names are given and both are directories,
3689 @command{diff} compares corresponding files in both directories, in
3690 alphabetical order; this comparison is not recursive unless the
3691 @option{--recursive} (@option{-r}) option is given.  @command{diff} never
3692 compares the actual contents of a directory as if it were a file.  The
3693 file that is fully specified may not be standard input, because standard
3694 input is nameless and the notion of ``file with the same name'' does not
3695 apply.
3696
3697 If the @option{--from-file=@var{file}} option is given, the number of
3698 file names is arbitrary, and @var{file} is compared to each named file.
3699 Similarly, if the @option{--to-file=@var{file}} option is given, each
3700 named file is compared to @var{file}.
3701
3702 @command{diff} options begin with @samp{-}, so normally file names
3703 may not begin with @samp{-}.  However, @option{--} as an
3704 argument by itself treats the remaining arguments as file names even if
3705 they begin with @samp{-}.
3706
3707 An exit status of 0 means no differences were found, 1 means some
3708 differences were found, and 2 means trouble.  Normally, differing
3709 binary files count as trouble, but this can be altered by using the
3710 @option{--text} (@option{-a}) option, or the @option{-q} or
3711 @option{--brief} option.
3712
3713 @menu
3714 * diff Options:: Summary of options to @command{diff}.
3715 @end menu
3716
3717 @node diff Options
3718 @section Options to @command{diff}
3719 @cindex @command{diff} options
3720 @cindex options for @command{diff}
3721
3722 Below is a summary of all of the options that @acronym{GNU}
3723 @command{diff} accepts.  Most options have two equivalent names, one
3724 of which is a single letter preceded by @samp{-}, and the other of
3725 which is a long name preceded by @samp{--}.  Multiple single letter
3726 options (unless they take an argument) can be combined into a single
3727 command line word: @option{-ac} is equivalent to @option{-a -c}.  Long
3728 named options can be abbreviated to any unique prefix of their name.
3729 Brackets ([ and ]) indicate that an option takes an optional argument.
3730
3731 @table @option
3732 @item -a
3733 @itemx --text
3734 Treat all files as text and compare them line-by-line, even if they
3735 do not seem to be text.  @xref{Binary}.
3736
3737 @item -b
3738 @itemx --ignore-space-change
3739 Ignore changes in amount of white space.  @xref{White Space}.
3740
3741 @item -B
3742 @itemx --ignore-blank-lines
3743 Ignore changes that just insert or delete blank lines.  @xref{Blank
3744 Lines}.
3745
3746 @item --binary
3747 Read and write data in binary mode.  @xref{Binary}.
3748
3749 @item -c
3750 Use the context output format, showing three lines of context.
3751 @xref{Context Format}.
3752
3753 @item -C @var{lines}
3754 @itemx --context@r{[}=@var{lines}@r{]}
3755 Use the context output format, showing @var{lines} (an integer) lines of
3756 context, or three if @var{lines} is not given.  @xref{Context Format}.
3757 For proper operation, @command{patch} typically needs at least two lines of
3758 context.
3759
3760 For compatibility @command{diff} also supports an obsolete option
3761 syntax @option{-@var{lines}} that has effect when combined with
3762 @option{-c}, @option{-p}, or @option{-u}.  New scripts should use
3763 @option{-U @var{lines}} (@option{-C @var{lines}}) instead.
3764
3765 @item --changed-group-format=@var{format}
3766 Use @var{format} to output a line group containing differing lines from
3767 both files in if-then-else format.  @xref{Line Group Formats}.
3768
3769 @item -d
3770 @itemx --minimal
3771 Change the algorithm perhaps find a smaller set of changes.  This makes
3772 @command{diff} slower (sometimes much slower).  @xref{diff Performance}.
3773
3774 @item -D @var{name}
3775 @itemx --ifdef=@var{name}
3776 Make merged @samp{#ifdef} format output, conditional on the preprocessor
3777 macro @var{name}.  @xref{If-then-else}.
3778
3779 @item -e
3780 @itemx --ed
3781 Make output that is a valid @command{ed} script.  @xref{ed Scripts}.
3782
3783 @item -E
3784 @itemx --ignore-tab-expansion
3785 Ignore changes due to tab expansion.
3786 @xref{White Space}.
3787
3788 @item -f
3789 @itemx --forward-ed
3790 Make output that looks vaguely like an @command{ed} script but has changes
3791 in the order they appear in the file.  @xref{Forward ed}.
3792
3793 @item -F @var{regexp}
3794 @itemx --show-function-line=@var{regexp}
3795 In context and unified format, for each hunk of differences, show some
3796 of the last preceding line that matches @var{regexp}.  @xref{Specified
3797 Headings}.
3798
3799 @item --from-file=@var{file}
3800 Compare @var{file} to each operand; @var{file} may be a directory.
3801
3802 @item --help
3803 Output a summary of usage and then exit.
3804
3805 @item --horizon-lines=@var{lines}
3806 Do not discard the last @var{lines} lines of the common prefix
3807 and the first @var{lines} lines of the common suffix.
3808 @xref{diff Performance}.
3809
3810 @item -i
3811 @itemx --ignore-case
3812 Ignore changes in case; consider upper- and lower-case letters
3813 equivalent.  @xref{Case Folding}.
3814
3815 @item -I @var{regexp}
3816 @itemx --ignore-matching-lines=@var{regexp}
3817 Ignore changes that just insert or delete lines that match @var{regexp}.
3818 @xref{Specified Lines}.
3819
3820 @item --ignore-file-name-case
3821 Ignore case when comparing file names.  For example, recursive
3822 comparison of @file{d} to @file{e} might compare the contents of
3823 @file{d/Init} and @file{e/inIt}.  At the top level, @samp{diff d inIt}
3824 might compare the contents of @file{d/Init} and @file{inIt}.
3825 @xref{Comparing Directories}.
3826
3827 @item -l
3828 @itemx --paginate
3829 Pass the output through @command{pr} to paginate it.  @xref{Pagination}.
3830
3831 @item -L @var{label}
3832 @itemx --label=@var{label}
3833 Use @var{label} instead of the file name in the context format
3834 (@pxref{Context Format}) and unified format (@pxref{Unified Format})
3835 headers.  @xref{RCS}.
3836
3837 @item --left-column
3838 Print only the left column of two common lines in side by side format.
3839 @xref{Side by Side Format}.
3840
3841 @item --line-format=@var{format}
3842 Use @var{format} to output all input lines in if-then-else format.
3843 @xref{Line Formats}.
3844
3845 @item -n
3846 @itemx --rcs
3847 Output @acronym{RCS}-format diffs; like @option{-f} except that each command
3848 specifies the number of lines affected.  @xref{RCS}.
3849
3850 @item -N
3851 @itemx --new-file
3852 If one file is missing, treat it as present but empty.
3853 @xref{Comparing Directories}.
3854
3855 @item --new-group-format=@var{format}
3856 Use @var{format} to output a group of lines taken from just the second
3857 file in if-then-else format.  @xref{Line Group Formats}.
3858
3859 @item --new-line-format=@var{format}
3860 Use @var{format} to output a line taken from just the second file in
3861 if-then-else format.  @xref{Line Formats}.
3862
3863 @item --no-dereference
3864 Act on symbolic links themselves instead of what they point to.
3865
3866 @item --old-group-format=@var{format}
3867 Use @var{format} to output a group of lines taken from just the first
3868 file in if-then-else format.  @xref{Line Group Formats}.
3869
3870 @item --old-line-format=@var{format}
3871 Use @var{format} to output a line taken from just the first file in
3872 if-then-else format.  @xref{Line Formats}.
3873
3874 @item -p
3875 @itemx --show-c-function
3876 Show which C function each change is in.  @xref{C Function Headings}.
3877
3878 @item -q
3879 @itemx --brief
3880 Report only whether the files differ, not the details of the
3881 differences.  @xref{Brief}.
3882
3883 @item -r
3884 @itemx --recursive
3885 When comparing directories, recursively compare any subdirectories
3886 found.  @xref{Comparing Directories}.
3887
3888 @item -s
3889 @itemx --report-identical-files
3890 Report when two files are the same.  @xref{Comparing Directories}.
3891
3892 @item -S @var{file}
3893 @itemx --starting-file=@var{file}
3894 When comparing directories, start with the file @var{file}.  This is
3895 used for resuming an aborted comparison.  @xref{Comparing Directories}.
3896
3897 @item --speed-large-files
3898 Use heuristics to speed handling of large files that have numerous
3899 scattered small changes.  @xref{diff Performance}.
3900
3901 @item --strip-trailing-cr
3902 Strip any trailing carriage return at the end of an input line.
3903 @xref{Binary}.
3904
3905 @item --suppress-common-lines
3906 Do not print common lines in side by side format.
3907 @xref{Side by Side Format}.
3908
3909 @item -t
3910 @itemx --expand-tabs
3911 Expand tabs to spaces in the output, to preserve the alignment of tabs
3912 in the input files.  @xref{Tabs}.
3913
3914 @item -T
3915 @itemx --initial-tab
3916 Output a tab rather than a space before the text of a line in normal or
3917 context format.  This causes the alignment of tabs in the line to look
3918 normal.  @xref{Tabs}.
3919
3920 @item --tabsize=@var{columns}
3921 Assume that tab stops are set every @var{columns} (default 8) print
3922 columns.  @xref{Tabs}.
3923
3924 @item --suppress-blank-empty
3925 Suppress any blanks before newlines when printing the representation
3926 of an empty line, when outputting normal, context, or unified format.
3927 @xref{Trailing Blanks}.
3928
3929 @item --to-file=@var{file}
3930 Compare each operand to @var{file}; @var{file} may be a directory.
3931
3932 @item -u
3933 Use the unified output format, showing three lines of context.
3934 @xref{Unified Format}.
3935
3936 @item --unchanged-group-format=@var{format}
3937 Use @var{format} to output a group of common lines taken from both files
3938 in if-then-else format.  @xref{Line Group Formats}.
3939
3940 @item --unchanged-line-format=@var{format}
3941 Use @var{format} to output a line common to both files in if-then-else
3942 format.  @xref{Line Formats}.
3943
3944 @item --unidirectional-new-file
3945 If a first file is missing, treat it as present but empty.
3946 @xref{Comparing Directories}.
3947
3948 @item -U @var{lines}
3949 @itemx --unified@r{[}=@var{lines}@r{]}
3950 Use the unified output format, showing @var{lines} (an integer) lines of
3951 context, or three if @var{lines} is not given.  @xref{Unified Format}.
3952 For proper operation, @command{patch} typically needs at least two lines of
3953 context.
3954
3955 On older systems, @command{diff} supports an obsolete option
3956 @option{-@var{lines}} that has effect when combined with @option{-u}.
3957 @acronym{POSIX} 1003.1-2001 (@pxref{Standards conformance}) does not allow
3958 this; use @option{-U @var{lines}} instead.
3959
3960 @item -v
3961 @itemx --version
3962 Output version information and then exit.
3963
3964 @item -w
3965 @itemx --ignore-all-space
3966 Ignore white space when comparing lines.  @xref{White Space}.
3967
3968 @item -W @var{columns}
3969 @itemx --width=@var{columns}
3970 Output at most @var{columns} (default 130) print columns per line in
3971 side by side format.  @xref{Side by Side Format}.
3972
3973 @item -x @var{pattern}
3974 @itemx --exclude=@var{pattern}
3975 When comparing directories, ignore files and subdirectories whose basenames
3976 match @var{pattern}.  @xref{Comparing Directories}.
3977
3978 @item -X @var{file}
3979 @itemx --exclude-from=@var{file}
3980 When comparing directories, ignore files and subdirectories whose basenames
3981 match any pattern contained in @var{file}.  @xref{Comparing Directories}.
3982
3983 @item -y
3984 @itemx --side-by-side
3985 Use the side by side output format.  @xref{Side by Side Format}.
3986
3987 @item -Z
3988 @itemx --ignore-trailing-space
3989 Ignore white space at line end.  @xref{White Space}.
3990 @end table
3991
3992 @node Invoking diff3
3993 @chapter Invoking @command{diff3}
3994 @cindex invoking @command{diff3}
3995 @cindex @command{diff3} invocation
3996
3997 The @command{diff3} command compares three files and outputs descriptions
3998 of their differences.  Its arguments are as follows:
3999
4000 @example
4001 diff3 @var{options}@dots{} @var{mine} @var{older} @var{yours}
4002 @end example
4003
4004 The files to compare are @var{mine}, @var{older}, and @var{yours}.
4005 At most one of these three file names may be @file{-},
4006 which tells @command{diff3} to read the standard input for that file.
4007
4008 An exit status of 0 means @command{diff3} was successful, 1 means some
4009 conflicts were found, and 2 means trouble.
4010
4011 @menu
4012 * diff3 Options:: Summary of options to @command{diff3}.
4013 @end menu
4014
4015 @node diff3 Options
4016 @section Options to @command{diff3}
4017 @cindex @command{diff3} options
4018 @cindex options for @command{diff3}
4019
4020 Below is a summary of all of the options that @acronym{GNU} @command{diff3}
4021 accepts.  Multiple single letter options (unless they take an argument)
4022 can be combined into a single command line argument.
4023
4024 @table @option
4025 @item -a
4026 @itemx --text
4027 Treat all files as text and compare them line-by-line, even if they
4028 do not appear to be text.  @xref{Binary}.
4029
4030 @item -A
4031 @itemx --show-all
4032 Incorporate all unmerged changes from @var{older} to @var{yours} into
4033 @var{mine}, surrounding conflicts with bracket lines.
4034 @xref{Marking Conflicts}.
4035
4036 @item --diff-program=@var{program}
4037 Use the compatible comparison program @var{program} to compare files
4038 instead of @command{diff}.
4039
4040 @item -e
4041 @itemx --ed
4042 Generate an @command{ed} script that incorporates all the changes from
4043 @var{older} to @var{yours} into @var{mine}.  @xref{Which Changes}.
4044
4045 @item -E
4046 @itemx --show-overlap
4047 Like @option{-e}, except bracket lines from overlapping changes' first
4048 and third files.
4049 @xref{Marking Conflicts}.
4050 With @option{-E}, an overlapping change looks like this:
4051
4052 @example
4053 <<<<<<< @var{mine}
4054 @r{lines from @var{mine}}
4055 =======
4056 @r{lines from @var{yours}}
4057 >>>>>>> @var{yours}
4058 @end example
4059
4060 @item --help
4061 Output a summary of usage and then exit.
4062
4063 @item -i
4064 Generate @samp{w} and @samp{q} commands at the end of the @command{ed}
4065 script for System V compatibility.  This option must be combined with
4066 one of the @option{-AeExX3} options, and may not be combined with @option{-m}.
4067 @xref{Saving the Changed File}.
4068
4069 @item --label=@var{label}
4070 Use the label @var{label} for the brackets output by the @option{-A},
4071 @option{-E} and @option{-X} options.  This option may be given up to three
4072 times, one for each input file.  The default labels are the names of
4073 the input files.  Thus @samp{diff3 --label X --label Y --label Z -m A
4074 B C} acts like
4075 @samp{diff3 -m A B C}, except that the output looks like it came from
4076 files named @samp{X}, @samp{Y} and @samp{Z} rather than from files
4077 named @samp{A}, @samp{B} and @samp{C}.  @xref{Marking Conflicts}.
4078
4079 @item -m
4080 @itemx --merge
4081 Apply the edit script to the first file and send the result to standard
4082 output.  Unlike piping the output from @command{diff3} to @command{ed}, this
4083 works even for binary files and incomplete lines.  @option{-A} is assumed
4084 if no edit script option is specified.  @xref{Bypassing ed}.
4085
4086 @item --strip-trailing-cr
4087 Strip any trailing carriage return at the end of an input line.
4088 @xref{Binary}.
4089
4090 @item -T
4091 @itemx --initial-tab
4092 Output a tab rather than two spaces before the text of a line in normal format.
4093 This causes the alignment of tabs in the line to look normal.  @xref{Tabs}.
4094
4095 @item -v
4096 @itemx --version
4097 Output version information and then exit.
4098
4099 @item -x
4100 @itemx --overlap-only
4101 Like @option{-e}, except output only the overlapping changes.
4102 @xref{Which Changes}.
4103
4104 @item -X
4105 Like @option{-E}, except output only the overlapping changes.
4106 In other words, like @option{-x}, except bracket changes as in @option{-E}.
4107 @xref{Marking Conflicts}.
4108
4109 @item -3
4110 @itemx --easy-only
4111 Like @option{-e}, except output only the nonoverlapping changes.
4112 @xref{Which Changes}.
4113 @end table
4114
4115 @node Invoking patch
4116 @chapter Invoking @command{patch}
4117 @cindex invoking @command{patch}
4118 @cindex @command{patch} invocation
4119
4120 Normally @command{patch} is invoked like this:
4121
4122 @example
4123 patch <@var{patchfile}
4124 @end example
4125
4126 The full format for invoking @command{patch} is:
4127
4128 @example
4129 patch @var{options}@dots{} @r{[}@var{origfile} @r{[}@var{patchfile}@r{]}@r{]}
4130 @end example
4131
4132 You can also specify where to read the patch from with the @option{-i
4133 @var{patchfile}} or @option{--input=@var{patchfile}} option.
4134 If you do not specify @var{patchfile}, or if @var{patchfile} is
4135 @file{-}, @command{patch} reads the patch (that is, the @command{diff} output)
4136 from the standard input.
4137
4138 If you do not specify an input file on the command line, @command{patch}
4139 tries to intuit from the @dfn{leading text} (any text in the patch
4140 that comes before the @command{diff} output) which file to edit.
4141 @xref{Multiple Patches}.
4142
4143 By default, @command{patch} replaces the original input file with the
4144 patched version, possibly after renaming the original file into a
4145 backup file (@pxref{Backup Names}, for a description of how
4146 @command{patch} names backup files).  You can also specify where to
4147 put the output with the @option{-o @var{file}} or
4148 @option{--output=@var{file}} option; however, do not use this option
4149 if @var{file} is one of the input files.
4150
4151 @menu
4152 * patch Options::     Summary table of options to @command{patch}.
4153 @end menu
4154
4155 @node patch Options
4156 @section Options to @command{patch}
4157 @cindex @command{patch} options
4158 @cindex options for @command{patch}
4159
4160 Here is a summary of all of the options that @acronym{GNU} @command{patch}
4161 accepts.  @xref{patch and Tradition}, for which of these options are
4162 safe to use in older versions of @command{patch}.
4163
4164 Multiple single-letter options that do not take an argument can be
4165 combined into a single command line argument with only one dash.
4166
4167 @table @option
4168 @item -b
4169 @itemx --backup
4170 Back up the original contents of each file, even if backups would
4171 normally not be made.  @xref{Backups}.
4172
4173 @item -B @var{prefix}
4174 @itemx --prefix=@var{prefix}
4175 Prepend @var{prefix} to backup file names.  @xref{Backup Names}.
4176
4177 @item --backup-if-mismatch
4178 Back up the original contents of each file if the patch does not
4179 exactly match the file.  This is the default behavior when not
4180 conforming to @acronym{POSIX}.  @xref{Backups}.
4181
4182 @item --binary
4183 Read and write all files in binary mode, except for standard output
4184 and @file{/dev/tty}.  This option has no effect on
4185 @acronym{POSIX}-conforming systems like @acronym{GNU}/Linux.  On systems where
4186 this option makes a difference, the patch should be generated by
4187 @samp{diff -a --binary}.  @xref{Binary}.
4188
4189 @item -c
4190 @itemx --context
4191 Interpret the patch file as a context diff.  @xref{patch Input}.
4192
4193 @item -d @var{directory}
4194 @itemx --directory=@var{directory}
4195 Make directory @var{directory} the current directory for interpreting
4196 both file names in the patch file, and file names given as arguments to
4197 other options.  @xref{patch Directories}.
4198
4199 @item -D @var{name}
4200 @itemx --ifdef=@var{name}
4201 Make merged if-then-else output using @var{name}.  @xref{If-then-else}.
4202
4203 @item --dry-run
4204 Print the results of applying the patches without actually changing
4205 any files.  @xref{Dry Runs}.
4206
4207 @item -e
4208 @itemx --ed
4209 Interpret the patch file as an @command{ed} script.  @xref{patch Input}.
4210
4211 @item -E
4212 @itemx --remove-empty-files
4213 Remove output files that are empty after the patches have been applied.
4214 @xref{Creating and Removing}.
4215
4216 @item -f
4217 @itemx --force
4218 Assume that the user knows exactly what he or she is doing, and do not
4219 ask any questions.  @xref{patch Messages}.
4220
4221 @item -F @var{lines}
4222 @itemx --fuzz=@var{lines}
4223 Set the maximum fuzz factor to @var{lines}.  @xref{Inexact}.
4224
4225 @item -g @var{num}
4226 @itemx --get=@var{num}
4227 If @var{num} is positive, get input files from a revision control
4228 system as necessary; if zero, do not get the files; if negative, ask
4229 the user whether to get the files.  @xref{Revision Control}.
4230
4231 @item --help
4232 Output a summary of usage and then exit.
4233
4234 @item -i @var{patchfile}
4235 @itemx --input=@var{patchfile}
4236 Read the patch from @var{patchfile} rather than from standard input.
4237 @xref{patch Options}.
4238
4239 @item -l
4240 @itemx --ignore-white-space
4241 Let any sequence of blanks (spaces or tabs) in the patch file match
4242 any sequence of blanks in the input file.  @xref{Changed White Space}.
4243
4244 @item -n
4245 @itemx --normal
4246 Interpret the patch file as a normal diff.  @xref{patch Input}.
4247
4248 @item -N
4249 @itemx --forward
4250 Ignore patches that @command{patch} thinks are reversed or already applied.
4251 See also @option{-R}.  @xref{Reversed Patches}.
4252
4253 @item --no-backup-if-mismatch
4254 Do not back up the original contents of files.  This is the default
4255 behavior when conforming to @acronym{POSIX}.  @xref{Backups}.
4256
4257 @item -o @var{file}
4258 @itemx --output=@var{file}
4259 Use @var{file} as the output file name.  @xref{patch Options}.
4260
4261 @item -p@var{number}
4262 @itemx --strip=@var{number}
4263 Set the file name strip count to @var{number}.  @xref{patch Directories}.
4264
4265 @item --posix
4266 Conform to @acronym{POSIX}, as if the @env{POSIXLY_CORRECT} environment
4267 variable had been set.  @xref{patch and POSIX}.
4268
4269 @item --quoting-style=@var{word}
4270 Use style @var{word} to quote names in diagnostics, as if the
4271 @env{QUOTING_STYLE} environment variable had been set to @var{word}.
4272 @xref{patch Quoting Style}.
4273
4274 @item -r @var{reject-file}
4275 @itemx --reject-file=@var{reject-file}
4276 Use @var{reject-file} as the reject file name.  @xref{Reject Names}.
4277
4278 @item -R
4279 @itemx --reverse
4280 Assume that this patch was created with the old and new files swapped.
4281 @xref{Reversed Patches}.
4282
4283 @item -s
4284 @itemx --quiet
4285 @itemx --silent
4286 Work silently unless an error occurs.  @xref{patch Messages}.
4287
4288 @item -t
4289 @itemx --batch
4290 Do not ask any questions.  @xref{patch Messages}.
4291
4292 @item -T
4293 @itemx --set-time
4294 Set the modification and access times of patched files from time
4295 stamps given in context diff headers, assuming that the context diff
4296 headers use local time.  @xref{Patching Time Stamps}.
4297
4298 @item -u
4299 @itemx --unified
4300 Interpret the patch file as a unified diff.  @xref{patch Input}.
4301
4302 @item -v
4303 @itemx --version
4304 Output version information and then exit.
4305
4306 @item -V @var{backup-style}
4307 @itemx --version=control=@var{backup-style}
4308 Select the naming convention for backup file names.  @xref{Backup Names}.
4309
4310 @item --verbose
4311 Print more diagnostics than usual.  @xref{patch Messages}.
4312
4313 @item -x @var{number}
4314 @itemx --debug=@var{number}
4315 Set internal debugging flags.  Of interest only to @command{patch}
4316 patchers.
4317
4318 @item -Y @var{prefix}
4319 @itemx --basename-prefix=@var{prefix}
4320 Prepend @var{prefix} to base names of backup files.  @xref{Backup Names}.
4321
4322 @item -z @var{suffix}
4323 @itemx --suffix=@var{suffix}
4324 Use @var{suffix} as the backup extension instead of @samp{.orig} or
4325 @samp{~}.  @xref{Backup Names}.
4326
4327 @item -Z
4328 @itemx --set-utc
4329 Set the modification and access times of patched files from time
4330 stamps given in context diff headers, assuming that the context diff
4331 headers use @acronym{UTC}.  @xref{Patching Time Stamps}.
4332
4333 @end table
4334
4335 @node Invoking sdiff
4336 @chapter Invoking @command{sdiff}
4337 @cindex invoking @command{sdiff}
4338 @cindex @command{sdiff} invocation
4339
4340 The @command{sdiff} command merges two files and interactively outputs the
4341 results.  Its arguments are as follows:
4342
4343 @example
4344 sdiff -o @var{outfile} @var{options}@dots{} @var{from-file} @var{to-file}
4345 @end example
4346
4347 This merges @var{from-file} with @var{to-file}, with output to @var{outfile}.
4348 If @var{from-file} is a directory and @var{to-file} is not, @command{sdiff}
4349 compares the file in @var{from-file} whose file name is that of @var{to-file},
4350 and vice versa.  @var{from-file} and @var{to-file} may not both be
4351 directories.
4352
4353 @command{sdiff} options begin with @samp{-}, so normally @var{from-file}
4354 and @var{to-file} may not begin with @samp{-}.  However, @option{--} as an
4355 argument by itself treats the remaining arguments as file names even if
4356 they begin with @samp{-}.  You may not use @file{-} as an input file.
4357
4358 @command{sdiff} without @option{--output} (@option{-o}) produces a
4359 side-by-side difference.  This usage is obsolete; use the
4360 @option{--side-by-side} (@option{-y}) option of @command{diff} instead.
4361
4362 An exit status of 0 means no differences were found, 1 means some
4363 differences were found, and 2 means trouble.
4364
4365 @menu
4366 * sdiff Options:: Summary of options to @command{diff}.
4367 @end menu
4368
4369 @node sdiff Options
4370 @section Options to @command{sdiff}
4371 @cindex @command{sdiff} options
4372 @cindex options for @command{sdiff}
4373
4374 Below is a summary of all of the options that @acronym{GNU}
4375 @command{sdiff} accepts.  Each option has two equivalent names, one of
4376 which is a single letter preceded by @samp{-}, and the other of which
4377 is a long name preceded by @samp{--}.  Multiple single letter options
4378 (unless they take an argument) can be combined into a single command
4379 line argument.  Long named options can be abbreviated to any unique
4380 prefix of their name.
4381
4382 @table @option
4383 @item -a
4384 @itemx --text
4385 Treat all files as text and compare them line-by-line, even if they
4386 do not appear to be text.  @xref{Binary}.
4387
4388 @item -b
4389 @itemx --ignore-space-change
4390 Ignore changes in amount of white space.  @xref{White Space}.
4391
4392 @item -B
4393 @itemx --ignore-blank-lines
4394 Ignore changes that just insert or delete blank lines.  @xref{Blank
4395 Lines}.
4396
4397 @item -d
4398 @itemx --minimal
4399 Change the algorithm to perhaps find a smaller set of changes.  This
4400 makes @command{sdiff} slower (sometimes much slower).  @xref{diff
4401 Performance}.
4402
4403 @item --diff-program=@var{program}
4404 Use the compatible comparison program @var{program} to compare files
4405 instead of @command{diff}.
4406
4407 @item -E
4408 @itemx --ignore-tab-expansion
4409 Ignore changes due to tab expansion.
4410 @xref{White Space}.
4411
4412 @item --help
4413 Output a summary of usage and then exit.
4414
4415 @item -i
4416 @itemx --ignore-case
4417 Ignore changes in case; consider upper- and lower-case to be the same.
4418 @xref{Case Folding}.
4419
4420 @item -I @var{regexp}
4421 @itemx --ignore-matching-lines=@var{regexp}
4422 Ignore changes that just insert or delete lines that match @var{regexp}.
4423 @xref{Specified Lines}.
4424
4425 @item -l
4426 @itemx --left-column
4427 Print only the left column of two common lines.
4428 @xref{Side by Side Format}.
4429
4430 @item -o @var{file}
4431 @itemx --output=@var{file}
4432 Put merged output into @var{file}.  This option is required for merging.
4433
4434 @item -s
4435 @itemx --suppress-common-lines
4436 Do not print common lines.  @xref{Side by Side Format}.
4437
4438 @item --speed-large-files
4439 Use heuristics to speed handling of large files that have numerous
4440 scattered small changes.  @xref{diff Performance}.
4441
4442 @item --strip-trailing-cr
4443 Strip any trailing carriage return at the end of an input line.
4444 @xref{Binary}.
4445
4446 @item -t
4447 @itemx --expand-tabs
4448 Expand tabs to spaces in the output, to preserve the alignment of tabs
4449 in the input files.  @xref{Tabs}.
4450
4451 @item --tabsize=@var{columns}
4452 Assume that tab stops are set every @var{columns} (default 8) print
4453 columns.  @xref{Tabs}.
4454
4455 @item -v
4456 @itemx --version
4457 Output version information and then exit.
4458
4459 @item -w @var{columns}
4460 @itemx --width=@var{columns}
4461 Output at most @var{columns} (default 130) print columns per line.
4462 @xref{Side by Side Format}.  Note that for historical reasons, this
4463 option is @option{-W} in @command{diff}, @option{-w} in @command{sdiff}.
4464
4465 @item -W
4466 @itemx --ignore-all-space
4467 Ignore white space when comparing lines.  @xref{White Space}.
4468 Note that for historical reasons, this option is @option{-w} in @command{diff},
4469 @option{-W} in @command{sdiff}.
4470
4471 @item -Z
4472 @itemx --ignore-trailing-space
4473 Ignore white space at line end.  @xref{White Space}.
4474 @end table
4475
4476 @node Standards conformance
4477 @chapter Standards conformance
4478 @cindex @acronym{POSIX}
4479
4480 @vindex POSIXLY_CORRECT
4481 In a few cases, the @acronym{GNU} utilities' default behavior is
4482 incompatible with the @acronym{POSIX} standard.  To suppress these
4483 incompatibilities, define the @env{POSIXLY_CORRECT} environment
4484 variable.  Unless you are checking for @acronym{POSIX} conformance, you
4485 probably do not need to define @env{POSIXLY_CORRECT}.
4486
4487 Normally options and operands can appear in any order, and programs act
4488 as if all the options appear before any operands.  For example,
4489 @samp{diff lao tzu -C 2} acts like @samp{diff -C 2 lao tzu}, since
4490 @samp{2} is an option-argument of @option{-C}.  However, if the
4491 @env{POSIXLY_CORRECT} environment variable is set, options must appear
4492 before operands, unless otherwise specified for a particular command.
4493
4494 Newer versions of @acronym{POSIX} are occasionally incompatible with older
4495 versions.  For example, older versions of @acronym{POSIX} allowed the
4496 command @samp{diff -c -10} to have the same meaning as @samp{diff -C
4497 10}, but @acronym{POSIX} 1003.1-2001 @samp{diff} no longer allows
4498 digit-string options like @option{-10}.
4499
4500 @vindex _POSIX2_VERSION
4501 The @acronym{GNU} utilities normally conform to the version of @acronym{POSIX}
4502 that is standard for your system.  To cause them to conform to a
4503 different version of @acronym{POSIX}, define the @env{_POSIX2_VERSION}
4504 environment variable to a value of the form @var{yyyymm} specifying
4505 the year and month the standard was adopted.  Two values are currently
4506 supported for @env{_POSIX2_VERSION}: @samp{199209} stands for
4507 @acronym{POSIX} 1003.2-1992, and @samp{200112} stands for @acronym{POSIX}
4508 1003.1-2001.  For example, if you are running older software that
4509 assumes an older version of @acronym{POSIX} and uses @samp{diff -c -10},
4510 you can work around the compatibility problems by setting
4511 @samp{_POSIX2_VERSION=199209} in your environment.
4512
4513 @node Projects
4514 @chapter Future Projects
4515
4516 Here are some ideas for improving @acronym{GNU} @command{diff} and
4517 @command{patch}.  The @acronym{GNU} project has identified some
4518 improvements as potential programming projects for volunteers.  You
4519 can also help by reporting any bugs that you find.
4520
4521 If you are a programmer and would like to contribute something to the
4522 @acronym{GNU} project, please consider volunteering for one of these
4523 projects.  If you are seriously contemplating work, please write to
4524 @email{gvc@@gnu.org} to coordinate with other volunteers.
4525
4526 @menu
4527 * Shortcomings:: Suggested projects for improvements.
4528 * Bugs::         Reporting bugs.
4529 @end menu
4530
4531 @node Shortcomings
4532 @section Suggested Projects for Improving @acronym{GNU} @command{diff} and @command{patch}
4533 @cindex projects for directories
4534
4535 One should be able to use @acronym{GNU} @command{diff} to generate a
4536 patch from any pair of directory trees, and given the patch and a copy
4537 of one such tree, use @command{patch} to generate a faithful copy of
4538 the other.  Unfortunately, some changes to directory trees cannot be
4539 expressed using current patch formats; also, @command{patch} does not
4540 handle some of the existing formats.  These shortcomings motivate the
4541 following suggested projects.
4542
4543 @menu
4544 * Internationalization:: Handling multibyte and varying-width characters.
4545 * Changing Structure::   Handling changes to the directory structure.
4546 * Special Files::        Handling symbolic links, device special files, etc.
4547 * Unusual File Names::   Handling file names that contain unusual characters.
4548 * Time Stamp Order::     Outputting diffs in time stamp order.
4549 * Ignoring Changes::     Ignoring certain changes while showing others.
4550 * Speedups::             Improving performance.
4551 @end menu
4552
4553 @node Internationalization
4554 @subsection Handling Multibyte and Varying-Width Characters
4555 @cindex multibyte characters
4556 @cindex varying-width characters
4557
4558 @command{diff}, @command{diff3} and @command{sdiff} treat each line of
4559 input as a string of unibyte characters.  This can mishandle multibyte
4560 characters in some cases.  For example, when asked to ignore spaces,
4561 @command{diff} does not properly ignore a multibyte space character.
4562
4563 Also, @command{diff} currently assumes that each byte is one column
4564 wide, and this assumption is incorrect in some locales, e.g., locales
4565 that use UTF-8 encoding.  This causes problems with the @option{-y} or
4566 @option{--side-by-side} option of @command{diff}.
4567
4568 These problems need to be fixed without unduly affecting the
4569 performance of the utilities in unibyte environments.
4570
4571 The IBM GNU/Linux Technology Center Internationalization Team has
4572 proposed
4573 @uref{http://oss.software.ibm.com/developer/opensource/linux/patches/i18n/diffutils-2.7.2-i18n-0.1.patch.gz,patches
4574 to support internationalized @command{diff}}.
4575 Unfortunately, these patches are incomplete and are to an older
4576 version of @command{diff}, so more work needs to be done in this area.
4577
4578 @node Changing Structure
4579 @subsection Handling Changes to the Directory Structure
4580 @cindex directory structure changes
4581
4582 @command{diff} and @command{patch} do not handle some changes to directory
4583 structure.  For example, suppose one directory tree contains a directory
4584 named @samp{D} with some subsidiary files, and another contains a file
4585 with the same name @samp{D}.  @samp{diff -r} does not output enough
4586 information for @command{patch} to transform the directory subtree into
4587 the file.
4588
4589 There should be a way to specify that a file has been removed without
4590 having to include its entire contents in the patch file.  There should
4591 also be a way to tell @command{patch} that a file was renamed, even if
4592 there is no way for @command{diff} to generate such information.
4593 There should be a way to tell @command{patch} that a file's time stamp
4594 has changed, even if its contents have not changed.
4595
4596 These problems can be fixed by extending the @command{diff} output format
4597 to represent changes in directory structure, and extending @command{patch}
4598 to understand these extensions.
4599
4600 @node Special Files
4601 @subsection Files that are Neither Directories Nor Regular Files
4602 @cindex special files
4603
4604 Some files are neither directories nor regular files: they are unusual
4605 files like symbolic links, device special files, named pipes, and
4606 sockets.  Currently, @command{diff} treats symbolic links as if they
4607 were the pointed-to files, except that a recursive @command{diff}
4608 reports an error if it detects infinite loops of symbolic links (e.g.,
4609 symbolic links to @file{..}).  @command{diff} treats other special
4610 files like regular files if they are specified at the top level, but
4611 simply reports their presence when comparing directories.  This means
4612 that @command{patch} cannot represent changes to such files.  For
4613 example, if you change which file a symbolic link points to,
4614 @command{diff} outputs the difference between the two files, instead
4615 of the change to the symbolic link.
4616
4617 @c This might not be a good idea; is it wise for root to install devices
4618 @c this way?
4619 @command{diff} should optionally report changes to special files specially,
4620 and @command{patch} should be extended to understand these extensions.
4621
4622 @node Unusual File Names
4623 @subsection File Names that Contain Unusual Characters
4624 @cindex file names with unusual characters
4625
4626 When a file name contains an unusual character like a newline or
4627 white space, @samp{diff -r} generates a patch that @command{patch} cannot
4628 parse.  The problem is with format of @command{diff} output, not just with
4629 @command{patch}, because with odd enough file names one can cause
4630 @command{diff} to generate a patch that is syntactically correct but
4631 patches the wrong files.  The format of @command{diff} output should be
4632 extended to handle all possible file names.
4633
4634 @node Time Stamp Order
4635 @subsection Outputting Diffs in Time Stamp Order
4636
4637 Applying @command{patch} to a multiple-file diff can result in files
4638 whose time stamps are out of order.  @acronym{GNU} @command{patch} has
4639 options to restore the time stamps of the updated files
4640 (@pxref{Patching Time Stamps}), but sometimes it is useful to generate
4641 a patch that works even if the recipient does not have @acronym{GNU} patch,
4642 or does not use these options.  One way to do this would be to
4643 implement a @command{diff} option to output diffs in time stamp order.
4644
4645 @node Ignoring Changes
4646 @subsection Ignoring Certain Changes
4647
4648 It would be nice to have a feature for specifying two strings, one in
4649 @var{from-file} and one in @var{to-file}, which should be considered to
4650 match.  Thus, if the two strings are @samp{foo} and @samp{bar}, then if
4651 two lines differ only in that @samp{foo} in file 1 corresponds to
4652 @samp{bar} in file 2, the lines are treated as identical.
4653
4654 It is not clear how general this feature can or should be, or
4655 what syntax should be used for it.
4656
4657 A partial substitute is to filter one or both files before comparing,
4658 e.g.:
4659
4660 @example
4661 sed 's/foo/bar/g' file1 | diff - file2
4662 @end example
4663
4664 However, this outputs the filtered text, not the original.
4665
4666 @node Speedups
4667 @subsection Improving Performance
4668
4669 When comparing two large directory structures, one of which was
4670 originally copied from the other with time stamps preserved (e.g.,
4671 with @samp{cp -pR}), it would greatly improve performance if an option
4672 told @command{diff} to assume that two files with the same size and
4673 time stamps have the same content.  @xref{diff Performance}.
4674
4675 @node Bugs
4676 @section Reporting Bugs
4677 @cindex bug reports
4678 @cindex reporting bugs
4679
4680 If you think you have found a bug in @acronym{GNU} @command{cmp},
4681 @command{diff}, @command{diff3}, or @command{sdiff}, please report it
4682 by electronic mail to the
4683 @uref{http://mail.gnu.org/mailman/listinfo/bug-gnu-utils,GNU utilities
4684 bug report mailing list} @email{bug-gnu-utils@@gnu.org}.  Please send
4685 bug reports for @acronym{GNU} @command{patch} to
4686 @email{bug-patch@@gnu.org}.  Send as precise a description of the
4687 problem as you can, including the output of the @option{--version}
4688 option and sample input files that produce the bug, if applicable.  If
4689 you have a nontrivial fix for the bug, please send it as well.  If you
4690 have a patch, please send it too.  It may simplify the maintainer's
4691 job if the patch is relative to a recent test release, which you can
4692 find in the directory @uref{ftp://alpha.gnu.org/gnu/diffutils/}.
4693
4694 @node Copying This Manual
4695 @appendix Copying This Manual
4696 @include fdl.texi
4697
4698 @node Translations
4699 @appendix Translations of This Manual
4700
4701 Nishio Futoshi of the GNUjdoc project has prepared a Japanese
4702 translation of this manual.  Its most recent version can be found at
4703 @uref{http://openlab.ring.gr.jp/gnujdoc/cvsweb/cvsweb.cgi/gnujdoc/}.
4704
4705 @node Index
4706 @appendix Index
4707
4708 @printindex cp
4709
4710 @bye