222e9b7c50c5ebbac293d0cb0520eacc8423c0a1
[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-2011 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; see @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
1733 Formats}.  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 For file names that are in only one of the directories, @command{diff}
1788 normally does not show the contents of the file that exists; it reports
1789 only that the file exists in that directory and not in the other.  You
1790 can make @command{diff} act as though the file existed but was empty in the
1791 other directory, so that it outputs the entire contents of the file that
1792 actually exists.  (It is output as either an insertion or a
1793 deletion, depending on whether it is in the first or the second
1794 directory given.)  To do this, use the @option{--new-file} (@option{-N})
1795 option.
1796
1797 If the older directory contains one or more large files that are not in
1798 the newer directory, you can make the patch smaller by using the
1799 @option{--unidirectional-new-file} option instead of @option{-N}.
1800 This option is like @option{-N} except that it only inserts the contents
1801 of files that appear in the second directory but not the first (that is,
1802 files that were added).  At the top of the patch, write instructions for
1803 the user applying the patch to remove the files that were deleted before
1804 applying the patch.  @xref{Making Patches}, for more discussion of
1805 making patches for distribution.
1806
1807 To ignore some files while comparing directories, use the
1808 @option{--exclude=@var{pattern}} (@option{-x @var{pattern}}) option.
1809 This option
1810 ignores any files or subdirectories whose base names match the shell
1811 pattern @var{pattern}.  Unlike in the shell, a period at the start of
1812 the base of a file name matches a wildcard at the start of a pattern.
1813 You should enclose @var{pattern} in quotes so that the shell does not
1814 expand it.  For example, the option @option{-x '*.[ao]'} ignores any file
1815 whose name ends with @samp{.a} or @samp{.o}.
1816
1817 This option accumulates if you specify it more than once.  For example,
1818 using the options @option{-x 'RCS' -x '*,v'} ignores any file or
1819 subdirectory whose base name is @samp{RCS} or ends with @samp{,v}.
1820
1821 If you need to give this option many times, you can instead put the
1822 patterns in a file, one pattern per line, and use the
1823 @option{--exclude-from=@var{file}} (@option{-X @var{file}}) option.
1824 Trailing white space and empty lines are ignored in the pattern file.
1825
1826 If you have been comparing two directories and stopped partway through,
1827 later you might want to continue where you left off.  You can do this by
1828 using the @option{--starting-file=@var{file}} (@option{-S @var{file}})
1829 option.  This compares only the file @var{file} and all alphabetically
1830 later files in the topmost directory level.
1831
1832 If two directories differ only in that file names are lower case in
1833 one directory and upper case in the upper, @command{diff} normally
1834 reports many differences because it compares file names in a
1835 case sensitive way.  With the @option{--ignore-file-name-case} option,
1836 @command{diff} ignores case differences in file names, so that for example
1837 the contents of the file @file{Tao} in one directory are compared to
1838 the contents of the file @file{TAO} in the other.  The
1839 @option{--no-ignore-file-name-case} option cancels the effect of the
1840 @option{--ignore-file-name-case} option, reverting to the default
1841 behavior.
1842
1843 If an @option{--exclude=@var{pattern}} (@option{-x @var{pattern}}) option,
1844 or an @option{--exclude-from=@var{file}} (@option{-X @var{file}}) option,
1845 is specified while the @option{--ignore-file-name-case} option is in
1846 effect, case is ignored when excluding file names matching the
1847 specified patterns.
1848
1849 @node Adjusting Output
1850 @chapter Making @command{diff} Output Prettier
1851
1852 @command{diff} provides several ways to adjust the appearance of its output.
1853 These adjustments can be applied to any output format.
1854
1855 @menu
1856 * Tabs::            Preserving the alignment of tab stops.
1857 * Trailing Blanks:: Suppressing blanks before empty output lines.
1858 * Pagination::      Page numbering and time-stamping @command{diff} output.
1859 @end menu
1860
1861 @node Tabs
1862 @section Preserving Tab Stop Alignment
1863 @cindex tab stop alignment
1864 @cindex aligning tab stops
1865
1866 The lines of text in some of the @command{diff} output formats are
1867 preceded by one or two characters that indicate whether the text is
1868 inserted, deleted, or changed.  The addition of those characters can
1869 cause tabs to move to the next tab stop, throwing off the alignment of
1870 columns in the line.  @acronym{GNU} @command{diff} provides two ways
1871 to make tab-aligned columns line up correctly.
1872
1873 The first way is to have @command{diff} convert all tabs into the correct
1874 number of spaces before outputting them; select this method with the
1875 @option{--expand-tabs} (@option{-t}) option.  To use this form of output with
1876 @command{patch}, you must give @command{patch} the @option{-l} or
1877 @option{--ignore-white-space} option (@pxref{Changed White Space}, for more
1878 information).  @command{diff} normally assumes that tab stops are set
1879 every 8 print columns, but this can be altered by the
1880 @option{--tabsize=@var{columns}} option.
1881
1882 The other method for making tabs line up correctly is to add a tab
1883 character instead of a space after the indicator character at the
1884 beginning of the line.  This ensures that all following tab characters
1885 are in the same position relative to tab stops that they were in the
1886 original files, so that the output is aligned correctly.  Its
1887 disadvantage is that it can make long lines too long to fit on one line
1888 of the screen or the paper.  It also does not work with the unified
1889 output format, which does not have a space character after the change
1890 type indicator character.  Select this method with the @option{-T} or
1891 @option{--initial-tab} option.
1892
1893 @node Trailing Blanks
1894 @section Omitting trailing blanks
1895 @cindex trailing blanks
1896 When outputting lines in normal or context format, or outputting an
1897 unchanged line in unified format, @command{diff} normally outputs a
1898 blank just before each line.  If the line is empty, the output of
1899 @command{diff} therefore contains trailing blanks even though the
1900 input does not contain them.  For example, when outputting an
1901 unchanged empty line in context format, @command{diff} normally
1902 outputs a line with two leading spaces.
1903
1904 Some text editors and email agents routinely delete trailing blanks,
1905 so it can be a problem to deal with diff output files that contain
1906 them.  You can avoid this problem with the
1907 @option{--suppress-blank-empty} option.  It causes @command{diff} to
1908 omit trailing blanks at the end of output lines in normal, context,
1909 and unified format, unless the trailing blanks were already present in
1910 the input.  This changes the output format slightly, so that output
1911 lines are guaranteed to never end in a blank unless an input line ends
1912 in a blank.  This format is less likely to be munged by text editors
1913 or by transmission via email.  It is accepted by @acronym{GNU}
1914 @command{patch} as well.
1915
1916 @node Pagination
1917 @section Paginating @command{diff} Output
1918 @cindex paginating @command{diff} output
1919
1920 It can be convenient to have long output page-numbered and time-stamped.
1921 The @option{--paginate} (@option{-l}) option does this by sending the
1922 @command{diff} output through the @command{pr} program.  Here is what the page
1923 header might look like for @samp{diff -lc lao tzu}:
1924
1925 @example
1926 2002-02-22 14:20                 diff -lc lao tzu                 Page 1
1927 @end example
1928
1929 @node diff Performance
1930 @chapter @command{diff} Performance Tradeoffs
1931 @cindex performance of @command{diff}
1932
1933 @acronym{GNU} @command{diff} runs quite efficiently; however, in some
1934 circumstances you can cause it to run faster or produce a more compact
1935 set of changes.
1936
1937 One way to improve @command{diff} performance is to use hard or
1938 symbolic links to files instead of copies.  This improves performance
1939 because @command{diff} normally does not need to read two hard or
1940 symbolic links to the same file, since their contents must be
1941 identical.  For example, suppose you copy a large directory hierarchy,
1942 make a few changes to the copy, and then often use @samp{diff -r} to
1943 compare the original to the copy.  If the original files are
1944 read-only, you can greatly improve performance by creating the copy
1945 using hard or symbolic links (e.g., with @acronym{GNU} @samp{cp -lR} or
1946 @samp{cp -sR}).  Before editing a file in the copy for the first time,
1947 you should break the link and replace it with a regular copy.
1948
1949 You can also affect the performance of @acronym{GNU} @command{diff} by
1950 giving it options that change the way it compares files.
1951 Performance has more than one dimension.  These options improve one
1952 aspect of performance at the cost of another, or they improve
1953 performance in some cases while hurting it in others.
1954
1955 The way that @acronym{GNU} @command{diff} determines which lines have
1956 changed always comes up with a near-minimal set of differences.
1957 Usually it is good enough for practical purposes.  If the
1958 @command{diff} output is large, you might want @command{diff} to use a
1959 modified algorithm that sometimes produces a smaller set of
1960 differences.  The @option{--minimal} (@option{-d}) option does this;
1961 however, it can also cause @command{diff} to run more slowly than
1962 usual, so it is not the default behavior.
1963
1964 When the files you are comparing are large and have small groups of
1965 changes scattered throughout them, you can use the
1966 @option{--speed-large-files} option to make a different modification to
1967 the algorithm that @command{diff} uses.  If the input files have a constant
1968 small density of changes, this option speeds up the comparisons without
1969 changing the output.  If not, @command{diff} might produce a larger set of
1970 differences; however, the output will still be correct.
1971
1972 Normally @command{diff} discards the prefix and suffix that is common to
1973 both files before it attempts to find a minimal set of differences.
1974 This makes @command{diff} run faster, but occasionally it may produce
1975 non-minimal output.  The @option{--horizon-lines=@var{lines}} option
1976 prevents @command{diff} from discarding the last @var{lines} lines of the
1977 prefix and the first @var{lines} lines of the suffix.  This gives
1978 @command{diff} further opportunities to find a minimal output.
1979
1980 Suppose a run of changed lines includes a sequence of lines at one end
1981 and there is an identical sequence of lines just outside the other end.
1982 The @command{diff} command is free to choose which identical sequence is
1983 included in the hunk.  In this case, @command{diff} normally shifts the
1984 hunk's boundaries when this merges adjacent hunks, or shifts a hunk's
1985 lines towards the end of the file.  Merging hunks can make the output
1986 look nicer in some cases.
1987
1988 @node Comparing Three Files
1989 @chapter Comparing Three Files
1990 @cindex comparing three files
1991 @cindex format of @command{diff3} output
1992
1993 Use the program @command{diff3} to compare three files and show any
1994 differences among them.  (@command{diff3} can also merge files; see
1995 @ref{diff3 Merging}).
1996
1997 The ``normal'' @command{diff3} output format shows each hunk of
1998 differences without surrounding context.  Hunks are labeled depending
1999 on whether they are two-way or three-way, and lines are annotated by
2000 their location in the input files.
2001
2002 @xref{Invoking diff3}, for more information on how to run @command{diff3}.
2003
2004 @menu
2005 * Sample diff3 Input::    Sample @command{diff3} input for examples.
2006 * Example diff3 Normal::  Sample output in the normal format.
2007 * diff3 Hunks::           The format of normal output format.
2008 * Detailed diff3 Normal:: A detailed description of normal output format.
2009 @end menu
2010
2011 @node Sample diff3 Input
2012 @section A Third Sample Input File
2013 @cindex @command{diff3} sample input
2014 @cindex sample input for @command{diff3}
2015
2016 Here is a third sample file that will be used in examples to illustrate
2017 the output of @command{diff3} and how various options can change it.  The
2018 first two files are the same that we used for @command{diff} (@pxref{Sample
2019 diff Input}).  This is the third sample file, called @file{tao}:
2020
2021 @example
2022 The Way that can be told of is not the eternal Way;
2023 The name that can be named is not the eternal name.
2024 The Nameless is the origin of Heaven and Earth;
2025 The named is the mother of all things.
2026
2027 Therefore let there always be non-being,
2028   so we may see their subtlety,
2029 And let there always be being,
2030   so we may see their result.
2031 The two are the same,
2032 But after they are produced,
2033   they have different names.
2034
2035   -- The Way of Lao-Tzu, tr. Wing-tsit Chan
2036 @end example
2037
2038 @node Example diff3 Normal
2039 @section An Example of @command{diff3} Normal Format
2040
2041 Here is the output of the command @samp{diff3 lao tzu tao}
2042 (@pxref{Sample diff3 Input}, for the complete contents of the files).
2043 Notice that it shows only the lines that are different among the three
2044 files.
2045
2046 @example
2047 ====2
2048 1:1,2c
2049 3:1,2c
2050   The Way that can be told of is not the eternal Way;
2051   The name that can be named is not the eternal name.
2052 2:0a
2053 ====1
2054 1:4c
2055   The Named is the mother of all things.
2056 2:2,3c
2057 3:4,5c
2058   The named is the mother of all things.
2059   @-
2060 ====3
2061 1:8c
2062 2:7c
2063     so we may see their outcome.
2064 3:9c
2065     so we may see their result.
2066 ====
2067 1:11a
2068 2:11,13c
2069   They both may be called deep and profound.
2070   Deeper and more profound,
2071   The door of all subtleties!
2072 3:13,14c
2073   @-
2074     -- The Way of Lao-Tzu, tr. Wing-tsit Chan
2075 @end example
2076
2077 @node Detailed diff3 Normal
2078 @section Detailed Description of @command{diff3} Normal Format
2079
2080 Each hunk begins with a line marked @samp{====}.  Three-way hunks have
2081 plain @samp{====} lines, and two-way hunks have @samp{1}, @samp{2}, or
2082 @samp{3} appended to specify which of the three input files differ in
2083 that hunk.  The hunks contain copies of two or three sets of input
2084 lines each preceded by one or two commands identifying where the lines
2085 came from.
2086
2087 Normally, two spaces precede each copy of an input line to distinguish
2088 it from the commands.  But with the @option{--initial-tab} (@option{-T})
2089 option, @command{diff3} uses a tab instead of two spaces; this lines up
2090 tabs correctly.  @xref{Tabs}, for more information.
2091
2092 Commands take the following forms:
2093
2094 @table @samp
2095 @item @var{file}:@var{l}a
2096 This hunk appears after line @var{l} of file @var{file}, and
2097 contains no lines in that file.  To edit this file to yield the other
2098 files, one must append hunk lines taken from the other files.  For
2099 example, @samp{1:11a} means that the hunk follows line 11 in the first
2100 file and contains no lines from that file.
2101
2102 @item @var{file}:@var{r}c
2103 This hunk contains the lines in the range @var{r} of file @var{file}.
2104 The range @var{r} is a comma-separated pair of line numbers, or just one
2105 number if there is only one line.  To edit this file to yield the
2106 other files, one must change the specified lines to be the lines taken
2107 from the other files.  For example, @samp{2:11,13c} means that the hunk
2108 contains lines 11 through 13 from the second file.
2109 @end table
2110
2111 If the last line in a set of input lines is incomplete
2112 (@pxref{Incomplete Lines}), it is distinguished on output from a full
2113 line by a following line that starts with @samp{\}.
2114
2115 @node diff3 Hunks
2116 @section @command{diff3} Hunks
2117 @cindex hunks for @command{diff3}
2118 @cindex @command{diff3} hunks
2119
2120 Groups of lines that differ in two or three of the input files are
2121 called @dfn{diff3 hunks}, by analogy with @command{diff} hunks
2122 (@pxref{Hunks}).  If all three input files differ in a @command{diff3}
2123 hunk, the hunk is called a @dfn{three-way hunk}; if just two input files
2124 differ, it is a @dfn{two-way hunk}.
2125
2126 As with @command{diff}, several solutions are possible.  When comparing the
2127 files @samp{A}, @samp{B}, and @samp{C}, @command{diff3} normally finds
2128 @command{diff3} hunks by merging the two-way hunks output by the two
2129 commands @samp{diff A B} and @samp{diff A C}.  This does not necessarily
2130 minimize the size of the output, but exceptions should be rare.
2131
2132 For example, suppose @file{F} contains the three lines @samp{a},
2133 @samp{b}, @samp{f}, @file{G} contains the lines @samp{g}, @samp{b},
2134 @samp{g}, and @file{H} contains the lines @samp{a}, @samp{b},
2135 @samp{h}.  @samp{diff3 F G H} might output the following:
2136
2137 @example
2138 ====2
2139 1:1c
2140 3:1c
2141   a
2142 2:1c
2143   g
2144 ====
2145 1:3c
2146   f
2147 2:3c
2148   g
2149 3:3c
2150   h
2151 @end example
2152
2153 @noindent
2154 because it found a two-way hunk containing @samp{a} in the first and
2155 third files and @samp{g} in the second file, then the single line
2156 @samp{b} common to all three files, then a three-way hunk containing
2157 the last line of each file.
2158
2159 @node diff3 Merging
2160 @chapter Merging From a Common Ancestor
2161 @cindex merging from a common ancestor
2162
2163 When two people have made changes to copies of the same file,
2164 @command{diff3} can produce a merged output that contains both sets of
2165 changes together with warnings about conflicts.
2166
2167 One might imagine programs with names like @command{diff4} and @command{diff5}
2168 to compare more than three files simultaneously, but in practice the
2169 need rarely arises.  You can use @command{diff3} to merge three or more
2170 sets of changes to a file by merging two change sets at a time.
2171
2172 @command{diff3} can incorporate changes from two modified versions into a
2173 common preceding version.  This lets you merge the sets of changes
2174 represented by the two newer files.  Specify the common ancestor version
2175 as the second argument and the two newer versions as the first and third
2176 arguments, like this:
2177
2178 @example
2179 diff3 @var{mine} @var{older} @var{yours}
2180 @end example
2181
2182 @noindent
2183 You can remember the order of the arguments by noting that they are in
2184 alphabetical order.
2185
2186 @cindex conflict
2187 @cindex overlap
2188 You can think of this as subtracting @var{older} from @var{yours} and
2189 adding the result to @var{mine}, or as merging into @var{mine} the
2190 changes that would turn @var{older} into @var{yours}.  This merging is
2191 well-defined as long as @var{mine} and @var{older} match in the
2192 neighborhood of each such change.  This fails to be true when all three
2193 input files differ or when only @var{older} differs; we call this
2194 a @dfn{conflict}.  When all three input files differ, we call the
2195 conflict an @dfn{overlap}.
2196
2197 @command{diff3} gives you several ways to handle overlaps and conflicts.
2198 You can omit overlaps or conflicts, or select only overlaps,
2199 or mark conflicts with special @samp{<<<<<<<} and @samp{>>>>>>>} lines.
2200
2201 @command{diff3} can output the merge results as an @command{ed} script that
2202 that can be applied to the first file to yield the merged output.
2203 However, it is usually better to have @command{diff3} generate the merged
2204 output directly; this bypasses some problems with @command{ed}.
2205
2206 @menu
2207 * Which Changes::            Selecting changes to incorporate.
2208 * Marking Conflicts::        Marking conflicts.
2209 * Bypassing ed::             Generating merged output directly.
2210 * Merging Incomplete Lines:: How @command{diff3} merges incomplete lines.
2211 * Saving the Changed File::  Emulating System V behavior.
2212 @end menu
2213
2214 @node Which Changes
2215 @section Selecting Which Changes to Incorporate
2216 @cindex overlapping change, selection of
2217 @cindex unmerged change
2218
2219 You can select all unmerged changes from @var{older} to @var{yours} for merging
2220 into @var{mine} with the @option{--ed} (@option{-e}) option.  You can
2221 select only the nonoverlapping unmerged changes with
2222 @option{--easy-only} (@option{-3}),
2223 and you can select only the overlapping changes with
2224 @option{--overlap-only} (@option{-x}).
2225
2226 The @option{-e}, @option{-3} and @option{-x} options select only
2227 @dfn{unmerged changes}, i.e.@: changes where @var{mine} and @var{yours}
2228 differ; they ignore changes from @var{older} to @var{yours} where
2229 @var{mine} and @var{yours} are identical, because they assume that such
2230 changes have already been merged.  If this assumption is not a safe
2231 one, you can use the @option{--show-all} (@option{-A}) option
2232 (@pxref{Marking Conflicts}).
2233
2234 Here is the output of the command @command{diff3} with each of these three
2235 options (@pxref{Sample diff3 Input}, for the complete contents of the files).
2236 Notice that @option{-e} outputs the union of the disjoint sets of changes
2237 output by @option{-3} and @option{-x}.
2238
2239 Output of @samp{diff3 -e lao tzu tao}:
2240 @example
2241 11a
2242
2243   -- The Way of Lao-Tzu, tr. Wing-tsit Chan
2244 .
2245 8c
2246   so we may see their result.
2247 .
2248 @end example
2249
2250 Output of @samp{diff3 -3 lao tzu tao}:
2251 @example
2252 8c
2253   so we may see their result.
2254 .
2255 @end example
2256
2257 Output of @samp{diff3 -x lao tzu tao}:
2258 @example
2259 11a
2260
2261   -- The Way of Lao-Tzu, tr. Wing-tsit Chan
2262 .
2263 @end example
2264
2265 @node Marking Conflicts
2266 @section Marking Conflicts
2267 @cindex conflict marking
2268 @cindex @samp{<<<<<<<} for marking conflicts
2269
2270 @command{diff3} can mark conflicts in the merged output by
2271 bracketing them with special marker lines.  A conflict
2272 that comes from two files @var{A} and @var{B} is marked as follows:
2273
2274 @example
2275 <<<<<<< @var{A}
2276 @r{lines from @var{A}}
2277 =======
2278 @r{lines from @var{B}}
2279 >>>>>>> @var{B}
2280 @end example
2281
2282 A conflict that comes from three files @var{A}, @var{B} and @var{C} is
2283 marked as follows:
2284
2285 @example
2286 <<<<<<< @var{A}
2287 @r{lines from @var{A}}
2288 ||||||| @var{B}
2289 @r{lines from @var{B}}
2290 =======
2291 @r{lines from @var{C}}
2292 >>>>>>> @var{C}
2293 @end example
2294
2295 The @option{--show-all} (@option{-A}) option acts like the @option{-e}
2296 option, except that it brackets conflicts, and it outputs all changes
2297 from @var{older} to @var{yours}, not just the unmerged changes.  Thus,
2298 given the sample input files (@pxref{Sample diff3 Input}), @samp{diff3
2299 -A lao tzu tao} puts brackets around the conflict where only @file{tzu}
2300 differs:
2301
2302 @example
2303 <<<<<<< tzu
2304 =======
2305 The Way that can be told of is not the eternal Way;
2306 The name that can be named is not the eternal name.
2307 >>>>>>> tao
2308 @end example
2309
2310 And it outputs the three-way conflict as follows:
2311
2312 @example
2313 <<<<<<< lao
2314 ||||||| tzu
2315 They both may be called deep and profound.
2316 Deeper and more profound,
2317 The door of all subtleties!
2318 =======
2319
2320   -- The Way of Lao-Tzu, tr. Wing-tsit Chan
2321 >>>>>>> tao
2322 @end example
2323
2324 The @option{--show-overlap} (@option{-E}) option outputs less information
2325 than the @option{--show-all} (@option{-A}) option, because it outputs only
2326 unmerged changes, and it never outputs the contents of the second
2327 file.  Thus the @option{-E} option acts like the @option{-e} option,
2328 except that it brackets the first and third files from three-way
2329 overlapping changes.  Similarly, @option{-X} acts like @option{-x}, except
2330 it brackets all its (necessarily overlapping) changes.  For example,
2331 for the three-way overlapping change above, the @option{-E} and @option{-X}
2332 options output the following:
2333
2334 @example
2335 <<<<<<< lao
2336 =======
2337
2338   -- The Way of Lao-Tzu, tr. Wing-tsit Chan
2339 >>>>>>> tao
2340 @end example
2341
2342 If you are comparing files that have meaningless or uninformative names,
2343 you can use the @option{--label=@var{label}}
2344 option to show alternate names in the @samp{<<<<<<<}, @samp{|||||||}
2345 and @samp{>>>>>>>} brackets.  This option can be given up to three
2346 times, once for each input file.  Thus @samp{diff3 -A --label X
2347 --label Y --label Z A
2348 B C} acts like @samp{diff3 -A A B C}, except that the output looks like
2349 it came from files named @samp{X}, @samp{Y} and @samp{Z} rather than
2350 from files named @samp{A}, @samp{B} and @samp{C}.
2351
2352 @node Bypassing ed
2353 @section Generating the Merged Output Directly
2354 @cindex merged @command{diff3} format
2355
2356 With the @option{--merge} (@option{-m}) option, @command{diff3} outputs the
2357 merged file directly.  This is more efficient than using @command{ed} to
2358 generate it, and works even with non-text files that @command{ed} would
2359 reject.  If you specify @option{-m} without an @command{ed} script option,
2360 @option{-A} is assumed.
2361
2362 For example, the command @samp{diff3 -m lao tzu tao}
2363 (@pxref{Sample diff3 Input} for a copy of the input files) would output
2364 the following:
2365
2366 @example
2367 <<<<<<< tzu
2368 =======
2369 The Way that can be told of is not the eternal Way;
2370 The name that can be named is not the eternal name.
2371 >>>>>>> tao
2372 The Nameless is the origin of Heaven and Earth;
2373 The Named is the mother of all things.
2374 Therefore let there always be non-being,
2375   so we may see their subtlety,
2376 And let there always be being,
2377   so we may see their result.
2378 The two are the same,
2379 But after they are produced,
2380   they have different names.
2381 <<<<<<< lao
2382 ||||||| tzu
2383 They both may be called deep and profound.
2384 Deeper and more profound,
2385 The door of all subtleties!
2386 =======
2387
2388   -- The Way of Lao-Tzu, tr. Wing-tsit Chan
2389 >>>>>>> tao
2390 @end example
2391
2392 @node Merging Incomplete Lines
2393 @section How @command{diff3} Merges Incomplete Lines
2394 @cindex incomplete line merging
2395
2396 With @option{-m}, incomplete lines (@pxref{Incomplete Lines}) are simply
2397 copied to the output as they are found; if the merged output ends in an
2398 conflict and one of the input files ends in an incomplete
2399 line, succeeding @samp{|||||||}, @samp{=======} or @samp{>>>>>>>}
2400 brackets appear somewhere other than the start of a line because
2401 they are appended to the incomplete line.
2402
2403 Without @option{-m}, if an @command{ed} script option is specified and an
2404 incomplete line is found, @command{diff3} generates a warning and acts as
2405 if a newline had been present.
2406
2407 @node Saving the Changed File
2408 @section Saving the Changed File
2409 @cindex System V @command{diff3} compatibility
2410
2411 Traditional Unix @command{diff3} generates an @command{ed} script without the
2412 trailing @samp{w} and @samp{q} commands that save the changes.
2413 System V @command{diff3} generates these extra commands.  @acronym{GNU}
2414 @command{diff3} normally behaves like traditional Unix
2415 @command{diff3}, but with the @option{-i} option it behaves like
2416 System V @command{diff3} and appends the @samp{w} and @samp{q}
2417 commands.
2418
2419 The @option{-i} option requires one of the @command{ed} script options
2420 @option{-AeExX3}, and is incompatible with the merged output option
2421 @option{-m}.
2422
2423 @node Interactive Merging
2424 @chapter Interactive Merging with @command{sdiff}
2425 @cindex diff merging
2426 @cindex interactive merging
2427
2428 With @command{sdiff}, you can merge two files interactively based on a
2429 side-by-side @option{-y} format comparison (@pxref{Side by Side}).  Use
2430 @option{--output=@var{file}} (@option{-o @var{file}}) to specify where to
2431 put the merged text.  @xref{Invoking sdiff}, for more details on the
2432 options to @command{sdiff}.
2433
2434 Another way to merge files interactively is to use the Emacs Lisp
2435 package @command{emerge}.  @xref{emerge, , emerge, emacs, The
2436 @acronym{GNU} Emacs Manual}, for more information.
2437
2438 @menu
2439 * sdiff Option Summary:: Summary of @command{sdiff} options.
2440 * Merge Commands::       Merging two files interactively.
2441 @end menu
2442
2443 @node sdiff Option Summary
2444 @section Specifying @command{diff} Options to @command{sdiff}
2445 @cindex @command{sdiff} output format
2446
2447 The following @command{sdiff} options have the same meaning as for
2448 @command{diff}.  @xref{diff Options}, for the use of these options.
2449
2450 @example
2451 -a -b -d -i -t -v
2452 -B -E -I @var{regexp} -Z
2453
2454 --expand-tabs
2455 --ignore-blank-lines  --ignore-case
2456 --ignore-matching-lines=@var{regexp}  --ignore-space-change
2457 --ignore-tab-expansion  --ignore-trailing-space
2458 --left-column  --minimal  --speed-large-files
2459 --strip-trailing-cr  --suppress-common-lines
2460 --tabsize=@var{columns}  --text  --version  --width=@var{columns}
2461 @end example
2462
2463 For historical reasons, @command{sdiff} has alternate names for some
2464 options.  The @option{-l} option is equivalent to the
2465 @option{--left-column} option, and similarly @option{-s} is equivalent
2466 to @option{--suppress-common-lines}.  The meaning of the @command{sdiff}
2467 @option{-w} and @option{-W} options is interchanged from that of
2468 @command{diff}: with @command{sdiff}, @option{-w @var{columns}} is
2469 equivalent to @option{--width=@var{columns}}, and @option{-W} is
2470 equivalent to @option{--ignore-all-space}.  @command{sdiff} without the
2471 @option{-o} option is equivalent to @command{diff} with the
2472 @option{--side-by-side} (@option{-y}) option (@pxref{Side by Side}).
2473
2474 @node Merge Commands
2475 @section Merge Commands
2476 @cindex merge commands
2477 @cindex merging interactively
2478
2479 Groups of common lines, with a blank gutter, are copied from the first
2480 file to the output.  After each group of differing lines, @command{sdiff}
2481 prompts with @samp{%} and pauses, waiting for one of the following
2482 commands.  Follow each command with @key{RET}.
2483
2484 @table @samp
2485 @item e
2486 Discard both versions.
2487 Invoke a text editor on an empty temporary file,
2488 then copy the resulting file to the output.
2489
2490 @item eb
2491 Concatenate the two versions, edit the result in a temporary file,
2492 then copy the edited result to the output.
2493
2494 @item ed
2495 Like @samp{eb}, except precede each version with a header that
2496 shows what file and lines the version came from.
2497
2498 @item el
2499 @itemx e1
2500 Edit a copy of the left version, then copy the result to the output.
2501
2502 @item er
2503 @itemx e2
2504 Edit a copy of the right version, then copy the result to the output.
2505
2506 @item l
2507 @itemx 1
2508 Copy the left version to the output.
2509
2510 @item q
2511 Quit.
2512
2513 @item r
2514 @itemx 2
2515 Copy the right version to the output.
2516
2517 @item s
2518 Silently copy common lines.
2519
2520 @item v
2521 Verbosely copy common lines.  This is the default.
2522 @end table
2523
2524 @vindex EDITOR
2525 The text editor invoked is specified by the @env{EDITOR} environment
2526 variable if it is set.  The default is system-dependent.
2527
2528 @node Merging with patch
2529 @chapter Merging with @command{patch}
2530
2531 @command{patch} takes comparison output produced by @command{diff} and applies
2532 the differences to a copy of the original file, producing a patched
2533 version.  With @command{patch}, you can distribute just the changes to a
2534 set of files instead of distributing the entire file set; your
2535 correspondents can apply @command{patch} to update their copy of the files
2536 with your changes.  @command{patch} automatically determines the diff
2537 format, skips any leading or trailing headers, and uses the headers to
2538 determine which file to patch.  This lets your correspondents feed a
2539 mail message containing a difference listing directly to
2540 @command{patch}.
2541
2542 @command{patch} detects and warns about common problems like forward
2543 patches.  It saves any patches that it could not apply.  It can also maintain a
2544 @code{patchlevel.h} file to ensure that your correspondents apply
2545 diffs in the proper order.
2546
2547 @command{patch} accepts a series of diffs in its standard input, usually
2548 separated by headers that specify which file to patch.  It applies
2549 @command{diff} hunks (@pxref{Hunks}) one by one.  If a hunk does not
2550 exactly match the original file, @command{patch} uses heuristics to try to
2551 patch the file as well as it can.  If no approximate match can be found,
2552 @command{patch} rejects the hunk and skips to the next hunk.  @command{patch}
2553 normally replaces each file @var{f} with its new version, putting reject
2554 hunks (if any) into @samp{@var{f}.rej}.
2555
2556 @xref{Invoking patch}, for detailed information on the options to
2557 @command{patch}.
2558
2559 @menu
2560 * patch Input::            Selecting the type of @command{patch} input.
2561 * Revision Control::       Getting files from @acronym{RCS}, @acronym{SCCS}, etc.
2562 * Imperfect::              Dealing with imperfect patches.
2563 * Creating and Removing::  Creating and removing files with a patch.
2564 * Patching Time Stamps::   Updating time stamps on patched files.
2565 * Multiple Patches::       Handling multiple patches in a file.
2566 * patch Directories::      Changing directory and stripping directories.
2567 * Backups::                Whether backup files are made.
2568 * Backup Names::           Backup file names.
2569 * Reject Names::           Reject file names.
2570 * patch Messages::         Messages and questions @command{patch} can produce.
2571 * patch and POSIX::        Conformance to the @acronym{POSIX} standard.
2572 * patch and Tradition::    @acronym{GNU} versus traditional @command{patch}.
2573 @end menu
2574
2575 @node patch Input
2576 @section Selecting the @command{patch} Input Format
2577 @cindex @command{patch} input format
2578
2579 @command{patch} normally determines which @command{diff} format the patch
2580 file uses by examining its contents.  For patch files that contain
2581 particularly confusing leading text, you might need to use one of the
2582 following options to force @command{patch} to interpret the patch file as a
2583 certain format of diff.  The output formats listed here are the only
2584 ones that @command{patch} can understand.
2585
2586 @table @option
2587 @item -c
2588 @itemx --context
2589 context diff.
2590
2591 @item -e
2592 @itemx --ed
2593 @command{ed} script.
2594
2595 @item -n
2596 @itemx --normal
2597 normal diff.
2598
2599 @item -u
2600 @itemx --unified
2601 unified diff.
2602 @end table
2603
2604 @node Revision Control
2605 @section Revision Control
2606 @cindex revision control
2607 @cindex version control
2608 @cindex @acronym{RCS}
2609 @cindex ClearCase
2610 @cindex @acronym{SCCS}
2611
2612 If a nonexistent input file is under a revision control system
2613 supported by @command{patch}, @command{patch} normally asks the user
2614 whether to get (or check out) the file from the revision control
2615 system.  Patch currently supports @acronym{RCS}, ClearCase and
2616 @acronym{SCCS}.  Under @acronym{RCS} and @acronym{SCCS},
2617 @command{patch} also asks when the input file is read-only and matches
2618 the default version in the revision control system.
2619
2620 @vindex PATCH_GET
2621 The @option{--get=@var{num}} (@option{-g @var{num}}) option affects access
2622 to files under supported revision control systems.  If @var{num} is
2623 positive, @command{patch} gets the file without asking the user; if
2624 zero, @command{patch} neither asks the user nor gets the file; and if
2625 negative, @command{patch} asks the user before getting the file.  The
2626 default value of @var{num} is given by the value of the
2627 @env{PATCH_GET} environment variable if it is set; if not, the default
2628 value is zero if @command{patch} is conforming to @acronym{POSIX}, negative
2629 otherwise.  @xref{patch and POSIX}.
2630
2631 @vindex VERSION_CONTROL
2632 The choice of revision control system is unaffected by the
2633 @env{VERSION_CONTROL} environment variable (@pxref{Backup Names}).
2634
2635 @node Imperfect
2636 @section Applying Imperfect Patches
2637 @cindex imperfect patch application
2638
2639 @command{patch} tries to skip any leading text in the patch file,
2640 apply the diff, and then skip any trailing text.  Thus you can feed a
2641 mail message directly to @command{patch}, and it should work.  If the
2642 entire diff is indented by a constant amount of white space,
2643 @command{patch} automatically ignores the indentation.  If a context
2644 diff contains trailing carriage return on each line, @command{patch}
2645 automatically ignores the carriage return.  If a context diff has been
2646 encapsulated by prepending @w{@samp{- }} to lines beginning with @samp{-}
2647 as per @uref{ftp://ftp.isi.edu/in-notes/rfc934.txt, Internet RFC 934},
2648 @command{patch} automatically unencapsulates the input.
2649
2650 However, certain other types of imperfect input require user
2651 intervention or testing.
2652
2653 @menu
2654 * Changed White Space:: When tabs and spaces don't match exactly.
2655 * Reversed Patches::    Applying reversed patches correctly.
2656 * Inexact::             Helping @command{patch} find close matches.
2657 * Dry Runs::            Predicting what @command{patch} will do.
2658 @end menu
2659
2660 @node Changed White Space
2661 @subsection Applying Patches with Changed White Space
2662 @cindex white space in patches
2663
2664 Sometimes mailers, editors, or other programs change spaces into tabs,
2665 or vice versa.  If this happens to a patch file or an input file, the
2666 files might look the same, but @command{patch} will not be able to match
2667 them properly.  If this problem occurs, use the @option{-l} or
2668 @option{--ignore-white-space} option, which makes @command{patch} compare
2669 blank characters (i.e.@: spaces and tabs) loosely so that any nonempty
2670 sequence of blanks in the patch file matches any nonempty sequence of
2671 blanks in the input files.  Non-blank
2672 characters must still match exactly.  Each line of the context must
2673 still match a line in the input file.
2674
2675 @node Reversed Patches
2676 @subsection Applying Reversed Patches
2677 @cindex reversed patches
2678
2679 Sometimes people run @command{diff} with the new file first instead of
2680 second.  This creates a diff that is ``reversed''.  To apply such
2681 patches, give @command{patch} the @option{--reverse} (@option{-R}) option.
2682 @command{patch} then attempts to swap each hunk around before applying it.
2683 Rejects come out in the swapped format.
2684
2685 Often @command{patch} can guess that the patch is reversed.  If the first
2686 hunk of a patch fails, @command{patch} reverses the hunk to see if it can
2687 apply it that way.  If it can, @command{patch} asks you if you want to have
2688 the @option{-R} option set; if it can't, @command{patch} continues to apply
2689 the patch normally.  This method cannot detect a reversed patch if it is
2690 a normal diff and the first command is an append (which should have been
2691 a delete) since appends always succeed, because a null context matches
2692 anywhere.  But most patches add or change lines rather than delete them,
2693 so most reversed normal diffs begin with a delete, which fails, and
2694 @command{patch} notices.
2695
2696 If you apply a patch that you have already applied, @command{patch} thinks
2697 it is a reversed patch and offers to un-apply the patch.  This could be
2698 construed as a feature.  If you did this inadvertently and you don't
2699 want to un-apply the patch, just answer @samp{n} to this offer and to
2700 the subsequent ``apply anyway'' question---or type @kbd{C-c} to kill the
2701 @command{patch} process.
2702
2703 @node Inexact
2704 @subsection Helping @command{patch} Find Inexact Matches
2705 @cindex inexact patches
2706 @cindex fuzz factor when patching
2707
2708 For context diffs, and to a lesser extent normal diffs, @command{patch} can
2709 detect when the line numbers mentioned in the patch are incorrect, and
2710 it attempts to find the correct place to apply each hunk of the patch.
2711 As a first guess, it takes the line number mentioned in the hunk, plus
2712 or minus any offset used in applying the previous hunk.  If that is not
2713 the correct place, @command{patch} scans both forward and backward for a
2714 set of lines matching the context given in the hunk.
2715
2716 First @command{patch} looks for a place where all lines of the context
2717 match.  If it cannot find such a place, and it is reading a context or
2718 unified diff, and the maximum fuzz factor is set to 1 or more, then
2719 @command{patch} makes another scan, ignoring the first and last line of
2720 context.  If that fails, and the maximum fuzz factor is set to 2 or
2721 more, it makes another scan, ignoring the first two and last two lines
2722 of context are ignored.  It continues similarly if the maximum fuzz
2723 factor is larger.
2724
2725 The @option{--fuzz=@var{lines}} (@option{-F @var{lines}}) option sets the
2726 maximum fuzz factor to @var{lines}.  This option only applies to context
2727 and unified diffs; it ignores up to @var{lines} lines while looking for
2728 the place to install a hunk.  Note that a larger fuzz factor increases
2729 the odds of making a faulty patch.  The default fuzz factor is 2; there
2730 is no point to setting it to more than the number of lines of context
2731 in the diff, ordinarily 3.
2732
2733 If @command{patch} cannot find a place to install a hunk of the patch, it
2734 writes the hunk out to a reject file (@pxref{Reject Names}, for information
2735 on how reject files are named).  It writes out rejected hunks in context
2736 format no matter what form the input patch is in.  If the input is a
2737 normal or @command{ed} diff, many of the contexts are simply null.  The
2738 line numbers on the hunks in the reject file may be different from those
2739 in the patch file: they show the approximate location where @command{patch}
2740 thinks the failed hunks belong in the new file rather than in the old
2741 one.
2742
2743 If the @option{--verbose} option is given, then
2744 as it completes each hunk @command{patch} tells you whether the hunk
2745 succeeded or failed, and if it failed, on which line (in the new file)
2746 @command{patch} thinks the hunk should go.  If this is different from the
2747 line number specified in the diff, it tells you the offset.  A single
2748 large offset @emph{may} indicate that @command{patch} installed a hunk in
2749 the wrong place.  @command{patch} also tells you if it used a fuzz factor
2750 to make the match, in which case you should also be slightly suspicious.
2751
2752 @command{patch} cannot tell if the line numbers are off in an @command{ed}
2753 script, and can only detect wrong line numbers in a normal diff when it
2754 finds a change or delete command.  It may have the same problem with a
2755 context diff using a fuzz factor equal to or greater than the number of
2756 lines of context shown in the diff (typically 3).  In these cases, you
2757 should probably look at a context diff between your original and patched
2758 input files to see if the changes make sense.  Compiling without errors
2759 is a pretty good indication that the patch worked, but not a guarantee.
2760
2761 A patch against an empty file applies to a nonexistent file, and vice
2762 versa.  @xref{Creating and Removing}.
2763
2764 @command{patch} usually produces the correct results, even when it must
2765 make many guesses.  However, the results are guaranteed only when
2766 the patch is applied to an exact copy of the file that the patch was
2767 generated from.
2768
2769 @node Dry Runs
2770 @subsection Predicting what @command{patch} will do
2771 @cindex testing @command{patch}
2772 @cindex dry runs for @command{patch}
2773
2774 It may not be obvious in advance what @command{patch} will do with a
2775 complicated or poorly formatted patch.  If you are concerned that the
2776 input might cause @command{patch} to modify the wrong files, you can
2777 use the @option{--dry-run} option, which causes @command{patch} to
2778 print the results of applying patches without actually changing any
2779 files.  You can then inspect the diagnostics generated by the dry run
2780 to see whether @command{patch} will modify the files that you expect.
2781 If the patch does not do what you want, you can modify the patch (or
2782 the other options to @command{patch}) and try another dry run.  Once
2783 you are satisfied with the proposed patch you can apply it by invoking
2784 @command{patch} as before, but this time without the
2785 @option{--dry-run} option.
2786
2787 @node Creating and Removing
2788 @section Creating and Removing Files
2789 @cindex creating files
2790 @cindex empty files, removing
2791 @cindex removing empty files
2792
2793 Sometimes when comparing two directories, a file may exist in one
2794 directory but not the other.  If you give @command{diff} the
2795 @option{--new-file} (@option{-N}) option, or if you supply an old or
2796 new file that is named @file{/dev/null} or is empty and is dated the
2797 Epoch (1970-01-01 00:00:00 UTC), @command{diff} outputs a patch that
2798 adds or deletes the contents of this file.  When given such a patch,
2799 @command{patch} normally creates a new file or removes the old file.
2800 However, when conforming to @acronym{POSIX} (@pxref{patch and POSIX}),
2801 @command{patch} does not remove the old file, but leaves it empty.
2802 The @option{--remove-empty-files} (@option{-E}) option causes
2803 @command{patch} to remove output files that are empty after applying a
2804 patch, even if the patch does not appear to be one that removed the
2805 file.
2806
2807 If the patch appears to create a file that already exists,
2808 @command{patch} asks for confirmation before applying the patch.
2809
2810 @node Patching Time Stamps
2811 @section Updating Time Stamps on Patched Files
2812 @cindex time stamps on patched files
2813
2814 When @command{patch} updates a file, it normally sets the file's
2815 last-modified time stamp to the current time of day.  If you are using
2816 @command{patch} to track a software distribution, this can cause
2817 @command{make} to incorrectly conclude that a patched file is out of
2818 date.  For example, if @file{syntax.c} depends on @file{syntax.y}, and
2819 @command{patch} updates @file{syntax.c} and then @file{syntax.y}, then
2820 @file{syntax.c} will normally appear to be out of date with respect to
2821 @file{syntax.y} even though its contents are actually up to date.
2822
2823 The @option{--set-utc} (@option{-Z}) option causes @command{patch} to
2824 set a patched file's modification and access times to the time stamps
2825 given in context diff headers.  If the context diff headers do not
2826 specify a time zone, they are assumed to use Coordinated Universal
2827 Time (@acronym{UTC}, often known as @acronym{GMT}).
2828
2829 The @option{--set-time} (@option{-T}) option acts like @option{-Z} or
2830 @option{--set-utc}, except that it assumes that the context diff
2831 headers' time stamps use local time instead of @acronym{UTC}.  This option
2832 is not recommended, because patches using local time cannot easily be
2833 used by people in other time zones, and because local time stamps are
2834 ambiguous when local clocks move backwards during daylight-saving time
2835 adjustments.  If the context diff headers specify a time zone, this
2836 option is equivalent to @option{--set-utc} (@option{-Z}).
2837
2838 @command{patch} normally refrains from setting a file's time stamps if
2839 the file's original last-modified time stamp does not match the time
2840 given in the diff header, of if the file's contents do not exactly
2841 match the patch.  However, if the @option{--force} (@option{-f})
2842 option is given, the file's time stamps are set regardless.
2843
2844 Due to the limitations of the current @command{diff} format,
2845 @command{patch} cannot update the times of files whose contents have
2846 not changed.  Also, if you set file time stamps to values other than
2847 the current time of day, you should also remove (e.g., with @samp{make
2848 clean}) all files that depend on the patched files, so that later
2849 invocations of @command{make} do not get confused by the patched
2850 files' times.
2851
2852 @node Multiple Patches
2853 @section Multiple Patches in a File
2854 @cindex multiple patches
2855 @cindex intuiting file names from patches
2856
2857 If the patch file contains more than one patch, and if you do not
2858 specify an input file on the command line, @command{patch} tries to
2859 apply each patch as if they came from separate patch files.  This
2860 means that it determines the name of the file to patch for each patch,
2861 and that it examines the leading text before each patch for file names
2862 and prerequisite revision level (@pxref{Making Patches}, for more on
2863 that topic).
2864
2865 @command{patch} uses the following rules to intuit a file name from
2866 the leading text before a patch.  First, @command{patch} takes an
2867 ordered list of candidate file names as follows:
2868
2869 @itemize @bullet
2870 @item
2871 If the header is that of a context diff, @command{patch} takes the old
2872 and new file names in the header.  A name is ignored if it does not
2873 have enough slashes to satisfy the @option{-p@var{num}} or
2874 @option{--strip=@var{num}} option.  The name @file{/dev/null} is also
2875 ignored.
2876
2877 @item
2878 If there is an @samp{Index:} line in the leading garbage and if either
2879 the old and new names are both absent or if @command{patch} is
2880 conforming to @acronym{POSIX}, @command{patch} takes the name in the
2881 @samp{Index:} line.
2882
2883 @item
2884 For the purpose of the following rules, the candidate file names are
2885 considered to be in the order (old, new, index), regardless of the
2886 order that they appear in the header.
2887 @end itemize
2888
2889 @noindent
2890 Then @command{patch} selects a file name from the candidate list as
2891 follows:
2892
2893 @itemize @bullet
2894 @item
2895 If some of the named files exist, @command{patch} selects the first
2896 name if conforming to @acronym{POSIX}, and the best name otherwise.
2897
2898 @item
2899 If @command{patch} is not ignoring @acronym{RCS}, ClearCase, and @acronym{SCCS}
2900 (@pxref{Revision Control}), and no named files exist but an @acronym{RCS},
2901 ClearCase, or @acronym{SCCS} master is found, @command{patch} selects the
2902 first named file with an @acronym{RCS}, ClearCase, or @acronym{SCCS} master.
2903
2904 @item
2905 If no named files exist, no @acronym{RCS}, ClearCase, or @acronym{SCCS} master
2906 was found, some names are given, @command{patch} is not conforming to
2907 @acronym{POSIX}, and the patch appears to create a file, @command{patch}
2908 selects the best name requiring the creation of the fewest
2909 directories.
2910
2911 @item
2912 If no file name results from the above heuristics, you are asked for
2913 the name of the file to patch, and @command{patch} selects that name.
2914 @end itemize
2915
2916 To determine the @dfn{best} of a nonempty list of file names,
2917 @command{patch} first takes all the names with the fewest path name
2918 components; of those, it then takes all the names with the shortest
2919 basename; of those, it then takes all the shortest names; finally, it
2920 takes the first remaining name.
2921
2922 @xref{patch and POSIX}, to see whether @command{patch} is conforming
2923 to @acronym{POSIX}.
2924
2925 @node patch Directories
2926 @section Applying Patches in Other Directories
2927 @cindex directories and patch
2928 @cindex patching directories
2929
2930 The @option{--directory=@var{directory}} (@option{-d @var{directory}})
2931 option to @command{patch} makes directory @var{directory} the current
2932 directory for interpreting both file names in the patch file, and file
2933 names given as arguments to other options (such as @option{-B} and
2934 @option{-o}).  For example, while in a mail reading program, you can patch
2935 a file in the @file{/usr/src/emacs} directory directly from a message
2936 containing the patch like this:
2937
2938 @example
2939 | patch -d /usr/src/emacs
2940 @end example
2941
2942 Sometimes the file names given in a patch contain leading directories,
2943 but you keep your files in a directory different from the one given in
2944 the patch.  In those cases, you can use the
2945 @option{--strip=@var{number}} (@option{-p@var{number}})
2946 option to set the file name strip count to @var{number}.  The strip
2947 count tells @command{patch} how many slashes, along with the directory
2948 names between them, to strip from the front of file names.  A sequence
2949 of one or more adjacent slashes is counted as a single slash.  By
2950 default, @command{patch} strips off all leading directories, leaving
2951 just the base file names.
2952
2953 For example, suppose the file name in the patch file is
2954 @file{/gnu/src/emacs/etc/NEWS}.  Using @option{-p0} gives the
2955 entire file name unmodified, @option{-p1} gives
2956 @file{gnu/src/emacs/etc/NEWS} (no leading slash), @option{-p4} gives
2957 @file{etc/NEWS}, and not specifying @option{-p} at all gives @file{NEWS}.
2958
2959 @command{patch} looks for each file (after any slashes have been stripped)
2960 in the current directory, or if you used the @option{-d @var{directory}}
2961 option, in that directory.
2962
2963 @node Backups
2964 @section Backup Files
2965 @cindex backup file strategy
2966
2967 Normally, @command{patch} creates a backup file if the patch does not
2968 exactly match the original input file, because in that case the
2969 original data might not be recovered if you undo the patch with
2970 @samp{patch -R} (@pxref{Reversed Patches}).  However, when conforming
2971 to @acronym{POSIX}, @command{patch} does not create backup files by
2972 default.  @xref{patch and POSIX}.
2973
2974 The @option{--backup} (@option{-b}) option causes @command{patch} to
2975 make a backup file regardless of whether the patch matches the
2976 original input.  The @option{--backup-if-mismatch} option causes
2977 @command{patch} to create backup files for mismatches files; this is
2978 the default when not conforming to @acronym{POSIX}.  The
2979 @option{--no-backup-if-mismatch} option causes @command{patch} to not
2980 create backup files, even for mismatched patches; this is the default
2981 when conforming to @acronym{POSIX}.
2982
2983 When backing up a file that does not exist, an empty, unreadable
2984 backup file is created as a placeholder to represent the nonexistent
2985 file.
2986
2987 @node Backup Names
2988 @section Backup File Names
2989 @cindex backup file names
2990
2991 Normally, @command{patch} renames an original input file into a backup
2992 file by appending to its name the extension @samp{.orig}, or @samp{~}
2993 if using @samp{.orig} would make the backup file name too
2994 long.@footnote{A coding error in @acronym{GNU} @command{patch} version
2995 2.5.4 causes it to always use @samp{~}, but this should be fixed in
2996 the next release.}  The @option{-z @var{backup-suffix}} or
2997 @option{--suffix=@var{backup-suffix}} option causes @command{patch} to
2998 use @var{backup-suffix} as the backup extension instead.
2999
3000 @vindex SIMPLE_BACKUP_SUFFIX
3001 Alternately, you can specify the extension for backup files with the
3002 @env{SIMPLE_BACKUP_SUFFIX} environment variable, which the options
3003 override.
3004
3005 @command{patch} can also create numbered backup files the way
3006 @acronym{GNU} Emacs does.  With this method, instead of having a
3007 single backup of each file, @command{patch} makes a new backup file
3008 name each time it patches a file.  For example, the backups of a file
3009 named @file{sink} would be called, successively, @file{sink.~1~},
3010 @file{sink.~2~}, @file{sink.~3~}, etc.
3011
3012 @vindex PATCH_VERSION_CONTROL
3013 @vindex VERSION_CONTROL
3014 The @option{-V @var{backup-style}} or
3015 @option{--version-control=@var{backup-style}} option takes as an
3016 argument a method for creating backup file names.  You can alternately
3017 control the type of backups that @command{patch} makes with the
3018 @env{PATCH_VERSION_CONTROL} environment variable, which the
3019 @option{-V} option overrides.  If @env{PATCH_VERSION_CONTROL} is not
3020 set, the @env{VERSION_CONTROL} environment variable is used instead.
3021 Please note that these options and variables control backup file
3022 names; they do not affect the choice of revision control system
3023 (@pxref{Revision Control}).
3024
3025 The values of these environment variables and the argument to the
3026 @option{-V} option are like the @acronym{GNU} Emacs @code{version-control}
3027 variable (@pxref{Backup Names, , , emacs, The @acronym{GNU} Emacs Manual},
3028 for more information on backup versions in Emacs).  They also
3029 recognize synonyms that are more descriptive.  The valid values are
3030 listed below; unique abbreviations are acceptable.
3031
3032 @table @option
3033 @item t
3034 @itemx numbered
3035 Always make numbered backups.
3036
3037 @item nil
3038 @itemx existing
3039 Make numbered backups of files that already have them, simple backups of
3040 the others.  This is the default.
3041
3042 @item never
3043 @itemx simple
3044 Always make simple backups.
3045 @end table
3046
3047 You can also tell @command{patch} to prepend a prefix, such as a
3048 directory name, to produce backup file names.  The
3049 @option{--prefix=@var{prefix}} (@option{-B @var{prefix}}) option makes backup
3050 files by prepending @var{prefix} to them.  The
3051 @option{--basename-prefix=@var{prefix}} (@option{-Y @var{prefix}}) prepends
3052 @var{prefix} to the last file name component of backup file names
3053 instead; for example, @option{-Y ~} causes the backup name for
3054 @file{dir/file.c} to be @file{dir/~file.c}.  If you use either of
3055 these prefix options, the suffix-based options are ignored.
3056
3057 If you specify the output file with the @option{-o} option, that file is
3058 the one that is backed up, not the input file.
3059
3060 Options that affect the names of backup files do not affect whether
3061 backups are made.  For example, if you specify the
3062 @option{--no-backup-if-mismatch} option, none of the options described
3063 in this section have any affect, because no backups are made.
3064
3065 @node Reject Names
3066 @section Reject File Names
3067 @cindex reject file names
3068
3069 The names for reject files (files containing patches that
3070 @command{patch} could not find a place to apply) are normally the name
3071 of the output file with @samp{.rej} appended (or @samp{#} if using
3072 @samp{.rej} would make the backup file name too long).
3073
3074 Alternatively, you can tell @command{patch} to place all of the rejected
3075 patches in a single file.  The @option{-r @var{reject-file}} or
3076 @option{--reject-file=@var{reject-file}} option uses @var{reject-file} as
3077 the reject file name.
3078
3079 @node patch Messages
3080 @section Messages and Questions from @command{patch}
3081 @cindex @command{patch} messages and questions
3082 @cindex diagnostics from @command{patch}
3083 @cindex messages from @command{patch}
3084
3085 @command{patch} can produce a variety of messages, especially if it
3086 has trouble decoding its input.  In a few situations where it's not
3087 sure how to proceed, @command{patch} normally prompts you for more
3088 information from the keyboard.  There are options to produce more or
3089 fewer messages, to have it not ask for keyboard input, and to
3090 affect the way that file names are quoted in messages.
3091
3092 @menu
3093 * More or Fewer Messages::    Controlling the verbosity of @command{patch}.
3094 * patch and Keyboard Input::  Inhibiting keyboard input.
3095 * patch Quoting Style::       Quoting file names in diagnostics.
3096 @end menu
3097
3098 @command{patch} exits with status 0 if all hunks are applied successfully,
3099 1 if some hunks cannot be applied, and 2 if there is more serious trouble.
3100 When applying a set of patches in a loop, you should check the
3101 exit status, so you don't apply a later patch to a partially patched
3102 file.
3103
3104 @node More or Fewer Messages
3105 @subsection Controlling the Verbosity of @command{patch}
3106 @cindex verbose messages from @command{patch}
3107 @cindex inhibit messages from @command{patch}
3108
3109 You can cause @command{patch} to produce more messages by using the
3110 @option{--verbose} option.  For example, when you give this option,
3111 the message @samp{Hmm...} indicates that @command{patch} is reading text in
3112 the patch file, attempting to determine whether there is a patch in that
3113 text, and if so, what kind of patch it is.
3114
3115 You can inhibit all terminal output from @command{patch}, unless an error
3116 occurs, by using the @option{-s}, @option{--quiet}, or @option{--silent}
3117 option.
3118
3119 @node patch and Keyboard Input
3120 @subsection Inhibiting Keyboard Input
3121 @cindex keyboard input to @command{patch}
3122
3123 There are two ways you can prevent @command{patch} from asking you any
3124 questions.  The @option{--force} (@option{-f}) option assumes that you know
3125 what you are doing.  It causes @command{patch} to do the following:
3126
3127 @itemize @bullet
3128 @item
3129 Skip patches that do not contain file names in their headers.
3130
3131 @item
3132 Patch files even though they have the wrong version for the
3133 @samp{Prereq:} line in the patch;
3134
3135 @item
3136 Assume that patches are not reversed even if they look like they are.
3137 @end itemize
3138
3139 @noindent
3140 The @option{--batch} (@option{-t}) option is similar to @option{-f}, in that
3141 it suppresses questions, but it makes somewhat different assumptions:
3142
3143 @itemize @bullet
3144 @item
3145 Skip patches that do not contain file names in their headers
3146 (the same as @option{-f}).
3147
3148 @item
3149 Skip patches for which the file has the wrong version for the
3150 @samp{Prereq:} line in the patch;
3151
3152 @item
3153 Assume that patches are reversed if they look like they are.
3154 @end itemize
3155
3156 @node patch Quoting Style
3157 @subsection @command{patch} Quoting Style
3158 @cindex quoting style
3159
3160 When @command{patch} outputs a file name in a diagnostic message, it
3161 can format the name in any of several ways.  This can be useful to
3162 output file names unambiguously, even if they contain punctuation or
3163 special characters like newlines.  The
3164 @option{--quoting-style=@var{word}} option controls how names are
3165 output.  The @var{word} should be one of the following:
3166
3167 @table @samp
3168 @item literal
3169 Output names as-is.
3170 @item shell
3171 Quote names for the shell if they contain shell metacharacters or would
3172 cause ambiguous output.
3173 @item shell-always
3174 Quote names for the shell, even if they would normally not require quoting.
3175 @item c
3176 Quote names as for a C language string.
3177 @item escape
3178 Quote as with @samp{c} except omit the surrounding double-quote
3179 characters.
3180 @c The following are not yet implemented in patch 2.5.4.
3181 @c @item clocale
3182 @c Quote as with @samp{c} except use quotation marks appropriate for the
3183 @c locale.
3184 @c @item locale
3185 @c @c Use @t instead of @samp to avoid duplicate quoting in some output styles.
3186 @c Like @samp{clocale}, but quote @t{`like this'} instead of @t{"like
3187 @c this"} in the default C locale.  This looks nicer on many displays.
3188 @end table
3189
3190 @vindex QUOTING_STYLE
3191 You can specify the default value of the @option{--quoting-style}
3192 option with the environment variable @env{QUOTING_STYLE}.  If that
3193 environment variable is not set, the default value is @samp{shell},
3194 but this default may change in a future version of @command{patch}.
3195
3196 @node patch and POSIX
3197 @section @command{patch} and the @acronym{POSIX} Standard
3198 @cindex @acronym{POSIX}
3199
3200 @vindex POSIXLY_CORRECT
3201 If you specify the @option{--posix} option, or set the
3202 @env{POSIXLY_CORRECT} environment variable, @command{patch} conforms
3203 more strictly to the @acronym{POSIX} standard, as follows:
3204
3205 @itemize @bullet
3206 @item
3207 Take the first existing file from the list (old, new, index)
3208 when intuiting file names from diff headers.  @xref{Multiple Patches}.
3209
3210 @item
3211 Do not remove files that are removed by a diff.
3212 @xref{Creating and Removing}.
3213
3214 @item
3215 Do not ask whether to get files from @acronym{RCS}, ClearCase, or
3216 @acronym{SCCS}.  @xref{Revision Control}.
3217
3218 @item
3219 Require that all options precede the files in the command line.
3220
3221 @item
3222 Do not backup files, even when there is a mismatch.  @xref{Backups}.
3223
3224 @end itemize
3225
3226 @node patch and Tradition
3227 @section @acronym{GNU} @command{patch} and Traditional @command{patch}
3228 @cindex traditional @command{patch}
3229
3230 The current version of @acronym{GNU} @command{patch} normally follows the
3231 @acronym{POSIX} standard.  @xref{patch and POSIX}, for the few exceptions
3232 to this general rule.
3233
3234 Unfortunately, @acronym{POSIX} redefined the behavior of @command{patch} in
3235 several important ways.  You should be aware of the following
3236 differences if you must interoperate with traditional @command{patch},
3237 or with @acronym{GNU} @command{patch} version 2.1 and earlier.
3238
3239 @itemize @bullet
3240 @item
3241 In traditional @command{patch}, the @option{-p} option's operand was
3242 optional, and a bare @option{-p} was equivalent to @option{-p0}.  The
3243 @option{-p} option now requires an operand, and @option{-p@ 0} is now
3244 equivalent to @option{-p0}.  For maximum compatibility, use options
3245 like @option{-p0} and @option{-p1}.
3246
3247 Also, traditional @command{patch} simply counted slashes when
3248 stripping path prefixes; @command{patch} now counts pathname
3249 components.  That is, a sequence of one or more adjacent slashes now
3250 counts as a single slash.  For maximum portability, avoid sending
3251 patches containing @file{//} in file names.
3252
3253 @item
3254 In traditional @command{patch}, backups were enabled by default.  This
3255 behavior is now enabled with the @option{--backup} (@option{-b})
3256 option.
3257
3258 Conversely, in @acronym{POSIX} @command{patch}, backups are never made,
3259 even when there is a mismatch.  In @acronym{GNU} @command{patch}, this
3260 behavior is enabled with the @option{--no-backup-if-mismatch} option,
3261 or by conforming to @acronym{POSIX}.
3262
3263 The @option{-b@ @var{suffix}} option of traditional @command{patch} is
3264 equivalent to the @samp{-b -z@ @var{suffix}} options of @acronym{GNU}
3265 @command{patch}.
3266
3267 @item
3268 Traditional @command{patch} used a complicated (and incompletely
3269 documented) method to intuit the name of the file to be patched from
3270 the patch header.  This method did not conform to @acronym{POSIX}, and had
3271 a few gotchas.  Now @command{patch} uses a different, equally
3272 complicated (but better documented) method that is optionally
3273 @acronym{POSIX}-conforming; we hope it has fewer gotchas.  The two methods
3274 are compatible if the file names in the context diff header and the
3275 @samp{Index:} line are all identical after prefix-stripping.  Your
3276 patch is normally compatible if each header's file names all contain
3277 the same number of slashes.
3278
3279 @item
3280 When traditional @command{patch} asked the user a question, it sent
3281 the question to standard error and looked for an answer from the first
3282 file in the following list that was a terminal: standard error,
3283 standard output, @file{/dev/tty}, and standard input.  Now
3284 @command{patch} sends questions to standard output and gets answers
3285 from @file{/dev/tty}.  Defaults for some answers have been changed so
3286 that @command{patch} never goes into an infinite loop when using
3287 default answers.
3288
3289 @item
3290 Traditional @command{patch} exited with a status value that counted
3291 the number of bad hunks, or with status 1 if there was real trouble.
3292 Now @command{patch} exits with status 1 if some hunks failed, or with
3293 2 if there was real trouble.
3294
3295 @item
3296 Limit yourself to the following options when sending instructions
3297 meant to be executed by anyone running @acronym{GNU} @command{patch},
3298 traditional @command{patch}, or a @command{patch} that conforms to
3299 @acronym{POSIX}.  Spaces are significant in the following list, and
3300 operands are required.
3301
3302 @example
3303 @option{-c}
3304 @option{-d @var{dir}}
3305 @option{-D @var{define}}
3306 @option{-e}
3307 @option{-l}
3308 @option{-n}
3309 @option{-N}
3310 @option{-o @var{outfile}}
3311 @option{-p@var{num}}
3312 @option{-R}
3313 @option{-r @var{rejectfile}}
3314 @end example
3315
3316 @end itemize
3317
3318 @node Making Patches
3319 @chapter Tips for Making and Using Patches
3320
3321 Use some common sense when making and using patches.  For example,
3322 when sending bug fixes to a program's maintainer, send several small
3323 patches, one per independent subject, instead of one large,
3324 harder-to-digest patch that covers all the subjects.
3325
3326 Here are some other things you should keep in mind if you are going to
3327 distribute patches for updating a software package.
3328
3329 @menu
3330 * Tips for Patch Producers::    Advice for making patches.
3331 * Tips for Patch Consumers::    Advice for using patches.
3332 * Avoiding Common Mistakes::    Avoiding common mistakes when using @command{patch}.
3333 * Generating Smaller Patches::  How to generate smaller patches.
3334 @end menu
3335
3336 @node Tips for Patch Producers
3337 @section Tips for Patch Producers
3338 @cindex patch producer tips
3339
3340 To create a patch that changes an older version of a package into a
3341 newer version, first make a copy of the older and newer versions in
3342 adjacent subdirectories.  It is common to do that by unpacking
3343 @command{tar} archives of the two versions.
3344
3345 To generate the patch, use the command @samp{diff -Naur @var{old}
3346 @var{new}} where @var{old} and @var{new} identify the old and new
3347 directories.  The names @var{old} and @var{new} should not contain any
3348 slashes.  The @option{-N} option lets the patch create and remove
3349 files; @option{-a} lets the patch update non-text files; @option{-u}
3350 generates useful time stamps and enough context; and @option{-r} lets
3351 the patch update subdirectories.  Here is an example command, using
3352 Bourne shell syntax:
3353
3354 @example
3355 diff -Naur gcc-3.0.3 gcc-3.0.4
3356 @end example
3357
3358 Tell your recipients how to apply the patches.  This should include
3359 which working directory to use, and which @command{patch} options to
3360 use; the option @samp{-p1} is recommended.  Test your procedure by
3361 pretending to be a recipient and applying your patches to a copy of
3362 the original files.
3363
3364 @xref{Avoiding Common Mistakes}, for how to avoid common mistakes when
3365 generating a patch.
3366
3367 @node Tips for Patch Consumers
3368 @section Tips for Patch Consumers
3369 @cindex patch consumer tips
3370
3371 A patch producer should tell recipients how to apply the patches, so
3372 the first rule of thumb for a patch consumer is to follow the
3373 instructions supplied with the patch.
3374
3375 @acronym{GNU} @command{diff} can analyze files with arbitrarily long lines
3376 and files that end in incomplete lines.  However, older versions of
3377 @command{patch} cannot patch such files.  If you are having trouble
3378 applying such patches, try upgrading to a recent version of @acronym{GNU}
3379 @command{patch}.
3380
3381 @node Avoiding Common Mistakes
3382 @section Avoiding Common Mistakes
3383 @cindex common mistakes with patches
3384 @cindex patch, common mistakes
3385
3386 When producing a patch for multiple files, apply @command{diff} to
3387 directories whose names do not have slashes.  This reduces confusion
3388 when the patch consumer specifies the @option{-p@var{number}} option,
3389 since this option can have surprising results when the old and new
3390 file names have different numbers of slashes.  For example, do not
3391 send a patch with a header that looks like this:
3392
3393 @example
3394 diff -Naur v2.0.29/prog/README prog/README
3395 --- v2.0.29/prog/README 2002-03-10 23:30:39.942229878 -0800
3396 +++ prog/README 2002-03-17 20:49:32.442260588 -0800
3397 @end example
3398
3399 @noindent
3400 because the two file names have different numbers of slashes, and
3401 different versions of @command{patch} interpret the file names
3402 differently.  To avoid confusion, send output that looks like this
3403 instead:
3404
3405 @example
3406 diff -Naur v2.0.29/prog/README v2.0.30/prog/README
3407 --- v2.0.29/prog/README 2002-03-10 23:30:39.942229878 -0800
3408 +++ v2.0.30/prog/README 2002-03-17 20:49:32.442260588 -0800
3409 @end example
3410
3411 Make sure you have specified the file names correctly, either in a
3412 context diff header or with an @samp{Index:} line.  Take care to not send out
3413 reversed patches, since these make people wonder whether they have
3414 already applied the patch.
3415
3416 Avoid sending patches that compare backup file names like
3417 @file{README.orig} or @file{README~}, since this might confuse
3418 @command{patch} into patching a backup file instead of the real file.
3419 Instead, send patches that compare the same base file names in
3420 different directories, e.g.@: @file{old/README} and @file{new/README}.
3421
3422 To save people from partially applying a patch before other patches that
3423 should have gone before it, you can make the first patch in the patch
3424 file update a file with a name like @file{patchlevel.h} or
3425 @file{version.c}, which contains a patch level or version number.  If
3426 the input file contains the wrong version number, @command{patch} will
3427 complain immediately.
3428
3429 An even clearer way to prevent this problem is to put a @samp{Prereq:}
3430 line before the patch.  If the leading text in the patch file contains a
3431 line that starts with @samp{Prereq:}, @command{patch} takes the next word
3432 from that line (normally a version number) and checks whether the next
3433 input file contains that word, preceded and followed by either
3434 white space or a newline.  If not, @command{patch} prompts you for
3435 confirmation before proceeding.  This makes it difficult to accidentally
3436 apply patches in the wrong order.
3437
3438 @node Generating Smaller Patches
3439 @section Generating Smaller Patches
3440 @cindex patches, shrinking
3441
3442 The simplest way to generate a patch is to use @samp{diff -Naur}
3443 (@pxref{Tips for Patch Producers}), but you might be able to reduce
3444 the size of the patch by renaming or removing some files before making
3445 the patch.  If the older version of the package contains any files
3446 that the newer version does not, or if any files have been renamed
3447 between the two versions, make a list of @command{rm} and @command{mv}
3448 commands for the user to execute in the old version directory before
3449 applying the patch.  Then run those commands yourself in the scratch
3450 directory.
3451
3452 If there are any files that you don't need to include in the patch
3453 because they can easily be rebuilt from other files (for example,
3454 @file{TAGS} and output from @command{yacc} and @command{makeinfo}),
3455 exclude them from the patch by giving @command{diff} the @option{-x
3456 @var{pattern}} option (@pxref{Comparing Directories}).  If you want
3457 your patch to modify a derived file because your recipients lack tools
3458 to build it, make sure that the patch for the derived file follows any
3459 patches for files that it depends on, so that the recipients' time
3460 stamps will not confuse @command{make}.
3461
3462 Now you can create the patch using @samp{diff -Naur}.  Make sure to
3463 specify the scratch directory first and the newer directory second.
3464
3465 Add to the top of the patch a note telling the user any @command{rm} and
3466 @command{mv} commands to run before applying the patch.  Then you can
3467 remove the scratch directory.
3468
3469 You can also shrink the patch size by using fewer lines of context,
3470 but bear in mind that @command{patch} typically needs at least two
3471 lines for proper operation when patches do not exactly match the input
3472 files.
3473
3474 @node Invoking cmp
3475 @chapter Invoking @command{cmp}
3476 @cindex invoking @command{cmp}
3477 @cindex @command{cmp} invocation
3478
3479 The @command{cmp} command compares two files, and if they differ,
3480 tells the first byte and line number where they differ or reports
3481 that one file is a prefix of the other.  Bytes and
3482 lines are numbered starting with 1.  The arguments of @command{cmp}
3483 are as follows:
3484
3485 @example
3486 cmp @var{options}@dots{} @var{from-file} @r{[}@var{to-file} @r{[}@var{from-skip} @r{[}@var{to-skip}@r{]}@r{]}@r{]}
3487 @end example
3488
3489 The file name @file{-} is always the standard input.  @command{cmp}
3490 also uses the standard input if one file name is omitted.  The
3491 @var{from-skip} and @var{to-skip} operands specify how many bytes to
3492 ignore at the start of each file; they are equivalent to the
3493 @option{--ignore-initial=@var{from-skip}:@var{to-skip}} option.
3494
3495 By default, @command{cmp} outputs nothing if the two files have the
3496 same contents.  If one file is a prefix of the other, @command{cmp}
3497 prints to standard error a message of the following form:
3498
3499 @example
3500 cmp: EOF on @var{shorter-file}
3501 @end example
3502
3503 Otherwise, @command{cmp} prints to standard output a message of the
3504 following form:
3505
3506 @example
3507 @var{from-file} @var{to-file} differ: char @var{byte-number}, line @var{line-number}
3508 @end example
3509
3510 The message formats can differ outside the @acronym{POSIX} locale.
3511 Also, @acronym{POSIX} allows the @acronym{EOF} message to be followed
3512 by a blank and some additional information.
3513
3514 An exit status of 0 means no differences were found, 1 means some
3515 differences were found, and 2 means trouble.
3516
3517 @menu
3518 * cmp Options:: Summary of options to @command{cmp}.
3519 @end menu
3520
3521 @node cmp Options
3522 @section Options to @command{cmp}
3523 @cindex @command{cmp} options
3524 @cindex options for @command{cmp}
3525
3526 Below is a summary of all of the options that @acronym{GNU}
3527 @command{cmp} accepts.  Most options have two equivalent names, one of
3528 which is a single letter preceded by @samp{-}, and the other of which
3529 is a long name preceded by @samp{--}.  Multiple single letter options
3530 (unless they take an argument) can be combined into a single command
3531 line word: @option{-bl} is equivalent to @option{-b -l}.
3532
3533 @table @option
3534 @item -b
3535 @itemx --print-bytes
3536 Print the differing bytes.  Display control bytes as a
3537 @samp{^} followed by a letter of the alphabet and precede bytes
3538 that have the high bit set with @samp{M-} (which stands for ``meta'').
3539
3540 @item --help
3541 Output a summary of usage and then exit.
3542
3543 @item -i @var{skip}
3544 @itemx --ignore-initial=@var{skip}
3545 Ignore any differences in the first @var{skip} bytes of the input
3546 files.  Treat files with fewer than @var{skip} bytes as if they are
3547 empty.  If @var{skip} is of the form
3548 @option{@var{from-skip}:@var{to-skip}}, skip the first @var{from-skip}
3549 bytes of the first input file and the first @var{to-skip} bytes of the
3550 second.
3551
3552 @item -l
3553 @itemx --verbose
3554 Output the (decimal) byte numbers and (octal) values of all differing bytes,
3555 instead of the default standard output.
3556 Also, output the @acronym{EOF} message if one file is shorter than the other.
3557
3558 @item -n @var{count}
3559 @itemx --bytes=@var{count}
3560 Compare at most @var{count} input bytes.
3561
3562 @item -s
3563 @itemx --quiet
3564 @itemx --silent
3565 Do not print anything; only return an exit status indicating whether
3566 the files differ.
3567
3568 @item -v
3569 @itemx --version
3570 Output version information and then exit.
3571 @end table
3572
3573 In the above table, operands that are byte counts are normally
3574 decimal, but may be preceded by @samp{0} for octal and @samp{0x} for
3575 hexadecimal.
3576
3577 A byte count can be followed by a suffix to specify a multiple of that
3578 count; in this case an omitted integer is understood to be 1.  A bare
3579 size letter, or one followed by @samp{iB}, specifies a multiple using
3580 powers of 1024.  A size letter followed by @samp{B} specifies powers
3581 of 1000 instead.  For example, @option{-n 4M} and @option{-n 4MiB} are
3582 equivalent to @option{-n 4194304}, whereas @option{-n 4MB} is
3583 equivalent to @option{-n 4000000}.  This notation is upward compatible
3584 with the @uref{http://www.bipm.fr/enus/3_SI/si-prefixes.html, SI
3585 prefixes} for decimal multiples and with the
3586 @uref{http://physics.nist.gov/cuu/Units/binary.html, IEC 60027-2
3587 prefixes for binary multiples}.
3588
3589 The following suffixes are defined.  Large sizes like @code{1Y} may be
3590 rejected by your computer due to limitations of its arithmetic.
3591
3592 @table @samp
3593 @item kB
3594 @cindex kilobyte, definition of
3595 kilobyte: @math{10^3 = 1000}.
3596 @item k
3597 @itemx K
3598 @itemx KiB
3599 @cindex kibibyte, definition of
3600 kibibyte: @math{2^10 = 1024}.  @samp{K} is special: the SI prefix is
3601 @samp{k} and the IEC 60027-2 prefix is @samp{Ki}, but tradition and
3602 @acronym{POSIX} use @samp{k} to mean @samp{KiB}.
3603 @item MB
3604 @cindex megabyte, definition of
3605 megabyte: @math{10^6 = 1,000,000}.
3606 @item M
3607 @itemx MiB
3608 @cindex mebibyte, definition of
3609 mebibyte: @math{2^20 = 1,048,576}.
3610 @item GB
3611 @cindex gigabyte, definition of
3612 gigabyte: @math{10^9 = 1,000,000,000}.
3613 @item G
3614 @itemx GiB
3615 @cindex gibibyte, definition of
3616 gibibyte: @math{2^30 = 1,073,741,824}.
3617 @item TB
3618 @cindex terabyte, definition of
3619 terabyte:  @math{10^12 = 1,000,000,000,000}.
3620 @item T
3621 @itemx TiB
3622 @cindex tebibyte, definition of
3623 tebibyte: @math{2^40 = 1,099,511,627,776}.
3624 @item PB
3625 @cindex petabyte, definition of
3626 petabyte: @math{10^15 = 1,000,000,000,000,000}.
3627 @item P
3628 @itemx PiB
3629 @cindex pebibyte, definition of
3630 pebibyte: @math{2^50 = 1,125,899,906,842,624}.
3631 @item EB
3632 @cindex exabyte, definition of
3633 exabyte: @math{10^18 = 1,000,000,000,000,000,000}.
3634 @item E
3635 @itemx EiB
3636 @cindex exbibyte, definition of
3637 exbibyte: @math{2^60 = 1,152,921,504,606,846,976}.
3638 @item ZB
3639 @cindex zettabyte, definition of
3640 zettabyte: @math{10^21 = 1,000,000,000,000,000,000,000}
3641 @item Z
3642 @itemx ZiB
3643 @math{2^70 = 1,180,591,620,717,411,303,424}.
3644 (@samp{Zi} is a GNU extension to IEC 60027-2.)
3645 @item YB
3646 @cindex yottabyte, definition of
3647 yottabyte: @math{10^24 = 1,000,000,000,000,000,000,000,000}.
3648 @item Y
3649 @itemx YiB
3650 @math{2^80 = 1,208,925,819,614,629,174,706,176}.
3651 (@samp{Yi} is a GNU extension to IEC 60027-2.)
3652 @end table
3653
3654 @node Invoking diff
3655 @chapter Invoking @command{diff}
3656 @cindex invoking @command{diff}
3657 @cindex @command{diff} invocation
3658
3659 The format for running the @command{diff} command is:
3660
3661 @example
3662 diff @var{options}@dots{} @var{files}@dots{}
3663 @end example
3664
3665 In the simplest case, two file names @var{from-file} and
3666 @var{to-file} are given, and @command{diff} compares the contents of
3667 @var{from-file} and @var{to-file}.  A file name of @file{-} stands for
3668 text read from the standard input.  As a special case, @samp{diff - -}
3669 compares a copy of standard input to itself.
3670
3671 If one file is a directory and the other is not, @command{diff} compares
3672 the file in the directory whose name is that of the non-directory.
3673 The non-directory file must not be @file{-}.
3674
3675 If two file names are given and both are directories,
3676 @command{diff} compares corresponding files in both directories, in
3677 alphabetical order; this comparison is not recursive unless the
3678 @option{--recursive} (@option{-r}) option is given.  @command{diff} never
3679 compares the actual contents of a directory as if it were a file.  The
3680 file that is fully specified may not be standard input, because standard
3681 input is nameless and the notion of ``file with the same name'' does not
3682 apply.
3683
3684 If the @option{--from-file=@var{file}} option is given, the number of
3685 file names is arbitrary, and @var{file} is compared to each named file.
3686 Similarly, if the @option{--to-file=@var{file}} option is given, each
3687 named file is compared to @var{file}.
3688
3689 @command{diff} options begin with @samp{-}, so normally file names
3690 may not begin with @samp{-}.  However, @option{--} as an
3691 argument by itself treats the remaining arguments as file names even if
3692 they begin with @samp{-}.
3693
3694 An exit status of 0 means no differences were found, 1 means some
3695 differences were found, and 2 means trouble.  Normally, differing
3696 binary files count as trouble, but this can be altered by using the
3697 @option{--text} (@option{-a}) option, or the @option{-q} or
3698 @option{--brief} option.
3699
3700 @menu
3701 * diff Options:: Summary of options to @command{diff}.
3702 @end menu
3703
3704 @node diff Options
3705 @section Options to @command{diff}
3706 @cindex @command{diff} options
3707 @cindex options for @command{diff}
3708
3709 Below is a summary of all of the options that @acronym{GNU}
3710 @command{diff} accepts.  Most options have two equivalent names, one
3711 of which is a single letter preceded by @samp{-}, and the other of
3712 which is a long name preceded by @samp{--}.  Multiple single letter
3713 options (unless they take an argument) can be combined into a single
3714 command line word: @option{-ac} is equivalent to @option{-a -c}.  Long
3715 named options can be abbreviated to any unique prefix of their name.
3716 Brackets ([ and ]) indicate that an option takes an optional argument.
3717
3718 @table @option
3719 @item -a
3720 @itemx --text
3721 Treat all files as text and compare them line-by-line, even if they
3722 do not seem to be text.  @xref{Binary}.
3723
3724 @item -b
3725 @itemx --ignore-space-change
3726 Ignore changes in amount of white space.  @xref{White Space}.
3727
3728 @item -B
3729 @itemx --ignore-blank-lines
3730 Ignore changes that just insert or delete blank lines.  @xref{Blank
3731 Lines}.
3732
3733 @item --binary
3734 Read and write data in binary mode.  @xref{Binary}.
3735
3736 @item -c
3737 Use the context output format, showing three lines of context.
3738 @xref{Context Format}.
3739
3740 @item -C @var{lines}
3741 @itemx --context@r{[}=@var{lines}@r{]}
3742 Use the context output format, showing @var{lines} (an integer) lines of
3743 context, or three if @var{lines} is not given.  @xref{Context Format}.
3744 For proper operation, @command{patch} typically needs at least two lines of
3745 context.
3746
3747 For compatibility @command{diff} also supports an obsolete option
3748 syntax @option{-@var{lines}} that has effect when combined with
3749 @option{-c}, @option{-p}, or @option{-u}.  New scripts should use
3750 @option{-U @var{lines}} (@option{-C @var{lines}}) instead.
3751
3752 @item --changed-group-format=@var{format}
3753 Use @var{format} to output a line group containing differing lines from
3754 both files in if-then-else format.  @xref{Line Group Formats}.
3755
3756 @item -d
3757 @itemx --minimal
3758 Change the algorithm perhaps find a smaller set of changes.  This makes
3759 @command{diff} slower (sometimes much slower).  @xref{diff Performance}.
3760
3761 @item -D @var{name}
3762 @itemx --ifdef=@var{name}
3763 Make merged @samp{#ifdef} format output, conditional on the preprocessor
3764 macro @var{name}.  @xref{If-then-else}.
3765
3766 @item -e
3767 @itemx --ed
3768 Make output that is a valid @command{ed} script.  @xref{ed Scripts}.
3769
3770 @item -E
3771 @itemx --ignore-tab-expansion
3772 Ignore changes due to tab expansion.
3773 @xref{White Space}.
3774
3775 @item -f
3776 @itemx --forward-ed
3777 Make output that looks vaguely like an @command{ed} script but has changes
3778 in the order they appear in the file.  @xref{Forward ed}.
3779
3780 @item -F @var{regexp}
3781 @itemx --show-function-line=@var{regexp}
3782 In context and unified format, for each hunk of differences, show some
3783 of the last preceding line that matches @var{regexp}.  @xref{Specified
3784 Headings}.
3785
3786 @item --from-file=@var{file}
3787 Compare @var{file} to each operand; @var{file} may be a directory.
3788
3789 @item --help
3790 Output a summary of usage and then exit.
3791
3792 @item --horizon-lines=@var{lines}
3793 Do not discard the last @var{lines} lines of the common prefix
3794 and the first @var{lines} lines of the common suffix.
3795 @xref{diff Performance}.
3796
3797 @item -i
3798 @itemx --ignore-case
3799 Ignore changes in case; consider upper- and lower-case letters
3800 equivalent.  @xref{Case Folding}.
3801
3802 @item -I @var{regexp}
3803 @itemx --ignore-matching-lines=@var{regexp}
3804 Ignore changes that just insert or delete lines that match @var{regexp}.
3805 @xref{Specified Lines}.
3806
3807 @item --ignore-file-name-case
3808 Ignore case when comparing file names.  For example, recursive
3809 comparison of @file{d} to @file{e} might compare the contents of
3810 @file{d/Init} and @file{e/inIt}.  At the top level, @samp{diff d inIt}
3811 might compare the contents of @file{d/Init} and @file{inIt}.
3812 @xref{Comparing Directories}.
3813
3814 @item -l
3815 @itemx --paginate
3816 Pass the output through @command{pr} to paginate it.  @xref{Pagination}.
3817
3818 @item -L @var{label}
3819 @itemx --label=@var{label}
3820 Use @var{label} instead of the file name in the context format
3821 (@pxref{Context Format}) and unified format (@pxref{Unified Format})
3822 headers.  @xref{RCS}.
3823
3824 @item --left-column
3825 Print only the left column of two common lines in side by side format.
3826 @xref{Side by Side Format}.
3827
3828 @item --line-format=@var{format}
3829 Use @var{format} to output all input lines in if-then-else format.
3830 @xref{Line Formats}.
3831
3832 @item -n
3833 @itemx --rcs
3834 Output @acronym{RCS}-format diffs; like @option{-f} except that each command
3835 specifies the number of lines affected.  @xref{RCS}.
3836
3837 @item -N
3838 @itemx --new-file
3839 In directory comparison, if a file is found in only one directory,
3840 treat it as present but empty in the other directory.  @xref{Comparing
3841 Directories}.
3842
3843 @item --new-group-format=@var{format}
3844 Use @var{format} to output a group of lines taken from just the second
3845 file in if-then-else format.  @xref{Line Group Formats}.
3846
3847 @item --new-line-format=@var{format}
3848 Use @var{format} to output a line taken from just the second file in
3849 if-then-else format.  @xref{Line Formats}.
3850
3851 @item --old-group-format=@var{format}
3852 Use @var{format} to output a group of lines taken from just the first
3853 file in if-then-else format.  @xref{Line Group Formats}.
3854
3855 @item --old-line-format=@var{format}
3856 Use @var{format} to output a line taken from just the first file in
3857 if-then-else format.  @xref{Line Formats}.
3858
3859 @item -p
3860 @itemx --show-c-function
3861 Show which C function each change is in.  @xref{C Function Headings}.
3862
3863 @item -q
3864 @itemx --brief
3865 Report only whether the files differ, not the details of the
3866 differences.  @xref{Brief}.
3867
3868 @item -r
3869 @itemx --recursive
3870 When comparing directories, recursively compare any subdirectories
3871 found.  @xref{Comparing Directories}.
3872
3873 @item -s
3874 @itemx --report-identical-files
3875 Report when two files are the same.  @xref{Comparing Directories}.
3876
3877 @item -S @var{file}
3878 @itemx --starting-file=@var{file}
3879 When comparing directories, start with the file @var{file}.  This is
3880 used for resuming an aborted comparison.  @xref{Comparing Directories}.
3881
3882 @item --speed-large-files
3883 Use heuristics to speed handling of large files that have numerous
3884 scattered small changes.  @xref{diff Performance}.
3885
3886 @item --strip-trailing-cr
3887 Strip any trailing carriage return at the end of an input line.
3888 @xref{Binary}.
3889
3890 @item --suppress-common-lines
3891 Do not print common lines in side by side format.
3892 @xref{Side by Side Format}.
3893
3894 @item -t
3895 @itemx --expand-tabs
3896 Expand tabs to spaces in the output, to preserve the alignment of tabs
3897 in the input files.  @xref{Tabs}.
3898
3899 @item -T
3900 @itemx --initial-tab
3901 Output a tab rather than a space before the text of a line in normal or
3902 context format.  This causes the alignment of tabs in the line to look
3903 normal.  @xref{Tabs}.
3904
3905 @item --tabsize=@var{columns}
3906 Assume that tab stops are set every @var{columns} (default 8) print
3907 columns.  @xref{Tabs}.
3908
3909 @item --suppress-blank-empty
3910 Suppress any blanks before newlines when printing the representation
3911 of an empty line, when outputting normal, context, or unified format.
3912 @xref{Trailing Blanks}.
3913
3914 @item --to-file=@var{file}
3915 Compare each operand to @var{file}; @var{file} may be a directory.
3916
3917 @item -u
3918 Use the unified output format, showing three lines of context.
3919 @xref{Unified Format}.
3920
3921 @item --unchanged-group-format=@var{format}
3922 Use @var{format} to output a group of common lines taken from both files
3923 in if-then-else format.  @xref{Line Group Formats}.
3924
3925 @item --unchanged-line-format=@var{format}
3926 Use @var{format} to output a line common to both files in if-then-else
3927 format.  @xref{Line Formats}.
3928
3929 @item --unidirectional-new-file
3930 When comparing directories, if a file appears only in the second
3931 directory of the two, treat it as present but empty in the other.
3932 @xref{Comparing Directories}.
3933
3934 @item -U @var{lines}
3935 @itemx --unified@r{[}=@var{lines}@r{]}
3936 Use the unified output format, showing @var{lines} (an integer) lines of
3937 context, or three if @var{lines} is not given.  @xref{Unified Format}.
3938 For proper operation, @command{patch} typically needs at least two lines of
3939 context.
3940
3941 On older systems, @command{diff} supports an obsolete option
3942 @option{-@var{lines}} that has effect when combined with @option{-u}.
3943 @acronym{POSIX} 1003.1-2001 (@pxref{Standards conformance}) does not allow
3944 this; use @option{-U @var{lines}} instead.
3945
3946 @item -v
3947 @itemx --version
3948 Output version information and then exit.
3949
3950 @item -w
3951 @itemx --ignore-all-space
3952 Ignore white space when comparing lines.  @xref{White Space}.
3953
3954 @item -W @var{columns}
3955 @itemx --width=@var{columns}
3956 Output at most @var{columns} (default 130) print columns per line in
3957 side by side format.  @xref{Side by Side Format}.
3958
3959 @item -x @var{pattern}
3960 @itemx --exclude=@var{pattern}
3961 When comparing directories, ignore files and subdirectories whose basenames
3962 match @var{pattern}.  @xref{Comparing Directories}.
3963
3964 @item -X @var{file}
3965 @itemx --exclude-from=@var{file}
3966 When comparing directories, ignore files and subdirectories whose basenames
3967 match any pattern contained in @var{file}.  @xref{Comparing Directories}.
3968
3969 @item -y
3970 @itemx --side-by-side
3971 Use the side by side output format.  @xref{Side by Side Format}.
3972
3973 @item -Z
3974 @itemx --ignore-trailing-space
3975 Ignore white space at line end.  @xref{White Space}.
3976 @end table
3977
3978 @node Invoking diff3
3979 @chapter Invoking @command{diff3}
3980 @cindex invoking @command{diff3}
3981 @cindex @command{diff3} invocation
3982
3983 The @command{diff3} command compares three files and outputs descriptions
3984 of their differences.  Its arguments are as follows:
3985
3986 @example
3987 diff3 @var{options}@dots{} @var{mine} @var{older} @var{yours}
3988 @end example
3989
3990 The files to compare are @var{mine}, @var{older}, and @var{yours}.
3991 At most one of these three file names may be @file{-},
3992 which tells @command{diff3} to read the standard input for that file.
3993
3994 An exit status of 0 means @command{diff3} was successful, 1 means some
3995 conflicts were found, and 2 means trouble.
3996
3997 @menu
3998 * diff3 Options:: Summary of options to @command{diff3}.
3999 @end menu
4000
4001 @node diff3 Options
4002 @section Options to @command{diff3}
4003 @cindex @command{diff3} options
4004 @cindex options for @command{diff3}
4005
4006 Below is a summary of all of the options that @acronym{GNU} @command{diff3}
4007 accepts.  Multiple single letter options (unless they take an argument)
4008 can be combined into a single command line argument.
4009
4010 @table @option
4011 @item -a
4012 @itemx --text
4013 Treat all files as text and compare them line-by-line, even if they
4014 do not appear to be text.  @xref{Binary}.
4015
4016 @item -A
4017 @itemx --show-all
4018 Incorporate all unmerged changes from @var{older} to @var{yours} into
4019 @var{mine}, surrounding conflicts with bracket lines.
4020 @xref{Marking Conflicts}.
4021
4022 @item --diff-program=@var{program}
4023 Use the compatible comparison program @var{program} to compare files
4024 instead of @command{diff}.
4025
4026 @item -e
4027 @itemx --ed
4028 Generate an @command{ed} script that incorporates all the changes from
4029 @var{older} to @var{yours} into @var{mine}.  @xref{Which Changes}.
4030
4031 @item -E
4032 @itemx --show-overlap
4033 Like @option{-e}, except bracket lines from overlapping changes' first
4034 and third files.
4035 @xref{Marking Conflicts}.
4036 With @option{-E}, an overlapping change looks like this:
4037
4038 @example
4039 <<<<<<< @var{mine}
4040 @r{lines from @var{mine}}
4041 =======
4042 @r{lines from @var{yours}}
4043 >>>>>>> @var{yours}
4044 @end example
4045
4046 @item --help
4047 Output a summary of usage and then exit.
4048
4049 @item -i
4050 Generate @samp{w} and @samp{q} commands at the end of the @command{ed}
4051 script for System V compatibility.  This option must be combined with
4052 one of the @option{-AeExX3} options, and may not be combined with @option{-m}.
4053 @xref{Saving the Changed File}.
4054
4055 @item --label=@var{label}
4056 Use the label @var{label} for the brackets output by the @option{-A},
4057 @option{-E} and @option{-X} options.  This option may be given up to three
4058 times, one for each input file.  The default labels are the names of
4059 the input files.  Thus @samp{diff3 --label X --label Y --label Z -m A
4060 B C} acts like
4061 @samp{diff3 -m A B C}, except that the output looks like it came from
4062 files named @samp{X}, @samp{Y} and @samp{Z} rather than from files
4063 named @samp{A}, @samp{B} and @samp{C}.  @xref{Marking Conflicts}.
4064
4065 @item -m
4066 @itemx --merge
4067 Apply the edit script to the first file and send the result to standard
4068 output.  Unlike piping the output from @command{diff3} to @command{ed}, this
4069 works even for binary files and incomplete lines.  @option{-A} is assumed
4070 if no edit script option is specified.  @xref{Bypassing ed}.
4071
4072 @item --strip-trailing-cr
4073 Strip any trailing carriage return at the end of an input line.
4074 @xref{Binary}.
4075
4076 @item -T
4077 @itemx --initial-tab
4078 Output a tab rather than two spaces before the text of a line in normal format.
4079 This causes the alignment of tabs in the line to look normal.  @xref{Tabs}.
4080
4081 @item -v
4082 @itemx --version
4083 Output version information and then exit.
4084
4085 @item -x
4086 @itemx --overlap-only
4087 Like @option{-e}, except output only the overlapping changes.
4088 @xref{Which Changes}.
4089
4090 @item -X
4091 Like @option{-E}, except output only the overlapping changes.
4092 In other words, like @option{-x}, except bracket changes as in @option{-E}.
4093 @xref{Marking Conflicts}.
4094
4095 @item -3
4096 @itemx --easy-only
4097 Like @option{-e}, except output only the nonoverlapping changes.
4098 @xref{Which Changes}.
4099 @end table
4100
4101 @node Invoking patch
4102 @chapter Invoking @command{patch}
4103 @cindex invoking @command{patch}
4104 @cindex @command{patch} invocation
4105
4106 Normally @command{patch} is invoked like this:
4107
4108 @example
4109 patch <@var{patchfile}
4110 @end example
4111
4112 The full format for invoking @command{patch} is:
4113
4114 @example
4115 patch @var{options}@dots{} @r{[}@var{origfile} @r{[}@var{patchfile}@r{]}@r{]}
4116 @end example
4117
4118 You can also specify where to read the patch from with the @option{-i
4119 @var{patchfile}} or @option{--input=@var{patchfile}} option.
4120 If you do not specify @var{patchfile}, or if @var{patchfile} is
4121 @file{-}, @command{patch} reads the patch (that is, the @command{diff} output)
4122 from the standard input.
4123
4124 If you do not specify an input file on the command line, @command{patch}
4125 tries to intuit from the @dfn{leading text} (any text in the patch
4126 that comes before the @command{diff} output) which file to edit.
4127 @xref{Multiple Patches}.
4128
4129 By default, @command{patch} replaces the original input file with the
4130 patched version, possibly after renaming the original file into a
4131 backup file (@pxref{Backup Names}, for a description of how
4132 @command{patch} names backup files).  You can also specify where to
4133 put the output with the @option{-o @var{file}} or
4134 @option{--output=@var{file}} option; however, do not use this option
4135 if @var{file} is one of the input files.
4136
4137 @menu
4138 * patch Options::     Summary table of options to @command{patch}.
4139 @end menu
4140
4141 @node patch Options
4142 @section Options to @command{patch}
4143 @cindex @command{patch} options
4144 @cindex options for @command{patch}
4145
4146 Here is a summary of all of the options that @acronym{GNU} @command{patch}
4147 accepts.  @xref{patch and Tradition}, for which of these options are
4148 safe to use in older versions of @command{patch}.
4149
4150 Multiple single-letter options that do not take an argument can be
4151 combined into a single command line argument with only one dash.
4152
4153 @table @option
4154 @item -b
4155 @itemx --backup
4156 Back up the original contents of each file, even if backups would
4157 normally not be made.  @xref{Backups}.
4158
4159 @item -B @var{prefix}
4160 @itemx --prefix=@var{prefix}
4161 Prepend @var{prefix} to backup file names.  @xref{Backup Names}.
4162
4163 @item --backup-if-mismatch
4164 Back up the original contents of each file if the patch does not
4165 exactly match the file.  This is the default behavior when not
4166 conforming to @acronym{POSIX}.  @xref{Backups}.
4167
4168 @item --binary
4169 Read and write all files in binary mode, except for standard output
4170 and @file{/dev/tty}.  This option has no effect on
4171 @acronym{POSIX}-conforming systems like @acronym{GNU}/Linux.  On systems where
4172 this option makes a difference, the patch should be generated by
4173 @samp{diff -a --binary}.  @xref{Binary}.
4174
4175 @item -c
4176 @itemx --context
4177 Interpret the patch file as a context diff.  @xref{patch Input}.
4178
4179 @item -d @var{directory}
4180 @itemx --directory=@var{directory}
4181 Make directory @var{directory} the current directory for interpreting
4182 both file names in the patch file, and file names given as arguments to
4183 other options.  @xref{patch Directories}.
4184
4185 @item -D @var{name}
4186 @itemx --ifdef=@var{name}
4187 Make merged if-then-else output using @var{name}.  @xref{If-then-else}.
4188
4189 @item --dry-run
4190 Print the results of applying the patches without actually changing
4191 any files.  @xref{Dry Runs}.
4192
4193 @item -e
4194 @itemx --ed
4195 Interpret the patch file as an @command{ed} script.  @xref{patch Input}.
4196
4197 @item -E
4198 @itemx --remove-empty-files
4199 Remove output files that are empty after the patches have been applied.
4200 @xref{Creating and Removing}.
4201
4202 @item -f
4203 @itemx --force
4204 Assume that the user knows exactly what he or she is doing, and do not
4205 ask any questions.  @xref{patch Messages}.
4206
4207 @item -F @var{lines}
4208 @itemx --fuzz=@var{lines}
4209 Set the maximum fuzz factor to @var{lines}.  @xref{Inexact}.
4210
4211 @item -g @var{num}
4212 @itemx --get=@var{num}
4213 If @var{num} is positive, get input files from a revision control
4214 system as necessary; if zero, do not get the files; if negative, ask
4215 the user whether to get the files.  @xref{Revision Control}.
4216
4217 @item --help
4218 Output a summary of usage and then exit.
4219
4220 @item -i @var{patchfile}
4221 @itemx --input=@var{patchfile}
4222 Read the patch from @var{patchfile} rather than from standard input.
4223 @xref{patch Options}.
4224
4225 @item -l
4226 @itemx --ignore-white-space
4227 Let any sequence of blanks (spaces or tabs) in the patch file match
4228 any sequence of blanks in the input file.  @xref{Changed White Space}.
4229
4230 @item -n
4231 @itemx --normal
4232 Interpret the patch file as a normal diff.  @xref{patch Input}.
4233
4234 @item -N
4235 @itemx --forward
4236 Ignore patches that @command{patch} thinks are reversed or already applied.
4237 See also @option{-R}.  @xref{Reversed Patches}.
4238
4239 @item --no-backup-if-mismatch
4240 Do not back up the original contents of files.  This is the default
4241 behavior when conforming to @acronym{POSIX}.  @xref{Backups}.
4242
4243 @item -o @var{file}
4244 @itemx --output=@var{file}
4245 Use @var{file} as the output file name.  @xref{patch Options}.
4246
4247 @item -p@var{number}
4248 @itemx --strip=@var{number}
4249 Set the file name strip count to @var{number}.  @xref{patch Directories}.
4250
4251 @item --posix
4252 Conform to @acronym{POSIX}, as if the @env{POSIXLY_CORRECT} environment
4253 variable had been set.  @xref{patch and POSIX}.
4254
4255 @item --quoting-style=@var{word}
4256 Use style @var{word} to quote names in diagnostics, as if the
4257 @env{QUOTING_STYLE} environment variable had been set to @var{word}.
4258 @xref{patch Quoting Style}.
4259
4260 @item -r @var{reject-file}
4261 @itemx --reject-file=@var{reject-file}
4262 Use @var{reject-file} as the reject file name.  @xref{Reject Names}.
4263
4264 @item -R
4265 @itemx --reverse
4266 Assume that this patch was created with the old and new files swapped.
4267 @xref{Reversed Patches}.
4268
4269 @item -s
4270 @itemx --quiet
4271 @itemx --silent
4272 Work silently unless an error occurs.  @xref{patch Messages}.
4273
4274 @item -t
4275 @itemx --batch
4276 Do not ask any questions.  @xref{patch Messages}.
4277
4278 @item -T
4279 @itemx --set-time
4280 Set the modification and access times of patched files from time
4281 stamps given in context diff headers, assuming that the context diff
4282 headers use local time.  @xref{Patching Time Stamps}.
4283
4284 @item -u
4285 @itemx --unified
4286 Interpret the patch file as a unified diff.  @xref{patch Input}.
4287
4288 @item -v
4289 @itemx --version
4290 Output version information and then exit.
4291
4292 @item -V @var{backup-style}
4293 @itemx --version=control=@var{backup-style}
4294 Select the naming convention for backup file names.  @xref{Backup Names}.
4295
4296 @item --verbose
4297 Print more diagnostics than usual.  @xref{patch Messages}.
4298
4299 @item -x @var{number}
4300 @itemx --debug=@var{number}
4301 Set internal debugging flags.  Of interest only to @command{patch}
4302 patchers.
4303
4304 @item -Y @var{prefix}
4305 @itemx --basename-prefix=@var{prefix}
4306 Prepend @var{prefix} to base names of backup files.  @xref{Backup Names}.
4307
4308 @item -z @var{suffix}
4309 @itemx --suffix=@var{suffix}
4310 Use @var{suffix} as the backup extension instead of @samp{.orig} or
4311 @samp{~}.  @xref{Backup Names}.
4312
4313 @item -Z
4314 @itemx --set-utc
4315 Set the modification and access times of patched files from time
4316 stamps given in context diff headers, assuming that the context diff
4317 headers use @acronym{UTC}.  @xref{Patching Time Stamps}.
4318
4319 @end table
4320
4321 @node Invoking sdiff
4322 @chapter Invoking @command{sdiff}
4323 @cindex invoking @command{sdiff}
4324 @cindex @command{sdiff} invocation
4325
4326 The @command{sdiff} command merges two files and interactively outputs the
4327 results.  Its arguments are as follows:
4328
4329 @example
4330 sdiff -o @var{outfile} @var{options}@dots{} @var{from-file} @var{to-file}
4331 @end example
4332
4333 This merges @var{from-file} with @var{to-file}, with output to @var{outfile}.
4334 If @var{from-file} is a directory and @var{to-file} is not, @command{sdiff}
4335 compares the file in @var{from-file} whose file name is that of @var{to-file},
4336 and vice versa.  @var{from-file} and @var{to-file} may not both be
4337 directories.
4338
4339 @command{sdiff} options begin with @samp{-}, so normally @var{from-file}
4340 and @var{to-file} may not begin with @samp{-}.  However, @option{--} as an
4341 argument by itself treats the remaining arguments as file names even if
4342 they begin with @samp{-}.  You may not use @file{-} as an input file.
4343
4344 @command{sdiff} without @option{--output} (@option{-o}) produces a
4345 side-by-side difference.  This usage is obsolete; use the
4346 @option{--side-by-side} (@option{-y}) option of @command{diff} instead.
4347
4348 An exit status of 0 means no differences were found, 1 means some
4349 differences were found, and 2 means trouble.
4350
4351 @menu
4352 * sdiff Options:: Summary of options to @command{diff}.
4353 @end menu
4354
4355 @node sdiff Options
4356 @section Options to @command{sdiff}
4357 @cindex @command{sdiff} options
4358 @cindex options for @command{sdiff}
4359
4360 Below is a summary of all of the options that @acronym{GNU}
4361 @command{sdiff} accepts.  Each option has two equivalent names, one of
4362 which is a single letter preceded by @samp{-}, and the other of which
4363 is a long name preceded by @samp{--}.  Multiple single letter options
4364 (unless they take an argument) can be combined into a single command
4365 line argument.  Long named options can be abbreviated to any unique
4366 prefix of their name.
4367
4368 @table @option
4369 @item -a
4370 @itemx --text
4371 Treat all files as text and compare them line-by-line, even if they
4372 do not appear to be text.  @xref{Binary}.
4373
4374 @item -b
4375 @itemx --ignore-space-change
4376 Ignore changes in amount of white space.  @xref{White Space}.
4377
4378 @item -B
4379 @itemx --ignore-blank-lines
4380 Ignore changes that just insert or delete blank lines.  @xref{Blank
4381 Lines}.
4382
4383 @item -d
4384 @itemx --minimal
4385 Change the algorithm to perhaps find a smaller set of changes.  This
4386 makes @command{sdiff} slower (sometimes much slower).  @xref{diff
4387 Performance}.
4388
4389 @item --diff-program=@var{program}
4390 Use the compatible comparison program @var{program} to compare files
4391 instead of @command{diff}.
4392
4393 @item -E
4394 @itemx --ignore-tab-expansion
4395 Ignore changes due to tab expansion.
4396 @xref{White Space}.
4397
4398 @item --help
4399 Output a summary of usage and then exit.
4400
4401 @item -i
4402 @itemx --ignore-case
4403 Ignore changes in case; consider upper- and lower-case to be the same.
4404 @xref{Case Folding}.
4405
4406 @item -I @var{regexp}
4407 @itemx --ignore-matching-lines=@var{regexp}
4408 Ignore changes that just insert or delete lines that match @var{regexp}.
4409 @xref{Specified Lines}.
4410
4411 @item -l
4412 @itemx --left-column
4413 Print only the left column of two common lines.
4414 @xref{Side by Side Format}.
4415
4416 @item -o @var{file}
4417 @itemx --output=@var{file}
4418 Put merged output into @var{file}.  This option is required for merging.
4419
4420 @item -s
4421 @itemx --suppress-common-lines
4422 Do not print common lines.  @xref{Side by Side Format}.
4423
4424 @item --speed-large-files
4425 Use heuristics to speed handling of large files that have numerous
4426 scattered small changes.  @xref{diff Performance}.
4427
4428 @item --strip-trailing-cr
4429 Strip any trailing carriage return at the end of an input line.
4430 @xref{Binary}.
4431
4432 @item -t
4433 @itemx --expand-tabs
4434 Expand tabs to spaces in the output, to preserve the alignment of tabs
4435 in the input files.  @xref{Tabs}.
4436
4437 @item --tabsize=@var{columns}
4438 Assume that tab stops are set every @var{columns} (default 8) print
4439 columns.  @xref{Tabs}.
4440
4441 @item -v
4442 @itemx --version
4443 Output version information and then exit.
4444
4445 @item -w @var{columns}
4446 @itemx --width=@var{columns}
4447 Output at most @var{columns} (default 130) print columns per line.
4448 @xref{Side by Side Format}.  Note that for historical reasons, this
4449 option is @option{-W} in @command{diff}, @option{-w} in @command{sdiff}.
4450
4451 @item -W
4452 @itemx --ignore-all-space
4453 Ignore white space when comparing lines.  @xref{White Space}.
4454 Note that for historical reasons, this option is @option{-w} in @command{diff},
4455 @option{-W} in @command{sdiff}.
4456
4457 @item -Z
4458 @itemx --ignore-trailing-space
4459 Ignore white space at line end.  @xref{White Space}.
4460 @end table
4461
4462 @node Standards conformance
4463 @chapter Standards conformance
4464 @cindex @acronym{POSIX}
4465
4466 @vindex POSIXLY_CORRECT
4467 In a few cases, the @acronym{GNU} utilities' default behavior is
4468 incompatible with the @acronym{POSIX} standard.  To suppress these
4469 incompatibilities, define the @env{POSIXLY_CORRECT} environment
4470 variable.  Unless you are checking for @acronym{POSIX} conformance, you
4471 probably do not need to define @env{POSIXLY_CORRECT}.
4472
4473 Normally options and operands can appear in any order, and programs act
4474 as if all the options appear before any operands.  For example,
4475 @samp{diff lao tzu -C 2} acts like @samp{diff -C 2 lao tzu}, since
4476 @samp{2} is an option-argument of @option{-C}.  However, if the
4477 @env{POSIXLY_CORRECT} environment variable is set, options must appear
4478 before operands, unless otherwise specified for a particular command.
4479
4480 Newer versions of @acronym{POSIX} are occasionally incompatible with older
4481 versions.  For example, older versions of @acronym{POSIX} allowed the
4482 command @samp{diff -c -10} to have the same meaning as @samp{diff -C
4483 10}, but @acronym{POSIX} 1003.1-2001 @samp{diff} no longer allows
4484 digit-string options like @option{-10}.
4485
4486 @vindex _POSIX2_VERSION
4487 The @acronym{GNU} utilities normally conform to the version of @acronym{POSIX}
4488 that is standard for your system.  To cause them to conform to a
4489 different version of @acronym{POSIX}, define the @env{_POSIX2_VERSION}
4490 environment variable to a value of the form @var{yyyymm} specifying
4491 the year and month the standard was adopted.  Two values are currently
4492 supported for @env{_POSIX2_VERSION}: @samp{199209} stands for
4493 @acronym{POSIX} 1003.2-1992, and @samp{200112} stands for @acronym{POSIX}
4494 1003.1-2001.  For example, if you are running older software that
4495 assumes an older version of @acronym{POSIX} and uses @samp{diff -c -10},
4496 you can work around the compatibility problems by setting
4497 @samp{_POSIX2_VERSION=199209} in your environment.
4498
4499 @node Projects
4500 @chapter Future Projects
4501
4502 Here are some ideas for improving @acronym{GNU} @command{diff} and
4503 @command{patch}.  The @acronym{GNU} project has identified some
4504 improvements as potential programming projects for volunteers.  You
4505 can also help by reporting any bugs that you find.
4506
4507 If you are a programmer and would like to contribute something to the
4508 @acronym{GNU} project, please consider volunteering for one of these
4509 projects.  If you are seriously contemplating work, please write to
4510 @email{gvc@@gnu.org} to coordinate with other volunteers.
4511
4512 @menu
4513 * Shortcomings:: Suggested projects for improvements.
4514 * Bugs::         Reporting bugs.
4515 @end menu
4516
4517 @node Shortcomings
4518 @section Suggested Projects for Improving @acronym{GNU} @command{diff} and @command{patch}
4519 @cindex projects for directories
4520
4521 One should be able to use @acronym{GNU} @command{diff} to generate a
4522 patch from any pair of directory trees, and given the patch and a copy
4523 of one such tree, use @command{patch} to generate a faithful copy of
4524 the other.  Unfortunately, some changes to directory trees cannot be
4525 expressed using current patch formats; also, @command{patch} does not
4526 handle some of the existing formats.  These shortcomings motivate the
4527 following suggested projects.
4528
4529 @menu
4530 * Internationalization:: Handling multibyte and varying-width characters.
4531 * Changing Structure::   Handling changes to the directory structure.
4532 * Special Files::        Handling symbolic links, device special files, etc.
4533 * Unusual File Names::   Handling file names that contain unusual characters.
4534 * Time Stamp Order::     Outputting diffs in time stamp order.
4535 * Ignoring Changes::     Ignoring certain changes while showing others.
4536 * Speedups::             Improving performance.
4537 @end menu
4538
4539 @node Internationalization
4540 @subsection Handling Multibyte and Varying-Width Characters
4541 @cindex multibyte characters
4542 @cindex varying-width characters
4543
4544 @command{diff}, @command{diff3} and @command{sdiff} treat each line of
4545 input as a string of unibyte characters.  This can mishandle multibyte
4546 characters in some cases.  For example, when asked to ignore spaces,
4547 @command{diff} does not properly ignore a multibyte space character.
4548
4549 Also, @command{diff} currently assumes that each byte is one column
4550 wide, and this assumption is incorrect in some locales, e.g., locales
4551 that use UTF-8 encoding.  This causes problems with the @option{-y} or
4552 @option{--side-by-side} option of @command{diff}.
4553
4554 These problems need to be fixed without unduly affecting the
4555 performance of the utilities in unibyte environments.
4556
4557 The IBM GNU/Linux Technology Center Internationalization Team has
4558 proposed
4559 @uref{http://oss.software.ibm.com/developer/opensource/linux/patches/i18n/diffutils-2.7.2-i18n-0.1.patch.gz,patches
4560 to support internationalized @command{diff}}.
4561 Unfortunately, these patches are incomplete and are to an older
4562 version of @command{diff}, so more work needs to be done in this area.
4563
4564 @node Changing Structure
4565 @subsection Handling Changes to the Directory Structure
4566 @cindex directory structure changes
4567
4568 @command{diff} and @command{patch} do not handle some changes to directory
4569 structure.  For example, suppose one directory tree contains a directory
4570 named @samp{D} with some subsidiary files, and another contains a file
4571 with the same name @samp{D}.  @samp{diff -r} does not output enough
4572 information for @command{patch} to transform the directory subtree into
4573 the file.
4574
4575 There should be a way to specify that a file has been removed without
4576 having to include its entire contents in the patch file.  There should
4577 also be a way to tell @command{patch} that a file was renamed, even if
4578 there is no way for @command{diff} to generate such information.
4579 There should be a way to tell @command{patch} that a file's time stamp
4580 has changed, even if its contents have not changed.
4581
4582 These problems can be fixed by extending the @command{diff} output format
4583 to represent changes in directory structure, and extending @command{patch}
4584 to understand these extensions.
4585
4586 @node Special Files
4587 @subsection Files that are Neither Directories Nor Regular Files
4588 @cindex special files
4589
4590 Some files are neither directories nor regular files: they are unusual
4591 files like symbolic links, device special files, named pipes, and
4592 sockets.  Currently, @command{diff} treats symbolic links as if they
4593 were the pointed-to files, except that a recursive @command{diff}
4594 reports an error if it detects infinite loops of symbolic links (e.g.,
4595 symbolic links to @file{..}).  @command{diff} treats other special
4596 files like regular files if they are specified at the top level, but
4597 simply reports their presence when comparing directories.  This means
4598 that @command{patch} cannot represent changes to such files.  For
4599 example, if you change which file a symbolic link points to,
4600 @command{diff} outputs the difference between the two files, instead
4601 of the change to the symbolic link.
4602
4603 @c This might not be a good idea; is it wise for root to install devices
4604 @c this way?
4605 @command{diff} should optionally report changes to special files specially,
4606 and @command{patch} should be extended to understand these extensions.
4607
4608 @node Unusual File Names
4609 @subsection File Names that Contain Unusual Characters
4610 @cindex file names with unusual characters
4611
4612 When a file name contains an unusual character like a newline or
4613 white space, @samp{diff -r} generates a patch that @command{patch} cannot
4614 parse.  The problem is with format of @command{diff} output, not just with
4615 @command{patch}, because with odd enough file names one can cause
4616 @command{diff} to generate a patch that is syntactically correct but
4617 patches the wrong files.  The format of @command{diff} output should be
4618 extended to handle all possible file names.
4619
4620 @node Time Stamp Order
4621 @subsection Outputting Diffs in Time Stamp Order
4622
4623 Applying @command{patch} to a multiple-file diff can result in files
4624 whose time stamps are out of order.  @acronym{GNU} @command{patch} has
4625 options to restore the time stamps of the updated files
4626 (@pxref{Patching Time Stamps}), but sometimes it is useful to generate
4627 a patch that works even if the recipient does not have @acronym{GNU} patch,
4628 or does not use these options.  One way to do this would be to
4629 implement a @command{diff} option to output diffs in time stamp order.
4630
4631 @node Ignoring Changes
4632 @subsection Ignoring Certain Changes
4633
4634 It would be nice to have a feature for specifying two strings, one in
4635 @var{from-file} and one in @var{to-file}, which should be considered to
4636 match.  Thus, if the two strings are @samp{foo} and @samp{bar}, then if
4637 two lines differ only in that @samp{foo} in file 1 corresponds to
4638 @samp{bar} in file 2, the lines are treated as identical.
4639
4640 It is not clear how general this feature can or should be, or
4641 what syntax should be used for it.
4642
4643 A partial substitute is to filter one or both files before comparing,
4644 e.g.:
4645
4646 @example
4647 sed 's/foo/bar/g' file1 | diff - file2
4648 @end example
4649
4650 However, this outputs the filtered text, not the original.
4651
4652 @node Speedups
4653 @subsection Improving Performance
4654
4655 When comparing two large directory structures, one of which was
4656 originally copied from the other with time stamps preserved (e.g.,
4657 with @samp{cp -pR}), it would greatly improve performance if an option
4658 told @command{diff} to assume that two files with the same size and
4659 time stamps have the same content.  @xref{diff Performance}.
4660
4661 @node Bugs
4662 @section Reporting Bugs
4663 @cindex bug reports
4664 @cindex reporting bugs
4665
4666 If you think you have found a bug in @acronym{GNU} @command{cmp},
4667 @command{diff}, @command{diff3}, or @command{sdiff}, please report it
4668 by electronic mail to the
4669 @uref{http://mail.gnu.org/mailman/listinfo/bug-gnu-utils,GNU utilities
4670 bug report mailing list} @email{bug-gnu-utils@@gnu.org}.  Please send
4671 bug reports for @acronym{GNU} @command{patch} to
4672 @email{bug-patch@@gnu.org}.  Send as precise a description of the
4673 problem as you can, including the output of the @option{--version}
4674 option and sample input files that produce the bug, if applicable.  If
4675 you have a nontrivial fix for the bug, please send it as well.  If you
4676 have a patch, please send it too.  It may simplify the maintainer's
4677 job if the patch is relative to a recent test release, which you can
4678 find in the directory @uref{ftp://alpha.gnu.org/gnu/diffutils/}.
4679
4680 @node Copying This Manual
4681 @appendix Copying This Manual
4682 @include fdl.texi
4683
4684 @node Translations
4685 @appendix Translations of This Manual
4686
4687 Nishio Futoshi of the GNUjdoc project has prepared a Japanese
4688 translation of this manual.  Its most recent version can be found at
4689 @uref{http://openlab.ring.gr.jp/gnujdoc/cvsweb/cvsweb.cgi/gnujdoc/}.
4690
4691 @node Index
4692 @appendix Index
4693
4694 @printindex cp
4695
4696 @bye