Debian Codegraph @

doc/diffutils.texi

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