1 \input texinfo @c -*-texinfo-*-
3 @comment $Id: diff.texi,v 1.2 2003/01/26 00:48:01 wiz Exp $
4 @comment %**start of header
7 @settitle Comparing and Merging Files
10 @comment %**end of header
12 This manual is for GNU Diffutils
13 (version @value{VERSION}, @value{UPDATED}),
14 and documents the @sc{gnu} @command{diff}, @command{diff3},
15 @command{sdiff}, and @command{cmp} commands for showing the
16 differences between files and the @sc{gnu} @command{patch} command for
17 using their output to update files.
19 Copyright @copyright{} 1992, 1993, 1994, 1998, 2001, 2002 Free
20 Software Foundation, Inc.
23 Permission is granted to copy, distribute and/or modify this document
24 under the terms of the GNU Free Documentation License, Version 1.1 or
25 any later version published by the Free Software Foundation; with no
26 Invariant Sections, with the Front-Cover texts being ``A GNU Manual,''
27 and with the Back-Cover Texts as in (a) below. A copy of the
28 license is included in the section entitled ``GNU Free Documentation
31 (a) The FSF's Back-Cover Text is: ``You have freedom to copy and modify
32 this GNU Manual, like GNU software. Copies published by the Free
33 Software Foundation raise funds for GNU development.''
37 @c Debian install-info (up through at least version 1.9.20) uses only the
38 @c first dircategory. Put this one first, as it is more useful in practice.
39 @dircategory Individual utilities
41 * cmp: (diff)Invoking cmp. Compare 2 files byte by byte.
42 * diff: (diff)Invoking diff. Compare 2 files line by line.
43 * diff3: (diff)Invoking diff3. Compare 3 files line by line.
44 * patch: (diff)Invoking patch. Apply a patch to a file.
45 * sdiff: (diff)Invoking sdiff. Merge 2 files side-by-side.
48 @dircategory GNU packages
50 * Diff: (diff). Comparing and merging files.
54 @title Comparing and Merging Files
55 @subtitle for Diffutils @value{VERSION} and @code{patch} 2.5.4
56 @subtitle @value{UPDATED}
57 @author David MacKenzie, Paul Eggert, and Richard Stallman
59 @vskip 0pt plus 1filll
68 @top Comparing and Merging Files
74 * Overview:: Preliminary information.
75 * Comparison:: What file comparison means.
77 * Output Formats:: Formats for two-way difference reports.
78 * Incomplete Lines:: Lines that lack trailing newlines.
79 * Comparing Directories:: Comparing files and directories.
80 * Adjusting Output:: Making @command{diff} output prettier.
81 * diff Performance:: Making @command{diff} smarter or faster.
83 * Comparing Three Files:: Formats for three-way difference reports.
84 * diff3 Merging:: Merging from a common ancestor.
86 * Interactive Merging:: Interactive merging with @command{sdiff}.
88 * Merging with patch:: Using @command{patch} to change old files into new ones.
89 * Making Patches:: Tips for making and using patch distributions.
91 * Invoking cmp:: Compare two files byte by byte.
92 * Invoking diff:: Compare two files line by line.
93 * Invoking diff3:: Compare three files line by line.
94 * Invoking patch:: Apply a diff file to an original.
95 * Invoking sdiff:: Side-by-side merge of file differences.
97 * Standards conformance:: Conformance to the @sc{posix} standard.
98 * Projects:: If you've found a bug or other shortcoming.
100 * Copying This Manual:: How to make copies of this manual.
106 @cindex overview of @command{diff} and @command{patch}
108 Computer users often find occasion to ask how two files differ. Perhaps
109 one file is a newer version of the other file. Or maybe the two files
110 started out as identical copies but were changed by different people.
112 You can use the @command{diff} command to show differences between two
113 files, or each corresponding file in two directories. @command{diff}
114 outputs differences between files line by line in any of several
115 formats, selectable by command line options. This set of differences is
116 often called a @dfn{diff} or @dfn{patch}. For files that are identical,
117 @command{diff} normally produces no output; for binary (non-text) files,
118 @command{diff} normally reports only that they are different.
120 You can use the @command{cmp} command to show the byte and line numbers
121 where two files differ. @command{cmp} can also show all the bytes
122 that differ between the two files, side by side. A way to compare
123 two files character by character is the Emacs command @kbd{M-x
124 compare-windows}. @xref{Other Window, , Other Window, emacs, The @sc{gnu}
125 Emacs Manual}, for more information on that command.
127 You can use the @command{diff3} command to show differences among three
128 files. When two people have made independent changes to a common
129 original, @command{diff3} can report the differences between the original
130 and the two changed versions, and can produce a merged file that
131 contains both persons' changes together with warnings about conflicts.
133 You can use the @command{sdiff} command to merge two files interactively.
135 You can use the set of differences produced by @command{diff} to distribute
136 updates to text files (such as program source code) to other people.
137 This method is especially useful when the differences are small compared
138 to the complete files. Given @command{diff} output, you can use the
139 @command{patch} program to update, or @dfn{patch}, a copy of the file. If you
140 think of @command{diff} as subtracting one file from another to produce
141 their difference, you can think of @command{patch} as adding the difference
142 to one file to reproduce the other.
144 This manual first concentrates on making diffs, and later shows how to
145 use diffs to update files.
147 @sc{gnu} @command{diff} was written by Paul Eggert, Mike Haertel,
148 David Hayes, Richard Stallman, and Len Tower. Wayne Davison designed and
149 implemented the unified output format. The basic algorithm is described
150 in ``An O(ND) Difference Algorithm and its Variations'', Eugene W. Myers,
151 @cite{Algorithmica} Vol.@: 1 No.@: 2, 1986, pp.@: 251--266; and in ``A File
152 Comparison Program'', Webb Miller and Eugene W. Myers,
153 @cite{Software---Practice and Experience} Vol.@: 15 No.@: 11, 1985,
155 @c From: "Gene Myers" <gene@cs.arizona.edu>
156 @c They are about the same basic algorithm; the Algorithmica
157 @c paper gives a rigorous treatment and the sub-algorithm for
158 @c delivering scripts and should be the primary reference, but
159 @c both should be mentioned.
160 The algorithm was independently discovered as described in
161 ``Algorithms for Approximate String Matching'',
162 E. Ukkonen, @cite{Information and Control} Vol.@: 64, 1985, pp.@: 100--118.
163 @c From: "Gene Myers" <gene@cs.arizona.edu>
164 @c Date: Wed, 29 Sep 1993 08:27:55 MST
165 @c Ukkonen should be given credit for also discovering the algorithm used
168 @sc{gnu} @command{diff3} was written by Randy Smith. @sc{gnu}
169 @command{sdiff} was written by Thomas Lord. @sc{gnu} @command{cmp}
170 was written by Torbjorn Granlund and David MacKenzie.
172 @command{patch} was written mainly by Larry Wall and Paul Eggert;
173 several @sc{gnu} enhancements were contributed by Wayne Davison and
174 David MacKenzie. Parts of this manual are adapted from a manual page
175 written by Larry Wall, with his permission.
178 @chapter What Comparison Means
181 There are several ways to think about the differences between two files.
182 One way to think of the differences is as a series of lines that were
183 deleted from, inserted in, or changed in one file to produce the other
184 file. @command{diff} compares two files line by line, finds groups of
185 lines that differ, and reports each group of differing lines. It can
186 report the differing lines in several formats, which have different
189 @sc{gnu} @command{diff} can show whether files are different without detailing
190 the differences. It also provides ways to suppress certain kinds of
191 differences that are not important to you. Most commonly, such
192 differences are changes in the amount of white space between words or
193 lines. @command{diff} also provides ways to suppress differences in
194 alphabetic case or in lines that match a regular expression that you
195 provide. These options can accumulate; for example, you can ignore
196 changes in both white space and alphabetic case.
198 Another way to think of the differences between two files is as a
199 sequence of pairs of bytes that can be either identical or
200 different. @command{cmp} reports the differences between two files
201 byte by byte, instead of line by line. As a result, it is often
202 more useful than @command{diff} for comparing binary files. For text
203 files, @command{cmp} is useful mainly when you want to know only whether
204 two files are identical, or whether one file is a prefix of the other.
206 To illustrate the effect that considering changes byte by byte
207 can have compared with considering them line by line, think of what
208 happens if a single newline character is added to the beginning of a
209 file. If that file is then compared with an otherwise identical file
210 that lacks the newline at the beginning, @command{diff} will report that a
211 blank line has been added to the file, while @command{cmp} will report that
212 almost every byte of the two files differs.
214 @command{diff3} normally compares three input files line by line, finds
215 groups of lines that differ, and reports each group of differing lines.
216 Its output is designed to make it easy to inspect two different sets of
217 changes to the same file.
220 * Hunks:: Groups of differing lines.
221 * White Space:: Suppressing differences in white space.
222 * Blank Lines:: Suppressing differences in blank lines.
223 * Case Folding:: Suppressing differences in alphabetic case.
224 * Specified Folding:: Suppressing differences that match regular expressions.
225 * Brief:: Summarizing which files are different.
226 * Binary:: Comparing binary files or forcing text comparisons.
233 When comparing two files, @command{diff} finds sequences of lines common to
234 both files, interspersed with groups of differing lines called
235 @dfn{hunks}. Comparing two identical files yields one sequence of
236 common lines and no hunks, because no lines differ. Comparing two
237 entirely different files yields no common lines and one large hunk that
238 contains all lines of both files. In general, there are many ways to
239 match up lines between two given files. @command{diff} tries to minimize
240 the total hunk size by finding large sequences of common lines
241 interspersed with small hunks of differing lines.
243 For example, suppose the file @file{F} contains the three lines
244 @samp{a}, @samp{b}, @samp{c}, and the file @file{G} contains the same
245 three lines in reverse order @samp{c}, @samp{b}, @samp{a}. If
246 @command{diff} finds the line @samp{c} as common, then the command
247 @samp{diff F G} produces this output:
259 But if @command{diff} notices the common line @samp{b} instead, it produces
274 It is also possible to find @samp{a} as the common line. @command{diff}
275 does not always find an optimal matching between the files; it takes
276 shortcuts to run faster. But its output is usually close to the
277 shortest possible. You can adjust this tradeoff with the
278 @option{--minimal} option (@pxref{diff Performance}).
281 @section Suppressing Differences in Blank and Tab Spacing
282 @cindex blank and tab difference suppression
283 @cindex tab and blank difference suppression
285 The @option{-E} and @option{--ignore-tab-expansion} options ignore the
286 distinction between tabs and spaces on input. A tab is considered to be
287 equivalent to the number of spaces to the next tab stop. @command{diff}
288 assumes that tab stops are set every 8 print columns.
290 The @option{-b} and @option{--ignore-space-change} options are stronger.
291 They ignore white space at line end, and consider all other sequences of
292 one or more white space characters to be equivalent. With these
293 options, @command{diff} considers the following two lines to be equivalent,
294 where @samp{$} denotes the line end:
297 Here lyeth muche rychnesse in lytell space. -- John Heywood$
298 Here lyeth muche rychnesse in lytell space. -- John Heywood $
301 The @option{-w} and @option{--ignore-all-space} options are stronger still.
302 They ignore difference even if one line has white space where
303 the other line has none. @dfn{White space} characters include
304 tab, newline, vertical tab, form feed, carriage return, and space;
305 some locales may define additional characters to be white space.
306 With these options, @command{diff} considers the
307 following two lines to be equivalent, where @samp{$} denotes the line
308 end and @samp{^M} denotes a carriage return:
311 Here lyeth muche rychnesse in lytell space.-- John Heywood$
312 He relyeth much erychnes seinly tells pace. --John Heywood ^M$
316 @section Suppressing Differences in Blank Lines
317 @cindex blank line difference suppression
319 The @option{-B} and @option{--ignore-blank-lines} options ignore insertions
320 or deletions of blank lines. These options affect only lines
321 that are completely empty; they do not affect lines that look empty but
322 contain space or tab characters. With these options, for example, a
325 1. A point is that which has no part.
327 2. A line is breadthless length.
328 -- Euclid, The Elements, I
331 is considered identical to a file containing
333 1. A point is that which has no part.
334 2. A line is breadthless length.
337 -- Euclid, The Elements, I
341 @section Suppressing Case Differences
342 @cindex case difference suppression
344 @sc{gnu} @command{diff} can treat lower case letters as equivalent to their
345 upper case counterparts, so that, for example, it considers @samp{Funky
346 Stuff}, @samp{funky STUFF}, and @samp{fUNKy stuFf} to all be the same.
347 To request this, use the @option{-i} or @option{--ignore-case} option.
349 @node Specified Folding
350 @section Suppressing Lines Matching a Regular Expression
351 @cindex regular expression suppression
353 To ignore insertions and deletions of lines that match a
354 @command{grep}-style regular expression, use the @option{-I
355 @var{regexp}} or @option{--ignore-matching-lines=@var{regexp}} option.
357 regular expressions that contain shell metacharacters to prevent the
358 shell from expanding them. For example, @samp{diff -I '^[[:digit:]]'} ignores
359 all changes to lines beginning with a digit.
361 However, @option{-I} only ignores the insertion or deletion of lines that
362 contain the regular expression if every changed line in the hunk---every
363 insertion and every deletion---matches the regular expression. In other
364 words, for each nonignorable change, @command{diff} prints the complete set
365 of changes in its vicinity, including the ignorable ones.
367 You can specify more than one regular expression for lines to ignore by
368 using more than one @option{-I} option. @command{diff} tries to match each
369 line against each regular expression.
372 @section Summarizing Which Files Differ
373 @cindex summarizing which files differ
374 @cindex brief difference reports
376 When you only want to find out whether files are different, and you
377 don't care what the differences are, you can use the summary output
378 format. In this format, instead of showing the differences between the
379 files, @command{diff} simply reports whether files differ. The @option{-q}
380 and @option{--brief} options select this output format.
382 This format is especially useful when comparing the contents of two
383 directories. It is also much faster than doing the normal line by line
384 comparisons, because @command{diff} can stop analyzing the files as soon as
385 it knows that there are any differences.
387 You can also get a brief indication of whether two files differ by using
388 @command{cmp}. For files that are identical, @command{cmp} produces no
389 output. When the files differ, by default, @command{cmp} outputs the byte
390 and line number where the first difference occurs. You can use
391 the @option{-s} option to suppress that information, so that @command{cmp}
392 produces no output and reports whether the files differ using only its
393 exit status (@pxref{Invoking cmp}).
396 Unlike @command{diff}, @command{cmp} cannot compare directories; it can only
400 @section Binary Files and Forcing Text Comparisons
401 @cindex binary file diff
402 @cindex text versus binary diff
404 If @command{diff} thinks that either of the two files it is comparing is
405 binary (a non-text file), it normally treats that pair of files much as
406 if the summary output format had been selected (@pxref{Brief}), and
407 reports only that the binary files are different. This is because line
408 by line comparisons are usually not meaningful for binary files.
410 @command{diff} determines whether a file is text or binary by checking the
411 first few bytes in the file; the exact number of bytes is system
412 dependent, but it is typically several thousand. If every byte in
413 that part of the file is non-null, @command{diff} considers the file to be
414 text; otherwise it considers the file to be binary.
416 Sometimes you might want to force @command{diff} to consider files to be
417 text. For example, you might be comparing text files that contain
418 null characters; @command{diff} would erroneously decide that those are
419 non-text files. Or you might be comparing documents that are in a
420 format used by a word processing system that uses null characters to
421 indicate special formatting. You can force @command{diff} to consider all
422 files to be text files, and compare them line by line, by using the
423 @option{-a} or @option{--text} option. If the files you compare using this
424 option do not in fact contain text, they will probably contain few
425 newline characters, and the @command{diff} output will consist of hunks
426 showing differences between long lines of whatever characters the files
429 You can also force @command{diff} to consider all files to be binary files,
430 and report only whether they differ (but not how). Use the
431 @option{-q} or @option{--brief} option for this.
433 Differing binary files are considered to cause trouble because the
434 resulting @command{diff} output does not capture all the differences.
435 This trouble causes @command{diff} to exit with status 2. However,
436 this trouble cannot occur with the @option{--a} or @option{--text}
437 option, or with the @option{-q} or @option{--brief} option, as these
438 options both cause @command{diff} to treat binary files like text
441 In operating systems that distinguish between text and binary files,
442 @command{diff} normally reads and writes all data as text. Use the
443 @option{--binary} option to force @command{diff} to read and write binary
444 data instead. This option has no effect on a @sc{posix}-compliant system
445 like @sc{gnu} or traditional Unix. However, many personal computer
446 operating systems represent the end of a line with a carriage return
447 followed by a newline. On such systems, @command{diff} normally ignores
448 these carriage returns on input and generates them at the end of each
449 output line, but with the @option{--binary} option @command{diff} treats
450 each carriage return as just another input character, and does not
451 generate a carriage return at the end of each output line. This can be
452 useful when dealing with non-text files that are meant to be
453 interchanged with @sc{posix}-compliant systems.
455 The @option{--strip-trailing-cr} causes @command{diff} to treat input
456 lines that end in carriage return followed by newline as if they end
457 in plain newline. This can be useful when comparing text that is
458 imperfectly imported from many personal computer operating systems.
459 This option affects how lines are read, which in turn affects how they
460 are compared and output.
462 If you want to compare two files byte by byte, you can use the
463 @command{cmp} program with the @option{-l} option to show the values
464 of each differing byte in the two files. With @sc{gnu} @command{cmp},
465 you can also use the @option{-b} option to show the @sc{ascii}
466 representation of those bytes. @xref{Invoking cmp}, for more
469 If @command{diff3} thinks that any of the files it is comparing is binary
470 (a non-text file), it normally reports an error, because such
471 comparisons are usually not useful. @command{diff3} uses the same test as
472 @command{diff} to decide whether a file is binary. As with @command{diff}, if
473 the input files contain a few non-text bytes but otherwise are like
474 text files, you can force @command{diff3} to consider all files to be text
475 files and compare them line by line by using the @option{-a} or
476 @option{--text} options.
479 @chapter @command{diff} Output Formats
480 @cindex output formats
481 @cindex format of @command{diff} output
483 @command{diff} has several mutually exclusive options for output format.
484 The following sections describe each format, illustrating how
485 @command{diff} reports the differences between two sample input files.
488 * Sample diff Input:: Sample @command{diff} input files for examples.
489 * Normal:: Showing differences without surrounding text.
490 * Context:: Showing differences with the surrounding text.
491 * Side by Side:: Showing differences in two columns.
492 * Scripts:: Generating scripts for other programs.
493 * If-then-else:: Merging files with if-then-else.
496 @node Sample diff Input
497 @section Two Sample Input Files
498 @cindex @command{diff} sample input
499 @cindex sample input for @command{diff}
501 Here are two sample files that we will use in numerous examples to
502 illustrate the output of @command{diff} and how various options can change
505 This is the file @file{lao}:
508 The Way that can be told of is not the eternal Way;
509 The name that can be named is not the eternal name.
510 The Nameless is the origin of Heaven and Earth;
511 The Named is the mother of all things.
512 Therefore let there always be non-being,
513 so we may see their subtlety,
514 And let there always be being,
515 so we may see their outcome.
516 The two are the same,
517 But after they are produced,
518 they have different names.
521 This is the file @file{tzu}:
524 The Nameless is the origin of Heaven and Earth;
525 The named is the mother of all things.
527 Therefore let there always be non-being,
528 so we may see their subtlety,
529 And let there always be being,
530 so we may see their outcome.
531 The two are the same,
532 But after they are produced,
533 they have different names.
534 They both may be called deep and profound.
535 Deeper and more profound,
536 The door of all subtleties!
539 In this example, the first hunk contains just the first two lines of
540 @file{lao}, the second hunk contains the fourth line of @file{lao}
541 opposing the second and third lines of @file{tzu}, and the last hunk
542 contains just the last three lines of @file{tzu}.
545 @section Showing Differences Without Context
546 @cindex normal output format
547 @cindex @samp{<} output format
549 The ``normal'' @command{diff} output format shows each hunk of differences
550 without any surrounding context. Sometimes such output is the clearest
551 way to see how lines have changed, without the clutter of nearby
552 unchanged lines (although you can get similar results with the context
553 or unified formats by using 0 lines of context). However, this format
554 is no longer widely used for sending out patches; for that purpose, the
555 context format (@pxref{Context Format}) and the unified format
556 (@pxref{Unified Format}) are superior. Normal format is the default for
557 compatibility with older versions of @command{diff} and the @sc{posix}
558 standard. Use the @option{--normal} option to select this output
562 * Detailed Normal:: A detailed description of normal output format.
563 * Example Normal:: Sample output in the normal format.
566 @node Detailed Normal
567 @subsection Detailed Description of Normal Format
569 The normal output format consists of one or more hunks of differences;
570 each hunk shows one area where the files differ. Normal format hunks
575 < @var{from-file-line}
576 < @var{from-file-line}@dots{}
579 > @var{to-file-line}@dots{}
582 There are three types of change commands. Each consists of a line
583 number or comma-separated range of lines in the first file, a single
584 character indicating the kind of change to make, and a line number or
585 comma-separated range of lines in the second file. All line numbers are
586 the original line numbers in each file. The types of change commands
590 @item @var{l}a@var{r}
591 Add the lines in range @var{r} of the second file after line @var{l} of
592 the first file. For example, @samp{8a12,15} means append lines 12--15
593 of file 2 after line 8 of file 1; or, if changing file 2 into file 1,
594 delete lines 12--15 of file 2.
596 @item @var{f}c@var{t}
597 Replace the lines in range @var{f} of the first file with lines in range
598 @var{t} of the second file. This is like a combined add and delete, but
599 more compact. For example, @samp{5,7c8,10} means change lines 5--7 of
600 file 1 to read as lines 8--10 of file 2; or, if changing file 2 into
601 file 1, change lines 8--10 of file 2 to read as lines 5--7 of file 1.
603 @item @var{r}d@var{l}
604 Delete the lines in range @var{r} from the first file; line @var{l} is where
605 they would have appeared in the second file had they not been deleted.
606 For example, @samp{5,7d3} means delete lines 5--7 of file 1; or, if
607 changing file 2 into file 1, append lines 5--7 of file 1 after line 3 of
612 @subsection An Example of Normal Format
614 Here is the output of the command @samp{diff lao tzu}
615 (@pxref{Sample diff Input}, for the complete contents of the two files).
616 Notice that it shows only the lines that are different between the two
621 < The Way that can be told of is not the eternal Way;
622 < The name that can be named is not the eternal name.
624 < The Named is the mother of all things.
626 > The named is the mother of all things.
629 > They both may be called deep and profound.
630 > Deeper and more profound,
631 > The door of all subtleties!
635 @section Showing Differences in Their Context
636 @cindex context output format
637 @cindex @samp{!} output format
639 Usually, when you are looking at the differences between files, you will
640 also want to see the parts of the files near the lines that differ, to
641 help you understand exactly what has changed. These nearby parts of the
642 files are called the @dfn{context}.
644 @sc{gnu} @command{diff} provides two output formats that show context
645 around the differing lines: @dfn{context format} and @dfn{unified
646 format}. It can optionally show in which function or section of the
647 file the differing lines are found.
649 If you are distributing new versions of files to other people in the
650 form of @command{diff} output, you should use one of the output formats
651 that show context so that they can apply the diffs even if they have
652 made small changes of their own to the files. @command{patch} can apply
653 the diffs in this case by searching in the files for the lines of
654 context around the differing lines; if those lines are actually a few
655 lines away from where the diff says they are, @command{patch} can adjust
656 the line numbers accordingly and still apply the diff correctly.
657 @xref{Imperfect}, for more information on using @command{patch} to apply
661 * Context Format:: An output format that shows surrounding lines.
662 * Unified Format:: A more compact output format that shows context.
663 * Sections:: Showing which sections of the files differences are in.
664 * Alternate Names:: Showing alternate file names in context headers.
668 @subsection Context Format
670 The context output format shows several lines of context around the
671 lines that differ. It is the standard format for distributing updates
674 To select this output format, use the @option{-C @var{lines}},
675 @option{--context@r{[}=@var{lines}@r{]}}, or @option{-c} option. The
676 argument @var{lines} that some of these options take is the number of
677 lines of context to show. If you do not specify @var{lines}, it
678 defaults to three. For proper operation, @command{patch} typically needs
679 at least two lines of context.
682 * Detailed Context:: A detailed description of the context output format.
683 * Example Context:: Sample output in context format.
684 * Less Context:: Another sample with less context.
687 @node Detailed Context
688 @subsubsection Detailed Description of Context Format
690 The context output format starts with a two-line header, which looks
694 *** @var{from-file} @var{from-file-modification-time}
695 --- @var{to-file} @var{to-file-modification time}
700 @cindex time stamp format, context diffs
701 The time stamp normally looks like @samp{2002-02-21 23:30:39.942229878
702 -0800} to indicate the date, time with fractional seconds, and time
703 zone in @uref{ftp://ftp.isi.edu/in-notes/rfc2822.txt, Internet RFC
704 2822 format}. However, a traditional time stamp like @samp{Thu Feb 21
705 23:30:39 2002} is used if the @env{LC_TIME} locale category is either
706 @samp{C} or @samp{POSIX}.
708 You can change the header's content with the
709 @option{--label=@var{label}} option; see @ref{Alternate Names}.
711 Next come one or more hunks of differences; each hunk shows one area
712 where the files differ. Context format hunks look like this:
716 *** @var{from-file-line-range} ****
718 @var{from-file-line}@dots{}
719 --- @var{to-file-line-range} ----
721 @var{to-file-line}@dots{}
724 The lines of context around the lines that differ start with two space
725 characters. The lines that differ between the two files start with one
726 of the following indicator characters, followed by a space character:
730 A line that is part of a group of one or more lines that changed between
731 the two files. There is a corresponding group of lines marked with
732 @samp{!} in the part of this hunk for the other file.
735 An ``inserted'' line in the second file that corresponds to nothing in
739 A ``deleted'' line in the first file that corresponds to nothing in the
743 If all of the changes in a hunk are insertions, the lines of
744 @var{from-file} are omitted. If all of the changes are deletions, the
745 lines of @var{to-file} are omitted.
747 @node Example Context
748 @subsubsection An Example of Context Format
750 Here is the output of @samp{diff -c lao tzu} (@pxref{Sample diff Input},
751 for the complete contents of the two files). Notice that up to three
752 lines that are not different are shown around each line that is
753 different; they are the context lines. Also notice that the first two
754 hunks have run together, because their contents overlap.
757 *** lao 2002-02-21 23:30:39.942229878 -0800
758 --- tzu 2002-02-21 23:30:50.442260588 -0800
761 - The Way that can be told of is not the eternal Way;
762 - The name that can be named is not the eternal name.
763 The Nameless is the origin of Heaven and Earth;
764 ! The Named is the mother of all things.
765 Therefore let there always be non-being,
766 so we may see their subtlety,
767 And let there always be being,
769 The Nameless is the origin of Heaven and Earth;
770 ! The named is the mother of all things.
772 Therefore let there always be non-being,
773 so we may see their subtlety,
774 And let there always be being,
778 The two are the same,
779 But after they are produced,
780 they have different names.
781 + They both may be called deep and profound.
782 + Deeper and more profound,
783 + The door of all subtleties!
787 @subsubsection An Example of Context Format with Less Context
789 Here is the output of @samp{diff -C 1 lao tzu} (@pxref{Sample diff
790 Input}, for the complete contents of the two files). Notice that at
791 most one context line is reported here.
794 *** lao 2002-02-21 23:30:39.942229878 -0800
795 --- tzu 2002-02-21 23:30:50.442260588 -0800
798 - The Way that can be told of is not the eternal Way;
799 - The name that can be named is not the eternal name.
800 The Nameless is the origin of Heaven and Earth;
801 ! The Named is the mother of all things.
802 Therefore let there always be non-being,
804 The Nameless is the origin of Heaven and Earth;
805 ! The named is the mother of all things.
807 Therefore let there always be non-being,
811 they have different names.
812 + They both may be called deep and profound.
813 + Deeper and more profound,
814 + The door of all subtleties!
818 @subsection Unified Format
819 @cindex unified output format
820 @cindex @samp{+-} output format
822 The unified output format is a variation on the context format that is
823 more compact because it omits redundant context lines. To select this
824 output format, use the @option{-U @var{lines}},
825 @option{--unified@r{[}=@var{lines}@r{]}}, or @option{-u}
826 option. The argument @var{lines} is the number of lines of context to
827 show. When it is not given, it defaults to three.
829 At present, only @sc{gnu} @command{diff} can produce this format and
830 only @sc{gnu} @command{patch} can automatically apply diffs in this
831 format. For proper operation, @command{patch} typically needs at
832 least three lines of context.
835 * Detailed Unified:: A detailed description of unified format.
836 * Example Unified:: Sample output in unified format.
839 @node Detailed Unified
840 @subsubsection Detailed Description of Unified Format
842 The unified output format starts with a two-line header, which looks
846 --- @var{from-file} @var{from-file-modification-time}
847 +++ @var{to-file} @var{to-file-modification-time}
851 @cindex time stamp format, unified diffs
852 The time stamp looks like @samp{2002-02-21 23:30:39.942229878 -0800}
853 to indicate the date, time with fractional seconds, and time zone.
855 You can change the header's content with the
856 @option{--label=@var{label}} option; see @xref{Alternate Names}.
858 Next come one or more hunks of differences; each hunk shows one area
859 where the files differ. Unified format hunks look like this:
862 @@@@ @var{from-file-range} @var{to-file-range} @@@@
863 @var{line-from-either-file}
864 @var{line-from-either-file}@dots{}
867 The lines common to both files begin with a space character. The lines
868 that actually differ between the two files have one of the following
869 indicator characters in the left print column:
873 A line was added here to the first file.
876 A line was removed here from the first file.
879 @node Example Unified
880 @subsubsection An Example of Unified Format
882 Here is the output of the command @samp{diff -u lao tzu}
883 (@pxref{Sample diff Input}, for the complete contents of the two files):
886 --- lao 2002-02-21 23:30:39.942229878 -0800
887 +++ tzu 2002-02-21 23:30:50.442260588 -0800
889 -The Way that can be told of is not the eternal Way;
890 -The name that can be named is not the eternal name.
891 The Nameless is the origin of Heaven and Earth;
892 -The Named is the mother of all things.
893 +The named is the mother of all things.
895 Therefore let there always be non-being,
896 so we may see their subtlety,
897 And let there always be being,
899 The two are the same,
900 But after they are produced,
901 they have different names.
902 +They both may be called deep and profound.
903 +Deeper and more profound,
904 +The door of all subtleties!
908 @subsection Showing Which Sections Differences Are in
910 @cindex section headings
912 Sometimes you might want to know which part of the files each change
913 falls in. If the files are source code, this could mean which function
914 was changed. If the files are documents, it could mean which chapter or
915 appendix was changed. @sc{gnu} @command{diff} can show this by displaying the
916 nearest section heading line that precedes the differing lines. Which
917 lines are ``section headings'' is determined by a regular expression.
920 * Specified Headings:: Showing headings that match regular expressions.
921 * C Function Headings:: Showing headings of C functions.
924 @node Specified Headings
925 @subsubsection Showing Lines That Match Regular Expressions
926 @cindex specified headings
927 @cindex regular expression matching headings
929 To show in which sections differences occur for files that are not
930 source code for C or similar languages, use the @option{-F @var{regexp}}
931 or @option{--show-function-line=@var{regexp}} option. @command{diff}
932 considers lines that match the @command{grep}-style regular expression
933 @var{regexp} to be the beginning
934 of a section of the file. Here are suggested regular expressions for
935 some common languages:
937 @c Please add to this list, e.g. Fortran, Pascal, Perl, Python.
947 This option does not automatically select an output format; in order to
948 use it, you must select the context format (@pxref{Context Format}) or
949 unified format (@pxref{Unified Format}). In other output formats it
952 The @option{-F} and @option{--show-function-line} options find the nearest
953 unchanged line that precedes each hunk of differences and matches the
954 given regular expression. Then they add that line to the end of the
955 line of asterisks in the context format, or to the @samp{@@@@} line in
956 unified format. If no matching line exists, they leave the output for
957 that hunk unchanged. If that line is more than 40 characters long, they
958 output only the first 40 characters. You can specify more than one
959 regular expression for such lines; @command{diff} tries to match each line
960 against each regular expression, starting with the last one given. This
961 means that you can use @option{-p} and @option{-F} together, if you wish.
963 @node C Function Headings
964 @subsubsection Showing C Function Headings
965 @cindex C function headings
966 @cindex function headings, C
968 To show in which functions differences occur for C and similar
969 languages, you can use the @option{-p} or @option{--show-c-function} option.
970 This option automatically defaults to the context output format
971 (@pxref{Context Format}), with the default number of lines of context.
972 You can override that number with @option{-C @var{lines}} elsewhere in the
973 command line. You can override both the format and the number with
974 @option{-U @var{lines}} elsewhere in the command line.
976 The @option{-p} and @option{--show-c-function} options are equivalent to
977 @option{-F '^[[:alpha:]$_]'} if the unified format is specified, otherwise
978 @option{-c -F '^[[:alpha:]$_]'} (@pxref{Specified Headings}). @sc{gnu}
979 @command{diff} provides them for the sake of convenience.
981 @node Alternate Names
982 @subsection Showing Alternate File Names
983 @cindex alternate file names
984 @cindex file name alternates
986 If you are comparing two files that have meaningless or uninformative
987 names, you might want @command{diff} to show alternate names in the header
988 of the context and unified output formats. To do this, use the
989 @option{--label=@var{label}} option. The first time
990 you give this option, its argument replaces the name and date of the
991 first file in the header; the second time, its argument replaces the
992 name and date of the second file. If you give this option more than
993 twice, @command{diff} reports an error. The @option{--label} option does not
994 affect the file names in the @command{pr} header when the @option{-l} or
995 @option{--paginate} option is used (@pxref{Pagination}).
997 Here are the first two lines of the output from @samp{diff -C 2
998 --label=original --label=modified lao tzu}:
1006 @section Showing Differences Side by Side
1007 @cindex side by side
1008 @cindex two-column output
1009 @cindex columnar output
1011 @command{diff} can produce a side by side difference listing of two files.
1012 The files are listed in two columns with a gutter between them. The
1013 gutter contains one of the following markers:
1017 The corresponding lines are in common. That is, either the lines are
1018 identical, or the difference is ignored because of one of the
1019 @option{--ignore} options (@pxref{White Space}).
1022 The corresponding lines differ, and they are either both complete
1026 The files differ and only the first file contains the line.
1029 The files differ and only the second file contains the line.
1032 Only the first file contains the line, but the difference is ignored.
1035 Only the second file contains the line, but the difference is ignored.
1038 The corresponding lines differ, and only the first line is incomplete.
1041 The corresponding lines differ, and only the second line is incomplete.
1044 Normally, an output line is incomplete if and only if the lines that it
1045 contains are incomplete; @xref{Incomplete Lines}. However, when an
1046 output line represents two differing lines, one might be incomplete
1047 while the other is not. In this case, the output line is complete,
1048 but its the gutter is marked @samp{\} if the first line is incomplete,
1049 @samp{/} if the second line is.
1051 Side by side format is sometimes easiest to read, but it has limitations.
1052 It generates much wider output than usual, and truncates lines that are
1053 too long to fit. Also, it relies on lining up output more heavily than
1054 usual, so its output looks particularly bad if you use varying
1055 width fonts, nonstandard tab stops, or nonprinting characters.
1057 You can use the @command{sdiff} command to interactively merge side by side
1058 differences. @xref{Interactive Merging}, for more information on merging files.
1061 * Side by Side Format:: Controlling side by side output format.
1062 * Example Side by Side:: Sample side by side output.
1065 @node Side by Side Format
1066 @subsection Controlling Side by Side Format
1067 @cindex side by side format
1069 The @option{-y} or @option{--side-by-side} option selects side by side
1070 format. Because side by side output lines contain two input lines, the
1071 output is wider than usual: normally 130 print columns, which can fit
1072 onto a traditional printer line. You can set the width of the output
1073 with the @option{-W @var{columns}} or @option{--width=@var{columns}}
1074 option. The output is split into two halves of equal width, separated by a
1075 small gutter to mark differences; the right half is aligned to a tab
1076 stop so that tabs line up. Input lines that are too long to fit in half
1077 of an output line are truncated for output.
1079 The @option{--left-column} option prints only the left column of two
1080 common lines. The @option{--suppress-common-lines} option suppresses
1081 common lines entirely.
1083 @node Example Side by Side
1084 @subsection An Example of Side by Side Format
1086 Here is the output of the command @samp{diff -y -W 72 lao tzu}
1087 (@pxref{Sample diff Input}, for the complete contents of the two files).
1090 The Way that can be told of is n <
1091 The name that can be named is no <
1092 The Nameless is the origin of He The Nameless is the origin of He
1093 The Named is the mother of all t | The named is the mother of all t
1095 Therefore let there always be no Therefore let there always be no
1096 so we may see their subtlety, so we may see their subtlety,
1097 And let there always be being, And let there always be being,
1098 so we may see their outcome. so we may see their outcome.
1099 The two are the same, The two are the same,
1100 But after they are produced, But after they are produced,
1101 they have different names. they have different names.
1102 > They both may be called deep and
1103 > Deeper and more profound,
1104 > The door of all subtleties!
1108 @section Making Edit Scripts
1109 @cindex script output formats
1111 Several output modes produce command scripts for editing @var{from-file}
1112 to produce @var{to-file}.
1115 * ed Scripts:: Using @command{diff} to produce commands for @command{ed}.
1116 * Forward ed:: Making forward @command{ed} scripts.
1117 * RCS:: A special @command{diff} output format used by @sc{rcs}.
1121 @subsection @command{ed} Scripts
1122 @cindex @command{ed} script output format
1124 @command{diff} can produce commands that direct the @command{ed} text editor
1125 to change the first file into the second file. Long ago, this was the
1126 only output mode that was suitable for editing one file into another
1127 automatically; today, with @command{patch}, it is almost obsolete. Use the
1128 @option{-e} or @option{--ed} option to select this output format.
1130 Like the normal format (@pxref{Normal}), this output format does not
1131 show any context; unlike the normal format, it does not include the
1132 information necessary to apply the diff in reverse (to produce the first
1133 file if all you have is the second file and the diff).
1135 If the file @file{d} contains the output of @samp{diff -e old new}, then
1136 the command @samp{(cat d && echo w) | ed - old} edits @file{old} to make
1137 it a copy of @file{new}. More generally, if @file{d1}, @file{d2},
1138 @dots{}, @file{dN} contain the outputs of @samp{diff -e old new1},
1139 @samp{diff -e new1 new2}, @dots{}, @samp{diff -e newN-1 newN},
1140 respectively, then the command @samp{(cat d1 d2 @dots{} dN && echo w) |
1141 ed - old} edits @file{old} to make it a copy of @file{newN}.
1144 * Detailed ed:: A detailed description of @command{ed} format.
1145 * Example ed:: A sample @command{ed} script.
1149 @subsubsection Detailed Description of @command{ed} Format
1151 The @command{ed} output format consists of one or more hunks of
1152 differences. The changes closest to the ends of the files come first so
1153 that commands that change the number of lines do not affect how
1154 @command{ed} interprets line numbers in succeeding commands. @command{ed}
1155 format hunks look like this:
1158 @var{change-command}
1160 @var{to-file-line}@dots{}
1164 Because @command{ed} uses a single period on a line to indicate the end of
1165 input, @sc{gnu} @command{diff} protects lines of changes that contain a single
1166 period on a line by writing two periods instead, then writing a
1167 subsequent @command{ed} command to change the two periods into one. The
1168 @command{ed} format cannot represent an incomplete line, so if the second
1169 file ends in a changed incomplete line, @command{diff} reports an error and
1170 then pretends that a newline was appended.
1172 There are three types of change commands. Each consists of a line
1173 number or comma-separated range of lines in the first file and a single
1174 character indicating the kind of change to make. All line numbers are
1175 the original line numbers in the file. The types of change commands
1180 Add text from the second file after line @var{l} in the first file. For
1181 example, @samp{8a} means to add the following lines after line 8 of file
1185 Replace the lines in range @var{r} in the first file with the following
1186 lines. Like a combined add and delete, but more compact. For example,
1187 @samp{5,7c} means change lines 5--7 of file 1 to read as the text file
1191 Delete the lines in range @var{r} from the first file. For example,
1192 @samp{5,7d} means delete lines 5--7 of file 1.
1196 @subsubsection Example @command{ed} Script
1198 Here is the output of @samp{diff -e lao tzu} (@pxref{Sample
1199 diff Input}, for the complete contents of the two files):
1203 They both may be called deep and profound.
1204 Deeper and more profound,
1205 The door of all subtleties!
1208 The named is the mother of all things.
1215 @subsection Forward @command{ed} Scripts
1216 @cindex forward @command{ed} script output format
1218 @command{diff} can produce output that is like an @command{ed} script, but
1219 with hunks in forward (front to back) order. The format of the commands
1220 is also changed slightly: command characters precede the lines they
1221 modify, spaces separate line numbers in ranges, and no attempt is made
1222 to disambiguate hunk lines consisting of a single period. Like
1223 @command{ed} format, forward @command{ed} format cannot represent incomplete
1226 Forward @command{ed} format is not very useful, because neither @command{ed}
1227 nor @command{patch} can apply diffs in this format. It exists mainly for
1228 compatibility with older versions of @command{diff}. Use the @option{-f} or
1229 @option{--forward-ed} option to select it.
1232 @subsection @sc{rcs} Scripts
1233 @cindex @sc{rcs} script output format
1235 The @sc{rcs} output format is designed specifically for use by the Revision
1236 Control System, which is a set of free programs used for organizing
1237 different versions and systems of files. Use the @option{-n} or
1238 @option{--rcs} option to select this output format. It is like the
1239 forward @command{ed} format (@pxref{Forward ed}), but it can represent
1240 arbitrary changes to the contents of a file because it avoids the
1241 forward @command{ed} format's problems with lines consisting of a single
1242 period and with incomplete lines. Instead of ending text sections with
1243 a line consisting of a single period, each command specifies the number
1244 of lines it affects; a combination of the @samp{a} and @samp{d}
1245 commands are used instead of @samp{c}. Also, if the second file ends
1246 in a changed incomplete line, then the output also ends in an
1249 Here is the output of @samp{diff -n lao tzu} (@pxref{Sample
1250 diff Input}, for the complete contents of the two files):
1256 The named is the mother of all things.
1259 They both may be called deep and profound.
1260 Deeper and more profound,
1261 The door of all subtleties!
1265 @section Merging Files with If-then-else
1266 @cindex merged output format
1267 @cindex if-then-else output format
1268 @cindex C if-then-else output format
1269 @cindex @command{ifdef} output format
1271 You can use @command{diff} to merge two files of C source code. The output
1272 of @command{diff} in this format contains all the lines of both files.
1273 Lines common to both files are output just once; the differing parts are
1274 separated by the C preprocessor directives @code{#ifdef @var{name}} or
1275 @code{#ifndef @var{name}}, @code{#else}, and @code{#endif}. When
1276 compiling the output, you select which version to use by either defining
1277 or leaving undefined the macro @var{name}.
1279 To merge two files, use @command{diff} with the @option{-D @var{name}} or
1280 @option{--ifdef=@var{name}} option. The argument @var{name} is the C
1281 preprocessor identifier to use in the @code{#ifdef} and @code{#ifndef}
1284 For example, if you change an instance of @code{wait (&s)} to
1285 @code{waitpid (-1, &s, 0)} and then merge the old and new files with
1286 the @option{--ifdef=HAVE_WAITPID} option, then the affected part of your code
1287 might look like this:
1291 #ifndef HAVE_WAITPID
1292 if ((w = wait (&s)) < 0 && errno != EINTR)
1293 #else /* HAVE_WAITPID */
1294 if ((w = waitpid (-1, &s, 0)) < 0 && errno != EINTR)
1295 #endif /* HAVE_WAITPID */
1297 @} while (w != child);
1300 You can specify formats for languages other than C by using line group
1301 formats and line formats, as described in the next sections.
1304 * Line Group Formats:: Formats for general if-then-else line groups.
1305 * Line Formats:: Formats for each line in a line group.
1306 * Detailed If-then-else:: A detailed description of if-then-else format.
1307 * Example If-then-else:: Sample if-then-else format output.
1310 @node Line Group Formats
1311 @subsection Line Group Formats
1312 @cindex line group formats
1313 @cindex formats for if-then-else line groups
1315 Line group formats let you specify formats suitable for many
1316 applications that allow if-then-else input, including programming
1317 languages and text formatting languages. A line group format specifies
1318 the output format for a contiguous group of similar lines.
1320 For example, the following command compares the TeX files @file{old}
1321 and @file{new}, and outputs a merged file in which old regions are
1322 surrounded by @samp{\begin@{em@}}-@samp{\end@{em@}} lines, and new
1323 regions are surrounded by @samp{\begin@{bf@}}-@samp{\end@{bf@}} lines.
1327 --old-group-format='\begin@{em@}
1330 --new-group-format='\begin@{bf@}
1336 The following command is equivalent to the above example, but it is a
1337 little more verbose, because it spells out the default line group formats.
1341 --old-group-format='\begin@{em@}
1344 --new-group-format='\begin@{bf@}
1347 --unchanged-group-format='%=' \
1348 --changed-group-format='\begin@{em@}
1356 Here is a more advanced example, which outputs a diff listing with
1357 headers containing line numbers in a ``plain English'' style.
1361 --unchanged-group-format='' \
1362 --old-group-format='-------- %dn line%(n=1?:s) deleted at %df:
1364 --new-group-format='-------- %dN line%(N=1?:s) added after %de:
1366 --changed-group-format='-------- %dn line%(n=1?:s) changed at %df:
1372 To specify a line group format, use @command{diff} with one of the options
1373 listed below. You can specify up to four line group formats, one for
1374 each kind of line group. You should quote @var{format}, because it
1375 typically contains shell metacharacters.
1378 @item --old-group-format=@var{format}
1379 These line groups are hunks containing only lines from the first file.
1380 The default old group format is the same as the changed group format if
1381 it is specified; otherwise it is a format that outputs the line group as-is.
1383 @item --new-group-format=@var{format}
1384 These line groups are hunks containing only lines from the second
1385 file. The default new group format is same as the changed group
1386 format if it is specified; otherwise it is a format that outputs the
1389 @item --changed-group-format=@var{format}
1390 These line groups are hunks containing lines from both files. The
1391 default changed group format is the concatenation of the old and new
1394 @item --unchanged-group-format=@var{format}
1395 These line groups contain lines common to both files. The default
1396 unchanged group format is a format that outputs the line group as-is.
1399 In a line group format, ordinary characters represent themselves;
1400 conversion specifications start with @samp{%} and have one of the
1405 stands for the lines from the first file, including the trailing newline.
1406 Each line is formatted according to the old line format (@pxref{Line Formats}).
1409 stands for the lines from the second file, including the trailing newline.
1410 Each line is formatted according to the new line format.
1413 stands for the lines common to both files, including the trailing newline.
1414 Each line is formatted according to the unchanged line format.
1417 stands for @samp{%}.
1420 where @var{C} is a single character, stands for @var{C}.
1421 @var{C} may not be a backslash or an apostrophe.
1422 For example, @samp{%c':'} stands for a colon, even inside
1423 the then-part of an if-then-else format, which a colon would
1427 where @var{O} is a string of 1, 2, or 3 octal digits,
1428 stands for the character with octal code @var{O}.
1429 For example, @samp{%c'\0'} stands for a null character.
1431 @item @var{F}@var{n}
1432 where @var{F} is a @code{printf} conversion specification and @var{n} is one
1433 of the following letters, stands for @var{n}'s value formatted with @var{F}.
1437 The line number of the line just before the group in the old file.
1440 The line number of the first line in the group in the old file;
1444 The line number of the last line in the group in the old file.
1447 The line number of the line just after the group in the old file;
1451 The number of lines in the group in the old file; equals @var{l} - @var{f} + 1.
1454 Likewise, for lines in the new file.
1459 The @code{printf} conversion specification can be @samp{%d},
1460 @samp{%o}, @samp{%x}, or @samp{%X}, specifying decimal, octal,
1461 lower case hexadecimal, or upper case hexadecimal output
1462 respectively. After the @samp{%} the following options can appear in
1463 sequence: a series of zero or more flags; an integer
1464 specifying the minimum field width; and a period followed by an
1465 optional integer specifying the minimum number of digits.
1466 The flags are @samp{-} for left-justification, @samp{'} for separating
1467 the digit into groups as specified by the @env{LC_NUMERIC} locale category,
1468 and @samp{0} for padding with zeros instead of spaces.
1469 For example, @samp{%5dN} prints the number of new lines in the group
1470 in a field of width 5 characters, using the @code{printf} format @code{"%5d"}.
1472 @item (@var{A}=@var{B}?@var{T}:@var{E})
1473 If @var{A} equals @var{B} then @var{T} else @var{E}.
1474 @var{A} and @var{B} are each either a decimal constant
1475 or a single letter interpreted as above.
1476 This format spec is equivalent to @var{T} if
1477 @var{A}'s value equals @var{B}'s; otherwise it is equivalent to @var{E}.
1479 For example, @samp{%(N=0?no:%dN) line%(N=1?:s)} is equivalent to
1480 @samp{no lines} if @var{N} (the number of lines in the group in the the
1481 new file) is 0, to @samp{1 line} if @var{N} is 1, and to @samp{%dN lines}
1486 @subsection Line Formats
1487 @cindex line formats
1489 Line formats control how each line taken from an input file is
1490 output as part of a line group in if-then-else format.
1492 For example, the following command outputs text with a one-character
1493 change indicator to the left of the text. The first character of output
1494 is @samp{-} for deleted lines, @samp{|} for added lines, and a space for
1495 unchanged lines. The formats contain newline characters where newlines
1496 are desired on output.
1500 --old-line-format='-%l
1502 --new-line-format='|%l
1504 --unchanged-line-format=' %l
1509 To specify a line format, use one of the following options. You should
1510 quote @var{format}, since it often contains shell metacharacters.
1513 @item --old-line-format=@var{format}
1514 formats lines just from the first file.
1516 @item --new-line-format=@var{format}
1517 formats lines just from the second file.
1519 @item --unchanged-line-format=@var{format}
1520 formats lines common to both files.
1522 @item --line-format=@var{format}
1523 formats all lines; in effect, it sets all three above options simultaneously.
1526 In a line format, ordinary characters represent themselves;
1527 conversion specifications start with @samp{%} and have one of the
1532 stands for the contents of the line, not counting its trailing
1533 newline (if any). This format ignores whether the line is incomplete;
1534 @xref{Incomplete Lines}.
1537 stands for the contents of the line, including its trailing newline
1538 (if any). If a line is incomplete, this format preserves its
1542 stands for @samp{%}.
1545 where @var{C} is a single character, stands for @var{C}.
1546 @var{C} may not be a backslash or an apostrophe.
1547 For example, @samp{%c':'} stands for a colon.
1550 where @var{O} is a string of 1, 2, or 3 octal digits,
1551 stands for the character with octal code @var{O}.
1552 For example, @samp{%c'\0'} stands for a null character.
1555 where @var{F} is a @code{printf} conversion specification,
1556 stands for the line number formatted with @var{F}.
1557 For example, @samp{%.5dn} prints the line number using the
1558 @code{printf} format @code{"%.5d"}. @xref{Line Group Formats}, for
1559 more about printf conversion specifications.
1563 The default line format is @samp{%l} followed by a newline character.
1565 If the input contains tab characters and it is important that they line
1566 up on output, you should ensure that @samp{%l} or @samp{%L} in a line
1567 format is just after a tab stop (e.g.@: by preceding @samp{%l} or
1568 @samp{%L} with a tab character), or you should use the @option{-t} or
1569 @option{--expand-tabs} option.
1571 Taken together, the line and line group formats let you specify many
1572 different formats. For example, the following command uses a format
1573 similar to normal @command{diff} format. You can tailor this command
1574 to get fine control over @command{diff} output.
1578 --old-line-format='< %l
1580 --new-line-format='> %l
1582 --old-group-format='%df%(f=l?:,%dl)d%dE
1584 --new-group-format='%dea%dF%(F=L?:,%dL)
1586 --changed-group-format='%df%(f=l?:,%dl)c%dF%(F=L?:,%dL)
1589 --unchanged-group-format='' \
1593 @node Detailed If-then-else
1594 @subsection Detailed Description of If-then-else Format
1596 For lines common to both files, @command{diff} uses the unchanged line
1597 group format. For each hunk of differences in the merged output
1598 format, if the hunk contains only lines from the first file,
1599 @command{diff} uses the old line group format; if the hunk contains only
1600 lines from the second file, @command{diff} uses the new group format;
1601 otherwise, @command{diff} uses the changed group format.
1603 The old, new, and unchanged line formats specify the output format of
1604 lines from the first file, lines from the second file, and lines common
1605 to both files, respectively.
1607 The option @option{--ifdef=@var{name}} is equivalent to
1608 the following sequence of options using shell syntax:
1611 --old-group-format='#ifndef @var{name}
1612 %<#endif /* ! @var{name} */
1614 --new-group-format='#ifdef @var{name}
1615 %>#endif /* @var{name} */
1617 --unchanged-group-format='%=' \
1618 --changed-group-format='#ifndef @var{name}
1619 %<#else /* @var{name} */
1620 %>#endif /* @var{name} */
1624 You should carefully check the @command{diff} output for proper nesting.
1625 For example, when using the @option{-D @var{name}} or
1626 @option{--ifdef=@var{name}} option, you should check that if the
1627 differing lines contain any of the C preprocessor directives
1628 @samp{#ifdef}, @samp{#ifndef}, @samp{#else}, @samp{#elif}, or
1629 @samp{#endif}, they are nested properly and match. If they don't, you
1630 must make corrections manually. It is a good idea to carefully check
1631 the resulting code anyway to make sure that it really does what you
1632 want it to; depending on how the input files were produced, the output
1633 might contain duplicate or otherwise incorrect code.
1635 The @command{patch} @option{-D @var{name}} option behaves like
1636 the @command{diff} @option{-D @var{name}} option, except it operates on
1637 a file and a diff to produce a merged file; @xref{patch Options}.
1639 @node Example If-then-else
1640 @subsection An Example of If-then-else Format
1642 Here is the output of @samp{diff -DTWO lao tzu} (@pxref{Sample
1643 diff Input}, for the complete contents of the two files):
1647 The Way that can be told of is not the eternal Way;
1648 The name that can be named is not the eternal name.
1650 The Nameless is the origin of Heaven and Earth;
1652 The Named is the mother of all things.
1654 The named is the mother of all things.
1657 Therefore let there always be non-being,
1658 so we may see their subtlety,
1659 And let there always be being,
1660 so we may see their outcome.
1661 The two are the same,
1662 But after they are produced,
1663 they have different names.
1665 They both may be called deep and profound.
1666 Deeper and more profound,
1667 The door of all subtleties!
1671 @node Incomplete Lines
1672 @chapter Incomplete Lines
1673 @cindex incomplete lines
1675 @cindex newline treatment by @command{diff}
1677 When an input file ends in a non-newline character, its last line is
1678 called an @dfn{incomplete line} because its last character is not a
1679 newline. All other lines are called @dfn{full lines} and end in a
1680 newline character. Incomplete lines do not match full lines unless
1681 differences in white space are ignored (@pxref{White Space}).
1683 An incomplete line is normally distinguished on output from a full line
1684 by a following line that starts with @samp{\}. However, the @sc{rcs} format
1685 (@pxref{RCS}) outputs the incomplete line as-is, without any trailing
1686 newline or following line. The side by side format normally represents
1687 incomplete lines as-is, but in some cases uses a @samp{\} or @samp{/}
1688 gutter marker; @xref{Side by Side}. The if-then-else line format
1689 preserves a line's incompleteness with @samp{%L}, and discards the
1690 newline with @samp{%l}; @xref{Line Formats}. Finally, with the
1691 @command{ed} and forward @command{ed} output formats (@pxref{Output Formats})
1692 @command{diff} cannot represent an incomplete line, so it pretends there
1693 was a newline and reports an error.
1695 For example, suppose @file{F} and @file{G} are one-byte files that
1696 contain just @samp{f} and @samp{g}, respectively. Then @samp{diff F G}
1702 \ No newline at end of file
1705 \ No newline at end of file
1709 (The exact message may differ in non-English locales.)
1710 @samp{diff -n F G} outputs the following without a trailing newline:
1719 @samp{diff -e F G} reports two errors and outputs the following:
1727 @node Comparing Directories
1728 @chapter Comparing Directories
1731 You can use @command{diff} to compare some or all of the files in two
1732 directory trees. When both file name arguments to @command{diff} are
1733 directories, it compares each file that is contained in both
1734 directories, examining file names in alphabetical order as specified by
1735 the @env{LC_COLLATE} locale category. Normally
1736 @command{diff} is silent about pairs of files that contain no differences,
1737 but if you use the @option{-s} or @option{--report-identical-files} option,
1738 it reports pairs of identical files. Normally @command{diff} reports
1739 subdirectories common to both directories without comparing
1740 subdirectories' files, but if you use the @option{-r} or
1741 @option{--recursive} option, it compares every corresponding pair of files
1742 in the directory trees, as many levels deep as they go.
1744 For file names that are in only one of the directories, @command{diff}
1745 normally does not show the contents of the file that exists; it reports
1746 only that the file exists in that directory and not in the other. You
1747 can make @command{diff} act as though the file existed but was empty in the
1748 other directory, so that it outputs the entire contents of the file that
1749 actually exists. (It is output as either an insertion or a
1750 deletion, depending on whether it is in the first or the second
1751 directory given.) To do this, use the @option{-N} or @option{--new-file}
1754 If the older directory contains one or more large files that are not in
1755 the newer directory, you can make the patch smaller by using the
1756 @option{--unidirectional-new-file} option instead of @option{-N}.
1757 This option is like @option{-N} except that it only inserts the contents
1758 of files that appear in the second directory but not the first (that is,
1759 files that were added). At the top of the patch, write instructions for
1760 the user applying the patch to remove the files that were deleted before
1761 applying the patch. @xref{Making Patches}, for more discussion of
1762 making patches for distribution.
1764 To ignore some files while comparing directories, use the @option{-x
1765 @var{pattern}} or @option{--exclude=@var{pattern}} option. This option
1766 ignores any files or subdirectories whose base names match the shell
1767 pattern @var{pattern}. Unlike in the shell, a period at the start of
1768 the base of a file name matches a wildcard at the start of a pattern.
1769 You should enclose @var{pattern} in quotes so that the shell does not
1770 expand it. For example, the option @option{-x '*.[ao]'} ignores any file
1771 whose name ends with @samp{.a} or @samp{.o}.
1773 This option accumulates if you specify it more than once. For example,
1774 using the options @option{-x 'RCS' -x '*,v'} ignores any file or
1775 subdirectory whose base name is @samp{RCS} or ends with @samp{,v}.
1777 If you need to give this option many times, you can instead put the
1778 patterns in a file, one pattern per line, and use the @option{-X
1779 @var{file}} or @option{--exclude-from=@var{file}} option.
1781 If you have been comparing two directories and stopped partway through,
1782 later you might want to continue where you left off. You can do this by
1783 using the @option{-S @var{file}} or @option{--starting-file=@var{file}}
1784 option. This compares only the file @var{file} and all alphabetically
1785 later files in the topmost directory level.
1787 If two directories differ only in that file names are lower case in
1788 one directory and upper case in the upper, @command{diff} normally
1789 reports many differences because it compares file names in a
1790 case sensitive way. With the @option{--ignore-file-name-case} option,
1791 @command{diff} ignores case differences in file names, so that for example
1792 the contents of the file @file{Tao} in one directory are compared to
1793 the contents of the file @file{TAO} in the other. The
1794 @option{--no-ignore-file-name-case} option cancels the effect of the
1795 @option{--ignore-file-name-case} option, reverting to the default
1798 If an @option{-x @var{pattern}}, @option{--exclude=@var{pattern}},
1799 @option{-X @var{file}}, or @option{--exclude-from=@var{file}} option
1800 is specified while the @option{--ignore-file-name-case} option is in
1801 effect, case is ignored when excluding file names matching the
1804 @node Adjusting Output
1805 @chapter Making @command{diff} Output Prettier
1807 @command{diff} provides several ways to adjust the appearance of its output.
1808 These adjustments can be applied to any output format.
1811 * Tabs:: Preserving the alignment of tab stops.
1812 * Pagination:: Page numbering and time-stamping @command{diff} output.
1816 @section Preserving Tab Stop Alignment
1817 @cindex tab stop alignment
1818 @cindex aligning tab stops
1820 The lines of text in some of the @command{diff} output formats are preceded
1821 by one or two characters that indicate whether the text is inserted,
1822 deleted, or changed. The addition of those characters can cause tabs to
1823 move to the next tab stop, throwing off the alignment of columns in the
1824 line. @sc{gnu} @command{diff} provides two ways to make tab-aligned columns
1827 The first way is to have @command{diff} convert all tabs into the correct
1828 number of spaces before outputting them; select this method with the
1829 @option{-t} or @option{--expand-tabs} option. @command{diff} assumes that
1830 tab stops are set every 8 print columns. To use this form of output with
1831 @command{patch}, you must give @command{patch} the @option{-l} or
1832 @option{--ignore-white-space} option (@pxref{Changed White Space}, for more
1835 The other method for making tabs line up correctly is to add a tab
1836 character instead of a space after the indicator character at the
1837 beginning of the line. This ensures that all following tab characters
1838 are in the same position relative to tab stops that they were in the
1839 original files, so that the output is aligned correctly. Its
1840 disadvantage is that it can make long lines too long to fit on one line
1841 of the screen or the paper. It also does not work with the unified
1842 output format, which does not have a space character after the change
1843 type indicator character. Select this method with the @option{-T} or
1844 @option{--initial-tab} option.
1847 @section Paginating @command{diff} Output
1848 @cindex paginating @command{diff} output
1850 It can be convenient to have long output page-numbered and time-stamped.
1851 The @option{-l} and @option{--paginate} options do this by sending the
1852 @command{diff} output through the @command{pr} program. Here is what the page
1853 header might look like for @samp{diff -lc lao tzu}:
1856 2002-02-22 14:20 diff -lc lao tzu Page 1
1859 @node diff Performance
1860 @chapter @command{diff} Performance Tradeoffs
1861 @cindex performance of @command{diff}
1863 @sc{gnu} @command{diff} runs quite efficiently; however, in some circumstances
1864 you can cause it to run faster or produce a more compact set of changes.
1866 One way to improve @command{diff} performance is to use hard or
1867 symbolic links to files instead of copies. This improves performance
1868 because @command{diff} normally does not need to read two hard or
1869 symbolic links to the same file, since their contents must be
1870 identical. For example, suppose you copy a large directory hierarchy,
1871 make a few changes to the copy, and then often use @samp{diff -r} to
1872 compare the original to the copy. If the original files are
1873 read-only, you can greatly improve performance by creating the copy
1874 using hard or symbolic links (e.g., with @sc{gnu} @samp{cp -lR} or
1875 @samp{cp -sR}). Before editing a file in the copy for the first time,
1876 you should break the link and replace it with a regular copy.
1878 You can also affect the performance of @sc{gnu} @command{diff} by
1879 giving it options that change the way it compares files.
1880 Performance has more than one dimension. These options improve one
1881 aspect of performance at the cost of another, or they improve
1882 performance in some cases while hurting it in others.
1884 The way that @sc{gnu} @command{diff} determines which lines have changed always
1885 comes up with a near-minimal set of differences. Usually it is good
1886 enough for practical purposes. If the @command{diff} output is large, you
1887 might want @command{diff} to use a modified algorithm that sometimes
1888 produces a smaller set of differences. The @option{-d} or
1889 @option{--minimal} option does this; however, it can also cause
1890 @command{diff} to run more slowly than usual, so it is not the default
1893 When the files you are comparing are large and have small groups of
1894 changes scattered throughout them, you can use the
1895 @option{--speed-large-files} option to make a different modification to
1896 the algorithm that @command{diff} uses. If the input files have a constant
1897 small density of changes, this option speeds up the comparisons without
1898 changing the output. If not, @command{diff} might produce a larger set of
1899 differences; however, the output will still be correct.
1901 Normally @command{diff} discards the prefix and suffix that is common to
1902 both files before it attempts to find a minimal set of differences.
1903 This makes @command{diff} run faster, but occasionally it may produce
1904 non-minimal output. The @option{--horizon-lines=@var{lines}} option
1905 prevents @command{diff} from discarding the last @var{lines} lines of the
1906 prefix and the first @var{lines} lines of the suffix. This gives
1907 @command{diff} further opportunities to find a minimal output.
1909 Suppose a run of changed lines includes a sequence of lines at one end
1910 and there is an identical sequence of lines just outside the other end.
1911 The @command{diff} command is free to choose which identical sequence is
1912 included in the hunk. In this case, @command{diff} normally shifts the
1913 hunk's boundaries when this merges adjacent hunks, or shifts a hunk's
1914 lines towards the end of the file. Merging hunks can make the output
1915 look nicer in some cases.
1917 @node Comparing Three Files
1918 @chapter Comparing Three Files
1919 @cindex comparing three files
1920 @cindex format of @command{diff3} output
1922 Use the program @command{diff3} to compare three files and show any
1923 differences among them. (@command{diff3} can also merge files; see
1924 @ref{diff3 Merging}).
1926 The ``normal'' @command{diff3} output format shows each hunk of
1927 differences without surrounding context. Hunks are labeled depending
1928 on whether they are two-way or three-way, and lines are annotated by
1929 their location in the input files.
1931 @xref{Invoking diff3}, for more information on how to run @command{diff3}.
1934 * Sample diff3 Input:: Sample @command{diff3} input for examples.
1935 * Detailed diff3 Normal:: A detailed description of normal output format.
1936 * diff3 Hunks:: The format of normal output format.
1937 * Example diff3 Normal:: Sample output in the normal format.
1940 @node Sample diff3 Input
1941 @section A Third Sample Input File
1942 @cindex @command{diff3} sample input
1943 @cindex sample input for @command{diff3}
1945 Here is a third sample file that will be used in examples to illustrate
1946 the output of @command{diff3} and how various options can change it. The
1947 first two files are the same that we used for @command{diff} (@pxref{Sample
1948 diff Input}). This is the third sample file, called @file{tao}:
1951 The Way that can be told of is not the eternal Way;
1952 The name that can be named is not the eternal name.
1953 The Nameless is the origin of Heaven and Earth;
1954 The named is the mother of all things.
1956 Therefore let there always be non-being,
1957 so we may see their subtlety,
1958 And let there always be being,
1959 so we may see their result.
1960 The two are the same,
1961 But after they are produced,
1962 they have different names.
1964 -- The Way of Lao-Tzu, tr. Wing-tsit Chan
1967 @node Detailed diff3 Normal
1968 @section Detailed Description of @command{diff3} Normal Format
1970 Each hunk begins with a line marked @samp{====}. Three-way hunks have
1971 plain @samp{====} lines, and two-way hunks have @samp{1}, @samp{2}, or
1972 @samp{3} appended to specify which of the three input files differ in
1973 that hunk. The hunks contain copies of two or three sets of input
1974 lines each preceded by one or two commands identifying where the lines
1977 Normally, two spaces precede each copy of an input line to distinguish
1978 it from the commands. But with the @option{-T} or @option{--initial-tab}
1979 option, @command{diff3} uses a tab instead of two spaces; this lines up
1980 tabs correctly. @xref{Tabs}, for more information.
1982 Commands take the following forms:
1985 @item @var{file}:@var{l}a
1986 This hunk appears after line @var{l} of file @var{file}, and
1987 contains no lines in that file. To edit this file to yield the other
1988 files, one must append hunk lines taken from the other files. For
1989 example, @samp{1:11a} means that the hunk follows line 11 in the first
1990 file and contains no lines from that file.
1992 @item @var{file}:@var{r}c
1993 This hunk contains the lines in the range @var{r} of file @var{file}.
1994 The range @var{r} is a comma-separated pair of line numbers, or just one
1995 number if the range is a singleton. To edit this file to yield the
1996 other files, one must change the specified lines to be the lines taken
1997 from the other files. For example, @samp{2:11,13c} means that the hunk
1998 contains lines 11 through 13 from the second file.
2001 If the last line in a set of input lines is incomplete
2002 (@pxref{Incomplete Lines}), it is distinguished on output from a full
2003 line by a following line that starts with @samp{\}.
2006 @section @command{diff3} Hunks
2007 @cindex hunks for @command{diff3}
2008 @cindex @command{diff3} hunks
2010 Groups of lines that differ in two or three of the input files are
2011 called @dfn{diff3 hunks}, by analogy with @command{diff} hunks
2012 (@pxref{Hunks}). If all three input files differ in a @command{diff3}
2013 hunk, the hunk is called a @dfn{three-way hunk}; if just two input files
2014 differ, it is a @dfn{two-way hunk}.
2016 As with @command{diff}, several solutions are possible. When comparing the
2017 files @samp{A}, @samp{B}, and @samp{C}, @command{diff3} normally finds
2018 @command{diff3} hunks by merging the two-way hunks output by the two
2019 commands @samp{diff A B} and @samp{diff A C}. This does not necessarily
2020 minimize the size of the output, but exceptions should be rare.
2022 For example, suppose @file{F} contains the three lines @samp{a},
2023 @samp{b}, @samp{f}, @file{G} contains the lines @samp{g}, @samp{b},
2024 @samp{g}, and @file{H} contains the lines @samp{a}, @samp{b},
2025 @samp{h}. @samp{diff3 F G H} might output the following:
2044 because it found a two-way hunk containing @samp{a} in the first and
2045 third files and @samp{g} in the second file, then the single line
2046 @samp{b} common to all three files, then a three-way hunk containing
2047 the last line of each file.
2049 @node Example diff3 Normal
2050 @section An Example of @command{diff3} Normal Format
2052 Here is the output of the command @samp{diff3 lao tzu tao}
2053 (@pxref{Sample diff3 Input}, for the complete contents of the files).
2054 Notice that it shows only the lines that are different among the three
2061 The Way that can be told of is not the eternal Way;
2062 The name that can be named is not the eternal name.
2066 The Named is the mother of all things.
2069 The named is the mother of all things.
2074 so we may see their outcome.
2076 so we may see their result.
2080 They both may be called deep and profound.
2081 Deeper and more profound,
2082 The door of all subtleties!
2085 -- The Way of Lao-Tzu, tr. Wing-tsit Chan
2089 @chapter Merging From a Common Ancestor
2090 @cindex merging from a common ancestor
2092 When two people have made changes to copies of the same file,
2093 @command{diff3} can produce a merged output that contains both sets of
2094 changes together with warnings about conflicts.
2096 One might imagine programs with names like @command{diff4} and @command{diff5}
2097 to compare more than three files simultaneously, but in practice the
2098 need rarely arises. You can use @command{diff3} to merge three or more
2099 sets of changes to a file by merging two change sets at a time.
2101 @command{diff3} can incorporate changes from two modified versions into a
2102 common preceding version. This lets you merge the sets of changes
2103 represented by the two newer files. Specify the common ancestor version
2104 as the second argument and the two newer versions as the first and third
2105 arguments, like this:
2108 diff3 @var{mine} @var{older} @var{yours}
2112 You can remember the order of the arguments by noting that they are in
2117 You can think of this as subtracting @var{older} from @var{yours} and
2118 adding the result to @var{mine}, or as merging into @var{mine} the
2119 changes that would turn @var{older} into @var{yours}. This merging is
2120 well-defined as long as @var{mine} and @var{older} match in the
2121 neighborhood of each such change. This fails to be true when all three
2122 input files differ or when only @var{older} differs; we call this
2123 a @dfn{conflict}. When all three input files differ, we call the
2124 conflict an @dfn{overlap}.
2126 @command{diff3} gives you several ways to handle overlaps and conflicts.
2127 You can omit overlaps or conflicts, or select only overlaps,
2128 or mark conflicts with special @samp{<<<<<<<} and @samp{>>>>>>>} lines.
2130 @command{diff3} can output the merge results as an @command{ed} script that
2131 that can be applied to the first file to yield the merged output.
2132 However, it is usually better to have @command{diff3} generate the merged
2133 output directly; this bypasses some problems with @command{ed}.
2136 * Which Changes:: Selecting changes to incorporate.
2137 * Marking Conflicts:: Marking conflicts.
2138 * Bypassing ed:: Generating merged output directly.
2139 * Merging Incomplete Lines:: How @command{diff3} merges incomplete lines.
2140 * Saving the Changed File:: Emulating System V behavior.
2144 @section Selecting Which Changes to Incorporate
2145 @cindex overlapping change, selection of
2146 @cindex unmerged change
2148 You can select all unmerged changes from @var{older} to @var{yours} for merging
2149 into @var{mine} with the @option{-e} or @option{--ed} option. You can
2150 select only the nonoverlapping unmerged changes with @option{-3} or
2151 @option{--easy-only}, and you can select only the overlapping changes with
2152 @option{-x} or @option{--overlap-only}.
2154 The @option{-e}, @option{-3} and @option{-x} options select only
2155 @dfn{unmerged changes}, i.e.@: changes where @var{mine} and @var{yours}
2156 differ; they ignore changes from @var{older} to @var{yours} where
2157 @var{mine} and @var{yours} are identical, because they assume that such
2158 changes have already been merged. If this assumption is not a safe
2159 one, you can use the @option{-A} or @option{--show-all} option
2160 (@pxref{Marking Conflicts}).
2162 Here is the output of the command @command{diff3} with each of these three
2163 options (@pxref{Sample diff3 Input}, for the complete contents of the files).
2164 Notice that @option{-e} outputs the union of the disjoint sets of changes
2165 output by @option{-3} and @option{-x}.
2167 Output of @samp{diff3 -e lao tzu tao}:
2171 -- The Way of Lao-Tzu, tr. Wing-tsit Chan
2174 so we may see their result.
2178 Output of @samp{diff3 -3 lao tzu tao}:
2181 so we may see their result.
2185 Output of @samp{diff3 -x lao tzu tao}:
2189 -- The Way of Lao-Tzu, tr. Wing-tsit Chan
2193 @node Marking Conflicts
2194 @section Marking Conflicts
2195 @cindex conflict marking
2196 @cindex @samp{<<<<<<<} for marking conflicts
2198 @command{diff3} can mark conflicts in the merged output by
2199 bracketing them with special marker lines. A conflict
2200 that comes from two files @var{A} and @var{B} is marked as follows:
2204 @r{lines from @var{A}}
2206 @r{lines from @var{B}}
2210 A conflict that comes from three files @var{A}, @var{B} and @var{C} is
2215 @r{lines from @var{A}}
2217 @r{lines from @var{B}}
2219 @r{lines from @var{C}}
2223 The @option{-A} or @option{--show-all} option acts like the @option{-e}
2224 option, except that it brackets conflicts, and it outputs all changes
2225 from @var{older} to @var{yours}, not just the unmerged changes. Thus,
2226 given the sample input files (@pxref{Sample diff3 Input}), @samp{diff3
2227 -A lao tzu tao} puts brackets around the conflict where only @file{tzu}
2233 The Way that can be told of is not the eternal Way;
2234 The name that can be named is not the eternal name.
2238 And it outputs the three-way conflict as follows:
2243 They both may be called deep and profound.
2244 Deeper and more profound,
2245 The door of all subtleties!
2248 -- The Way of Lao-Tzu, tr. Wing-tsit Chan
2252 The @option{-E} or @option{--show-overlap} option outputs less information
2253 than the @option{-A} or @option{--show-all} option, because it outputs only
2254 unmerged changes, and it never outputs the contents of the second
2255 file. Thus the @option{-E} option acts like the @option{-e} option,
2256 except that it brackets the first and third files from three-way
2257 overlapping changes. Similarly, @option{-X} acts like @option{-x}, except
2258 it brackets all its (necessarily overlapping) changes. For example,
2259 for the three-way overlapping change above, the @option{-E} and @option{-X}
2260 options output the following:
2266 -- The Way of Lao-Tzu, tr. Wing-tsit Chan
2270 If you are comparing files that have meaningless or uninformative names,
2271 you can use the @option{-L @var{label}} or @option{--label=@var{label}}
2272 option to show alternate names in the @samp{<<<<<<<}, @samp{|||||||}
2273 and @samp{>>>>>>>} brackets. This option can be given up to three
2274 times, once for each input file. Thus @samp{diff3 -A -L X -L Y -L Z A
2275 B C} acts like @samp{diff3 -A A B C}, except that the output looks like
2276 it came from files named @samp{X}, @samp{Y} and @samp{Z} rather than
2277 from files named @samp{A}, @samp{B} and @samp{C}.
2280 @section Generating the Merged Output Directly
2281 @cindex merged @command{diff3} format
2283 With the @option{-m} or @option{--merge} option, @command{diff3} outputs the
2284 merged file directly. This is more efficient than using @command{ed} to
2285 generate it, and works even with non-text files that @command{ed} would
2286 reject. If you specify @option{-m} without an @command{ed} script option,
2287 @option{-A} (@option{--show-all}) is assumed.
2289 For example, the command @samp{diff3 -m lao tzu tao}
2290 (@pxref{Sample diff3 Input} for a copy of the input files) would output
2296 The Way that can be told of is not the eternal Way;
2297 The name that can be named is not the eternal name.
2299 The Nameless is the origin of Heaven and Earth;
2300 The Named is the mother of all things.
2301 Therefore let there always be non-being,
2302 so we may see their subtlety,
2303 And let there always be being,
2304 so we may see their result.
2305 The two are the same,
2306 But after they are produced,
2307 they have different names.
2310 They both may be called deep and profound.
2311 Deeper and more profound,
2312 The door of all subtleties!
2315 -- The Way of Lao-Tzu, tr. Wing-tsit Chan
2319 @node Merging Incomplete Lines
2320 @section How @command{diff3} Merges Incomplete Lines
2321 @cindex incomplete line merging
2323 With @option{-m}, incomplete lines (@pxref{Incomplete Lines}) are simply
2324 copied to the output as they are found; if the merged output ends in an
2325 conflict and one of the input files ends in an incomplete
2326 line, succeeding @samp{|||||||}, @samp{=======} or @samp{>>>>>>>}
2327 brackets appear somewhere other than the start of a line because
2328 they are appended to the incomplete line.
2330 Without @option{-m}, if an @command{ed} script option is specified and an
2331 incomplete line is found, @command{diff3} generates a warning and acts as
2332 if a newline had been present.
2334 @node Saving the Changed File
2335 @section Saving the Changed File
2336 @cindex System V @command{diff3} compatibility
2338 Traditional Unix @command{diff3} generates an @command{ed} script without the
2339 trailing @samp{w} and @samp{q} commands that save the changes.
2340 System V @command{diff3} generates these extra commands. @sc{gnu}
2341 @command{diff3} normally behaves like traditional Unix
2342 @command{diff3}, but with the @option{-i} option it behaves like
2343 System V @command{diff3} and appends the @samp{w} and @samp{q}
2346 The @option{-i} option requires one of the @command{ed} script options
2347 @option{-AeExX3}, and is incompatible with the merged output option
2350 @node Interactive Merging
2351 @chapter Interactive Merging with @command{sdiff}
2352 @cindex diff merging
2353 @cindex interactive merging
2355 With @command{sdiff}, you can merge two files interactively based on a
2356 side-by-side @option{-y} format comparison (@pxref{Side by Side}). Use
2357 @option{-o @var{file}} or @option{--output=@var{file}} to specify where to
2358 put the merged text. @xref{Invoking sdiff}, for more details on the
2359 options to @command{sdiff}.
2361 Another way to merge files interactively is to use the Emacs Lisp
2362 package @command{emerge}. @xref{emerge, , emerge, emacs, The @sc{gnu} Emacs
2363 Manual}, for more information.
2366 * sdiff Option Summary:: Summary of @command{sdiff} options.
2367 * Merge Commands:: Merging two files interactively.
2370 @node sdiff Option Summary
2371 @section Specifying @command{diff} Options to @command{sdiff}
2372 @cindex @command{sdiff} output format
2374 The following @command{sdiff} options have the same meaning as for
2375 @command{diff}. @xref{diff Options}, for the use of these options.
2379 -B -E -I @var{regexp}
2381 --ignore-blank-lines --ignore-case
2382 --ignore-matching-lines=@var{regexp} --ignore-space-change
2383 --ignore-tab-expansion
2384 --left-column --minimal --speed-large-files
2385 --strip-trailing-cr --suppress-common-lines --expand-tabs
2386 --text --version --width=@var{columns}
2389 For historical reasons, @command{sdiff} has alternate names for some
2390 options. The @option{-l} option is equivalent to the
2391 @option{--left-column} option, and similarly @option{-s} is equivalent
2392 to @option{--suppress-common-lines}. The meaning of the @command{sdiff}
2393 @option{-w} and @option{-W} options is interchanged from that of
2394 @command{diff}: with @command{sdiff}, @option{-w @var{columns}} is
2395 equivalent to @option{--width=@var{columns}}, and @option{-W} is
2396 equivalent to @option{--ignore-all-space}. @command{sdiff} without the
2397 @option{-o} option is equivalent to @command{diff} with the @option{-y}
2398 or @option{--side-by-side} option (@pxref{Side by Side}).
2400 @node Merge Commands
2401 @section Merge Commands
2402 @cindex merge commands
2403 @cindex merging interactively
2405 Groups of common lines, with a blank gutter, are copied from the first
2406 file to the output. After each group of differing lines, @command{sdiff}
2407 prompts with @samp{%} and pauses, waiting for one of the following
2408 commands. Follow each command with @key{RET}.
2412 Discard both versions.
2413 Invoke a text editor on an empty temporary file,
2414 then copy the resulting file to the output.
2417 Concatenate the two versions, edit the result in a temporary file,
2418 then copy the edited result to the output.
2421 Like @samp{eb}, except precede each version with a header that
2422 shows what file and lines the version came from.
2425 Edit a copy of the left version, then copy the result to the output.
2428 Edit a copy of the right version, then copy the result to the output.
2431 Copy the left version to the output.
2437 Copy the right version to the output.
2440 Silently copy common lines.
2443 Verbosely copy common lines. This is the default.
2447 The text editor invoked is specified by the @env{EDITOR} environment
2448 variable if it is set. The default is system-dependent.
2450 @node Merging with patch
2451 @chapter Merging with @command{patch}
2453 @command{patch} takes comparison output produced by @command{diff} and applies
2454 the differences to a copy of the original file, producing a patched
2455 version. With @command{patch}, you can distribute just the changes to a
2456 set of files instead of distributing the entire file set; your
2457 correspondents can apply @command{patch} to update their copy of the files
2458 with your changes. @command{patch} automatically determines the diff
2459 format, skips any leading or trailing headers, and uses the headers to
2460 determine which file to patch. This lets your correspondents feed a
2461 mail message containing a difference listing directly to
2464 @command{patch} detects and warns about common problems like forward
2465 patches. It saves any patches that it could not apply. It can also maintain a
2466 @code{patchlevel.h} file to ensure that your correspondents apply
2467 diffs in the proper order.
2469 @command{patch} accepts a series of diffs in its standard input, usually
2470 separated by headers that specify which file to patch. It applies
2471 @command{diff} hunks (@pxref{Hunks}) one by one. If a hunk does not
2472 exactly match the original file, @command{patch} uses heuristics to try to
2473 patch the file as well as it can. If no approximate match can be found,
2474 @command{patch} rejects the hunk and skips to the next hunk. @command{patch}
2475 normally replaces each file @var{f} with its new version, putting reject
2476 hunks (if any) into @samp{@var{f}.rej}.
2478 @xref{Invoking patch}, for detailed information on the options to
2482 * patch Input:: Selecting the type of @command{patch} input.
2483 * Revision Control:: Getting files from @sc{rcs}, @sc{sccs}, etc.
2484 * Imperfect:: Dealing with imperfect patches.
2485 * Creating and Removing:: Creating and removing files with a patch.
2486 * Patching Time Stamps:: Updating time stamps on patched files.
2487 * Multiple Patches:: Handling multiple patches in a file.
2488 * patch Directories:: Changing directory and stripping directories.
2489 * Backups:: Whether backup files are made.
2490 * Backup Names:: Backup file names.
2491 * Reject Names:: Reject file names.
2492 * patch Messages:: Messages and questions @command{patch} can produce.
2493 * patch and POSIX:: Conformance to the @sc{posix} standard.
2494 * patch and Tradition:: @sc{gnu} versus traditional @command{patch}.
2498 @section Selecting the @command{patch} Input Format
2499 @cindex @command{patch} input format
2501 @command{patch} normally determines which @command{diff} format the patch
2502 file uses by examining its contents. For patch files that contain
2503 particularly confusing leading text, you might need to use one of the
2504 following options to force @command{patch} to interpret the patch file as a
2505 certain format of diff. The output formats listed here are the only
2506 ones that @command{patch} can understand.
2515 @command{ed} script.
2526 @node Revision Control
2527 @section Revision Control
2528 @cindex revision control
2529 @cindex version control
2534 If a nonexistent input file is under a revision control system
2535 supported by @command{patch}, @command{patch} normally asks the user
2536 whether to get (or check out) the file from the revision control
2537 system. Patch currently supports @sc{rcs}, ClearCase and @sc{sccs}.
2538 Under @sc{rcs} and @sc{sccs}, @command{patch} also asks when the input
2539 file is read-only and matches the default version in the revision
2543 The @option{-g @var{num}} or @option{--get=@var{num}} affects access
2544 to files under supported revision control systems. If @var{num} is
2545 positive, @command{patch} gets the file without asking the user; if
2546 zero, @command{patch} neither asks the user nor gets the file; and if
2547 negative, @command{patch} asks the user before getting the file. The
2548 default value of @var{num} is given by the value of the
2549 @env{PATCH_GET} environment variable if it is set; if not, the default
2550 value is zero if @command{patch} is conforming to @sc{posix}, negative
2551 otherwise. @xref{patch and POSIX}.
2553 @vindex VERSION_CONTROL
2554 The choice of revision control system is unaffected by the
2555 @env{VERSION_CONTROL} environment variable (@pxref{Backup Names}).
2558 @section Applying Imperfect Patches
2559 @cindex imperfect patch application
2561 @command{patch} tries to skip any leading text in the patch file,
2562 apply the diff, and then skip any trailing text. Thus you can feed a
2563 mail message directly to @command{patch}, and it should work. If the
2564 entire diff is indented by a constant amount of white space,
2565 @command{patch} automatically ignores the indentation. If a context
2566 diff contains trailing carriage return on each line, @command{patch}
2567 automatically ignores the carriage return. If a context diff has been
2568 encapsulated by prepending @w{@samp{- }} to lines beginning with @samp{-}
2569 as per @uref{ftp://ftp.isi.edu/in-notes/rfc934.txt, Internet RFC 934},
2570 @command{patch} automatically unencapsulates the input.
2572 However, certain other types of imperfect input require user
2573 intervention or testing.
2576 * Changed White Space:: When tabs and spaces don't match exactly.
2577 * Reversed Patches:: Applying reversed patches correctly.
2578 * Inexact:: Helping @command{patch} find close matches.
2579 * Dry Runs:: Predicting what @command{patch} will do.
2582 @node Changed White Space
2583 @subsection Applying Patches with Changed White Space
2584 @cindex white space in patches
2586 Sometimes mailers, editors, or other programs change spaces into tabs,
2587 or vice versa. If this happens to a patch file or an input file, the
2588 files might look the same, but @command{patch} will not be able to match
2589 them properly. If this problem occurs, use the @option{-l} or
2590 @option{--ignore-white-space} option, which makes @command{patch} compare
2591 blank characters (i.e.@: spaces and tabs) loosely so that any nonempty
2592 sequence of blanks in the patch file matches any nonempty sequence of
2593 blanks in the input files. Non-blank
2594 characters must still match exactly. Each line of the context must
2595 still match a line in the input file.
2597 @node Reversed Patches
2598 @subsection Applying Reversed Patches
2599 @cindex reversed patches
2601 Sometimes people run @command{diff} with the new file first instead of
2602 second. This creates a diff that is ``reversed''. To apply such
2603 patches, give @command{patch} the @option{-R} or @option{--reverse} option.
2604 @command{patch} then attempts to swap each hunk around before applying it.
2605 Rejects come out in the swapped format.
2607 Often @command{patch} can guess that the patch is reversed. If the first
2608 hunk of a patch fails, @command{patch} reverses the hunk to see if it can
2609 apply it that way. If it can, @command{patch} asks you if you want to have
2610 the @option{-R} option set; if it can't, @command{patch} continues to apply
2611 the patch normally. This method cannot detect a reversed patch if it is
2612 a normal diff and the first command is an append (which should have been
2613 a delete) since appends always succeed, because a null context matches
2614 anywhere. But most patches add or change lines rather than delete them,
2615 so most reversed normal diffs begin with a delete, which fails, and
2616 @command{patch} notices.
2618 If you apply a patch that you have already applied, @command{patch} thinks
2619 it is a reversed patch and offers to un-apply the patch. This could be
2620 construed as a feature. If you did this inadvertently and you don't
2621 want to un-apply the patch, just answer @samp{n} to this offer and to
2622 the subsequent ``apply anyway'' question---or type @kbd{C-c} to kill the
2623 @command{patch} process.
2626 @subsection Helping @command{patch} Find Inexact Matches
2627 @cindex inexact patches
2628 @cindex fuzz factor when patching
2630 For context diffs, and to a lesser extent normal diffs, @command{patch} can
2631 detect when the line numbers mentioned in the patch are incorrect, and
2632 it attempts to find the correct place to apply each hunk of the patch.
2633 As a first guess, it takes the line number mentioned in the hunk, plus
2634 or minus any offset used in applying the previous hunk. If that is not
2635 the correct place, @command{patch} scans both forward and backward for a
2636 set of lines matching the context given in the hunk.
2638 First @command{patch} looks for a place where all lines of the context
2639 match. If it cannot find such a place, and it is reading a context or
2640 unified diff, and the maximum fuzz factor is set to 1 or more, then
2641 @command{patch} makes another scan, ignoring the first and last line of
2642 context. If that fails, and the maximum fuzz factor is set to 2 or
2643 more, it makes another scan, ignoring the first two and last two lines
2644 of context are ignored. It continues similarly if the maximum fuzz
2647 The @option{-F @var{lines}} or @option{--fuzz=@var{lines}} option sets the
2648 maximum fuzz factor to @var{lines}. This option only applies to context
2649 and unified diffs; it ignores up to @var{lines} lines while looking for
2650 the place to install a hunk. Note that a larger fuzz factor increases
2651 the odds of making a faulty patch. The default fuzz factor is 2; there
2652 is no point to setting it to more than the number of lines of context
2653 in the diff, ordinarily 3.
2655 If @command{patch} cannot find a place to install a hunk of the patch, it
2656 writes the hunk out to a reject file (@pxref{Reject Names}, for information
2657 on how reject files are named). It writes out rejected hunks in context
2658 format no matter what form the input patch is in. If the input is a
2659 normal or @command{ed} diff, many of the contexts are simply null. The
2660 line numbers on the hunks in the reject file may be different from those
2661 in the patch file: they show the approximate location where @command{patch}
2662 thinks the failed hunks belong in the new file rather than in the old
2665 If the @option{--verbose} option is given, then
2666 as it completes each hunk @command{patch} tells you whether the hunk
2667 succeeded or failed, and if it failed, on which line (in the new file)
2668 @command{patch} thinks the hunk should go. If this is different from the
2669 line number specified in the diff, it tells you the offset. A single
2670 large offset @emph{may} indicate that @command{patch} installed a hunk in
2671 the wrong place. @command{patch} also tells you if it used a fuzz factor
2672 to make the match, in which case you should also be slightly suspicious.
2674 @command{patch} cannot tell if the line numbers are off in an @command{ed}
2675 script, and can only detect wrong line numbers in a normal diff when it
2676 finds a change or delete command. It may have the same problem with a
2677 context diff using a fuzz factor equal to or greater than the number of
2678 lines of context shown in the diff (typically 3). In these cases, you
2679 should probably look at a context diff between your original and patched
2680 input files to see if the changes make sense. Compiling without errors
2681 is a pretty good indication that the patch worked, but not a guarantee.
2683 A patch against an empty file applies to a nonexistent file, and vice
2684 versa. @xref{Creating and Removing}.
2686 @command{patch} usually produces the correct results, even when it must
2687 make many guesses. However, the results are guaranteed only when
2688 the patch is applied to an exact copy of the file that the patch was
2692 @subsection Predicting what @command{patch} will do
2693 @cindex testing @command{patch}
2694 @cindex dry runs for @command{patch}
2696 It may not be obvious in advance what @command{patch} will do with a
2697 complicated or poorly formatted patch. If you are concerned that the
2698 input might cause @command{patch} to modify the wrong files, you can
2699 use the @option{--dry-run} option, which causes @command{patch} to
2700 print the results of applying patches without actually changing any
2701 files. You can then inspect the diagnostics generated by the dry run
2702 to see whether @command{patch} will modify the files that you expect.
2703 If the patch does not do what you want, you can modify the patch (or
2704 the other options to @command{patch}) and try another dry run. Once
2705 you are satisfied with the proposed patch you can apply it by invoking
2706 @command{patch} as before, but this time without the
2707 @option{--dry-run} option.
2709 @node Creating and Removing
2710 @section Creating and Removing Files
2711 @cindex creating files
2712 @cindex empty files, removing
2713 @cindex removing empty files
2715 Sometimes when comparing two directories, a file may exist in one
2716 directory but not the other. If you give @command{diff} the
2717 @option{-N} or @option{--new-file} option, or if you supply an old or
2718 new file that is named @file{/dev/null} or is empty and is dated the
2719 Epoch (1970-01-01 00:00:00 UTC), @command{diff} outputs a patch that
2720 adds or deletes the contents of this file. When given such a patch,
2721 @command{patch} normally creates a new file or removes the old file.
2722 However, when conforming to @sc{posix} (@pxref{patch and POSIX}),
2723 @command{patch} does not remove the old file, but leaves it empty.
2724 The @option{-E} or @option{--remove-empty-files} option causes
2725 @command{patch} to remove output files that are empty after applying a
2726 patch, even if the patch does not appear to be one that removed the
2729 If the patch appears to create a file that already exists,
2730 @command{patch} asks for confirmation before applying the patch.
2732 @node Patching Time Stamps
2733 @section Updating Time Stamps on Patched Files
2734 @cindex time stamps on patched files
2736 When @command{patch} updates a file, it normally sets the file's
2737 last-modified time stamp to the current time of day. If you are using
2738 @command{patch} to track a software distribution, this can cause
2739 @command{make} to incorrectly conclude that a patched file is out of
2740 date. For example, if @file{syntax.c} depends on @file{syntax.y}, and
2741 @command{patch} updates @file{syntax.c} and then @file{syntax.y}, then
2742 @file{syntax.c} will normally appear to be out of date with respect to
2743 @file{syntax.y} even though its contents are actually up to date.
2745 The @option{-Z} or @option{--set-utc} option causes @command{patch} to
2746 set a patched file's modification and access times to the time stamps
2747 given in context diff headers. If the context diff headers do not
2748 specify a time zone, they are assumed to use Coordinated Universal
2749 Time (@sc{utc}, often known as @sc{gmt}).
2751 The @option{-T} or @option{--set-time} option acts like @option{-Z} or
2752 @option{--set-utc}, except that it assumes that the context diff
2753 headers' time stamps use local time instead of @sc{utc}. This option
2754 is not recommended, because patches using local time cannot easily be
2755 used by people in other time zones, and because local time stamps are
2756 ambiguous when local clocks move backwards during daylight-saving time
2757 adjustments. If the context diff headers specify a time zone, this
2758 option is equivalent to @option{-Z} or @option{--set-utc}.
2760 @command{patch} normally refrains from setting a file's time stamps if
2761 the file's original last-modified time stamp does not match the time
2762 given in the diff header, of if the file's contents do not exactly
2763 match the patch. However, if the @option{-f} or @option{--force}
2764 option is given, the file's time stamps are set regardless.
2766 Due to the limitations of the current @command{diff} format,
2767 @command{patch} cannot update the times of files whose contents have
2768 not changed. Also, if you set file time stamps to values other than
2769 the current time of day, you should also remove (e.g., with @samp{make
2770 clean}) all files that depend on the patched files, so that later
2771 invocations of @command{make} do not get confused by the patched
2774 @node Multiple Patches
2775 @section Multiple Patches in a File
2776 @cindex multiple patches
2777 @cindex intuiting file names from patches
2779 If the patch file contains more than one patch, and if you do not
2780 specify an input file on the command line, @command{patch} tries to
2781 apply each patch as if they came from separate patch files. This
2782 means that it determines the name of the file to patch for each patch,
2783 and that it examines the leading text before each patch for file names
2784 and prerequisite revision level (@pxref{Making Patches}, for more on
2787 @command{patch} uses the following rules to intuit a file name from
2788 the leading text before a patch. First, @command{patch} takes an
2789 ordered list of candidate file names as follows:
2793 If the header is that of a context diff, @command{patch} takes the old
2794 and new file names in the header. A name is ignored if it does not
2795 have enough slashes to satisfy the @option{-p@var{num}} or
2796 @option{--strip=@var{num}} option. The name @file{/dev/null} is also
2800 If there is an @samp{Index:} line in the leading garbage and if either
2801 the old and new names are both absent or if @command{patch} is
2802 conforming to @sc{posix}, @command{patch} takes the name in the
2806 For the purpose of the following rules, the candidate file names are
2807 considered to be in the order (old, new, index), regardless of the
2808 order that they appear in the header.
2812 Then @command{patch} selects a file name from the candidate list as
2817 If some of the named files exist, @command{patch} selects the first
2818 name if conforming to @sc{posix}, and the best name otherwise.
2821 If @command{patch} is not ignoring @sc{rcs}, ClearCase, and @sc{sccs}
2822 (@pxref{Revision Control}), and no named files exist but an @sc{rcs},
2823 ClearCase, or @sc{sccs} master is found, @command{patch} selects the
2824 first named file with an @sc{rcs}, ClearCase, or @sc{sccs} master.
2827 If no named files exist, no @sc{rcs}, ClearCase, or @sc{sccs} master
2828 was found, some names are given, @command{patch} is not conforming to
2829 @sc{posix}, and the patch appears to create a file, @command{patch}
2830 selects the best name requiring the creation of the fewest
2834 If no file name results from the above heuristics, you are asked for
2835 the name of the file to patch, and @command{patch} selects that name.
2838 To determine the @dfn{best} of a nonempty list of file names,
2839 @command{patch} first takes all the names with the fewest path name
2840 components; of those, it then takes all the names with the shortest
2841 basename; of those, it then takes all the shortest names; finally, it
2842 takes the first remaining name.
2844 @xref{patch and POSIX}, to see whether @command{patch} is conforming
2847 @node patch Directories
2848 @section Applying Patches in Other Directories
2849 @cindex directories and patch
2850 @cindex patching directories
2852 The @option{-d @var{directory}} or @option{--directory=@var{directory}}
2853 option to @command{patch} makes directory @var{directory} the current
2854 directory for interpreting both file names in the patch file, and file
2855 names given as arguments to other options (such as @option{-B} and
2856 @option{-o}). For example, while in a mail reading program, you can patch
2857 a file in the @file{/usr/src/emacs} directory directly from a message
2858 containing the patch like this:
2861 | patch -d /usr/src/emacs
2864 Sometimes the file names given in a patch contain leading directories,
2865 but you keep your files in a directory different from the one given in
2866 the patch. In those cases, you can use the
2867 @option{-p@var{number}} or @option{--strip=@var{number}}
2868 option to set the file name strip count to @var{number}. The strip
2869 count tells @command{patch} how many slashes, along with the directory
2870 names between them, to strip from the front of file names. A sequence
2871 of one or more adjacent slashes is counted as a single slash. By
2872 default, @command{patch} strips off all leading directories, leaving
2873 just the base file names.
2875 For example, suppose the file name in the patch file is
2876 @file{/gnu/src/emacs/etc/NEWS}. Using @option{-p0} gives the
2877 entire file name unmodified, @option{-p1} gives
2878 @file{gnu/src/emacs/etc/NEWS} (no leading slash), @option{-p4} gives
2879 @file{etc/NEWS}, and not specifying @option{-p} at all gives @file{NEWS}.
2881 @command{patch} looks for each file (after any slashes have been stripped)
2882 in the current directory, or if you used the @option{-d @var{directory}}
2883 option, in that directory.
2886 @section Backup Files
2887 @cindex backup file strategy
2889 Normally, @command{patch} creates a backup file if the patch does not
2890 exactly match the original input file, because in that case the
2891 original data might not be recovered if you undo the patch with
2892 @samp{patch -R} (@pxref{Reversed Patches}). However, when conforming
2893 to @sc{posix}, @command{patch} does not create backup files by
2894 default. @xref{patch and POSIX}.
2896 The @option{-b} or @option{--backup} option causes @command{patch} to
2897 make a backup file regardless of whether the patch matches the
2898 original input. The @option{--backup-if-mismatch} option causes
2899 @command{patch} to create backup files for mismatches files; this is
2900 the default when not conforming to @sc{posix}. The
2901 @option{--no-backup-if-mismatch} option causes @command{patch} to not
2902 create backup files, even for mismatched patches; this is the default
2903 when conforming to @sc{posix}.
2905 When backing up a file that does not exist, an empty, unreadable
2906 backup file is created as a placeholder to represent the nonexistent
2910 @section Backup File Names
2911 @cindex backup file names
2913 Normally, @command{patch} renames an original input file into a backup
2914 file by appending to its name the extension @samp{.orig}, or @samp{~}
2915 if using @samp{.orig} would make the backup file name too
2916 long.@footnote{A coding error in @sc{gnu} @command{patch} version
2917 2.5.4 causes it to always use @samp{~}, but this should be fixed in
2918 the next release.} The @option{-z @var{backup-suffix}} or
2919 @option{--suffix=@var{backup-suffix}} option causes @command{patch} to
2920 use @var{backup-suffix} as the backup extension instead.
2922 @vindex SIMPLE_BACKUP_SUFFIX
2923 Alternately, you can specify the extension for backup files with the
2924 @env{SIMPLE_BACKUP_SUFFIX} environment variable, which the options
2927 @command{patch} can also create numbered backup files the way @sc{gnu} Emacs
2928 does. With this method, instead of having a single backup of each file,
2929 @command{patch} makes a new backup file name each time it patches a file.
2930 For example, the backups of a file named @file{sink} would be called,
2931 successively, @file{sink.~1~}, @file{sink.~2~}, @file{sink.~3~}, etc.
2933 @vindex PATCH_VERSION_CONTROL
2934 @vindex VERSION_CONTROL
2935 The @option{-V @var{backup-style}} or
2936 @option{--version-control=@var{backup-style}} option takes as an
2937 argument a method for creating backup file names. You can alternately
2938 control the type of backups that @command{patch} makes with the
2939 @env{PATCH_VERSION_CONTROL} environment variable, which the
2940 @option{-V} option overrides. If @env{PATCH_VERSION_CONTROL} is not
2941 set, the @env{VERSION_CONTROL} environment variable is used instead.
2942 Please note that these options and variables control backup file
2943 names; they do not affect the choice of revision control system
2944 (@pxref{Revision Control}).
2946 The values of these environment variables and the argument to the
2947 @option{-V} option are like the @sc{gnu} Emacs @code{version-control}
2948 variable (@pxref{Backup Names, , , emacs, The @sc{gnu} Emacs Manual},
2949 for more information on backup versions in Emacs). They also
2950 recognize synonyms that are more descriptive. The valid values are
2951 listed below; unique abbreviations are acceptable.
2956 Always make numbered backups.
2960 Make numbered backups of files that already have them, simple backups of
2961 the others. This is the default.
2965 Always make simple backups.
2968 You can also tell @command{patch} to prepend a prefix, such as a
2969 directory name, to produce backup file names. The @option{-B
2970 @var{prefix}} or @option{--prefix=@var{prefix}} option makes backup
2971 files by prepending @var{prefix} to them. The @option{-Y
2972 @var{prefix}} or @option{--basename-prefix=@var{prefix}} prepends
2973 @var{prefix} to the last file name component of backup file names
2974 instead; for example, @option{-Y ~} causes the backup name for
2975 @file{dir/file.c} to be @file{dir/~file.c}. If you use either of
2976 these prefix options, the suffix-based options are ignored.
2978 If you specify the output file with the @option{-o} option, that file is
2979 the one that is backed up, not the input file.
2981 Options that affect the names of backup files do not affect whether
2982 backups are made. For example, if you specify the
2983 @option{--no-backup-if-mismatch} option, none of the options described
2984 in this section have any affect, because no backups are made.
2987 @section Reject File Names
2988 @cindex reject file names
2990 The names for reject files (files containing patches that
2991 @command{patch} could not find a place to apply) are normally the name
2992 of the output file with @samp{.rej} appended (or @samp{#} if if using
2993 @samp{.rej} would make the backup file name too long).
2995 Alternatively, you can tell @command{patch} to place all of the rejected
2996 patches in a single file. The @option{-r @var{reject-file}} or
2997 @option{--reject-file=@var{reject-file}} option uses @var{reject-file} as
2998 the reject file name.
3000 @node patch Messages
3001 @section Messages and Questions from @command{patch}
3002 @cindex @command{patch} messages and questions
3003 @cindex diagnostics from @command{patch}
3004 @cindex messages from @command{patch}
3006 @command{patch} can produce a variety of messages, especially if it
3007 has trouble decoding its input. In a few situations where it's not
3008 sure how to proceed, @command{patch} normally prompts you for more
3009 information from the keyboard. There are options to produce more or
3010 fewer messages, to have it not ask for keyboard input, and to
3011 affect the way that file names are quoted in messages.
3014 * More or Fewer Messages:: Controlling the verbosity of @command{patch}.
3015 * patch and Keyboard Input:: Inhibiting keyboard input.
3016 * patch Quoting Style:: Quoting file names in diagnostics.
3019 @command{patch} exits with status 0 if all hunks are applied successfully,
3020 1 if some hunks cannot be applied, and 2 if there is more serious trouble.
3021 When applying a set of patches in a loop, you should check the
3022 exit status, so you don't apply a later patch to a partially patched
3025 @node More or Fewer Messages
3026 @subsection Controlling the Verbosity of @command{patch}
3027 @cindex verbose messages from @command{patch}
3028 @cindex inhibit messages from @command{patch}
3030 You can cause @command{patch} to produce more messages by using the
3031 @option{--verbose} option. For example, when you give this option,
3032 the message @samp{Hmm...} indicates that @command{patch} is reading text in
3033 the patch file, attempting to determine whether there is a patch in that
3034 text, and if so, what kind of patch it is.
3036 You can inhibit all terminal output from @command{patch}, unless an error
3037 occurs, by using the @option{-s}, @option{--quiet}, or @option{--silent}
3040 @node patch and Keyboard Input
3041 @subsection Inhibiting Keyboard Input
3042 @cindex keyboard input to @command{patch}
3044 There are two ways you can prevent @command{patch} from asking you any
3045 questions. The @option{-f} or @option{--force} option assumes that you know
3046 what you are doing. It causes @command{patch} to do the following:
3050 Skip patches that do not contain file names in their headers.
3053 Patch files even though they have the wrong version for the
3054 @samp{Prereq:} line in the patch;
3057 Assume that patches are not reversed even if they look like they are.
3061 The @option{-t} or @option{--batch} option is similar to @option{-f}, in that
3062 it suppresses questions, but it makes somewhat different assumptions:
3066 Skip patches that do not contain file names in their headers
3067 (the same as @option{-f}).
3070 Skip patches for which the file has the wrong version for the
3071 @samp{Prereq:} line in the patch;
3074 Assume that patches are reversed if they look like they are.
3077 @node patch Quoting Style
3078 @subsection @command{patch} Quoting Style
3079 @cindex quoting style
3081 When @command{patch} outputs a file name in a diagnostic message, it
3082 can format the name in any of several ways. This can be useful to
3083 output file names unambiguously, even if they contain punctuation or
3084 special characters like newlines. The
3085 @option{--quoting-style=@var{word}} option controls how names are
3086 output. The @var{word} should be one of the following:
3092 Quote names for the shell if they contain shell metacharacters or would
3093 cause ambiguous output.
3095 Quote names for the shell, even if they would normally not require quoting.
3097 Quote names as for a C language string.
3099 Quote as with @samp{c} except omit the surrounding double-quote
3101 @c The following are not yet implemented in patch 2.5.4.
3103 @c Quote as with @samp{c} except use quotation marks appropriate for the
3106 @c @c Use @t instead of @samp to avoid duplicate quoting in some output styles.
3107 @c Like @samp{clocale}, but quote @t{`like this'} instead of @t{"like
3108 @c this"} in the default C locale. This looks nicer on many displays.
3111 @vindex QUOTING_STYLE
3112 You can specify the default value of the @option{--quoting-style}
3113 option with the environment variable @env{QUOTING_STYLE}. If that
3114 environment variable is not set, the default value is @samp{shell},
3115 but this default may change in a future version of @command{patch}.
3117 @node patch and POSIX
3118 @section @command{patch} and the @sc{posix} Standard
3121 @vindex POSIXLY_CORRECT
3122 If you specify the @option{--posix} option, or set the
3123 @env{POSIXLY_CORRECT} environment variable, @command{patch} conforms
3124 more strictly to the @sc{posix} standard, as follows:
3128 Take the first existing file from the list (old, new, index)
3129 when intuiting file names from diff headers. @xref{Multiple Patches}.
3132 Do not remove files that are removed by a diff.
3133 @xref{Creating and Removing}.
3136 Do not ask whether to get files from @sc{rcs}, ClearCase, or
3137 @sc{sccs}. @xref{Revision Control}.
3140 Require that all options precede the files in the command line.
3143 Do not backup files, even when there is a mismatch. @xref{Backups}.
3147 @node patch and Tradition
3148 @section @sc{gnu} @command{patch} and Traditional @command{patch}
3149 @cindex traditional @command{patch}
3151 The current version of @sc{gnu} @command{patch} normally follows the
3152 @sc{posix} standard. @xref{patch and POSIX}, for the few exceptions
3153 to this general rule.
3155 Unfortunately, @sc{posix} redefined the behavior of @command{patch} in
3156 several important ways. You should be aware of the following
3157 differences if you must interoperate with traditional @command{patch},
3158 or with @sc{gnu} @command{patch} version 2.1 and earlier.
3162 In traditional @command{patch}, the @option{-p} option's operand was
3163 optional, and a bare @option{-p} was equivalent to @option{-p0}. The
3164 @option{-p} option now requires an operand, and @option{-p@ 0} is now
3165 equivalent to @option{-p0}. For maximum compatibility, use options
3166 like @option{-p0} and @option{-p1}.
3168 Also, traditional @command{patch} simply counted slashes when
3169 stripping path prefixes; @command{patch} now counts pathname
3170 components. That is, a sequence of one or more adjacent slashes now
3171 counts as a single slash. For maximum portability, avoid sending
3172 patches containing @file{//} in file names.
3175 In traditional @command{patch}, backups were enabled by default. This
3176 behavior is now enabled with the @option{-b} or @option{--backup}
3179 Conversely, in @sc{posix} @command{patch}, backups are never made,
3180 even when there is a mismatch. In @sc{gnu} @command{patch}, this
3181 behavior is enabled with the @option{--no-backup-if-mismatch} option,
3182 or by conforming to @sc{posix}.
3184 The @option{-b@ @var{suffix}} option of traditional @command{patch} is
3185 equivalent to the @samp{-b -z@ @var{suffix}} options of @sc{gnu}
3189 Traditional @command{patch} used a complicated (and incompletely
3190 documented) method to intuit the name of the file to be patched from
3191 the patch header. This method did not conform to @sc{posix}, and had
3192 a few gotchas. Now @command{patch} uses a different, equally
3193 complicated (but better documented) method that is optionally
3194 @sc{posix}-conforming; we hope it has fewer gotchas. The two methods
3195 are compatible if the file names in the context diff header and the
3196 @samp{Index:} line are all identical after prefix-stripping. Your
3197 patch is normally compatible if each header's file names all contain
3198 the same number of slashes.
3201 When traditional @command{patch} asked the user a question, it sent
3202 the question to standard error and looked for an answer from the first
3203 file in the following list that was a terminal: standard error,
3204 standard output, @file{/dev/tty}, and standard input. Now
3205 @command{patch} sends questions to standard output and gets answers
3206 from @file{/dev/tty}. Defaults for some answers have been changed so
3207 that @command{patch} never goes into an infinite loop when using
3211 Traditional @command{patch} exited with a status value that counted
3212 the number of bad hunks, or with status 1 if there was real trouble.
3213 Now @command{patch} exits with status 1 if some hunks failed, or with
3214 2 if there was real trouble.
3217 Limit yourself to the following options when sending instructions
3218 meant to be executed by anyone running @sc{gnu} @command{patch},
3219 traditional @command{patch}, or a @command{patch} that conforms to
3220 @sc{posix}. Spaces are significant in the following list, and
3221 operands are required.
3225 @option{-d @var{dir}}
3226 @option{-D @var{define}}
3231 @option{-o @var{outfile}}
3232 @option{-p@var{num}}
3234 @option{-r @var{rejectfile}}
3239 @node Making Patches
3240 @chapter Tips for Making and Using Patches
3242 Use some common sense when making and using patches. For example,
3243 when sending bug fixes to a program's maintainer, send several small
3244 patches, one per independent subject, instead of one large,
3245 harder-to-digest patch that covers all the subjects.
3247 Here are some other things you should keep in mind if you are going to
3248 distribute patches for updating a software package.
3251 * Tips for Patch Producers:: Advice for making patches.
3252 * Tips for Patch Consumers:: Advice for using patches.
3253 * Avoiding Common Mistakes:: Avoiding common mistakes when using @command{patch}.
3254 * Generating Smaller Patches:: How to generate smaller patches.
3257 @node Tips for Patch Producers
3258 @section Tips for Patch Producers
3259 @cindex patch producer tips
3261 To create a patch that changes an older version of a package into a
3262 newer version, first make a copy of the older and newer versions in
3263 adjacent subdirectories. It is common to do that by unpacking
3264 @command{tar} archives of the two versions.
3266 To generate the patch, use the command @samp{diff -Naur @var{old}
3267 @var{new}} where @var{old} and @var{new} identify the old and new
3268 directories. The names @var{old} and @var{new} should not contain any
3269 slashes. The @option{-N} option lets the patch create and remove
3270 files; @option{-a} lets the patch update non-text files; @option{-u}
3271 generates useful time stamps and enough context; and @option{-r} lets
3272 the patch update subdirectories. Here is an example command, using
3273 Bourne shell syntax:
3276 diff -Naur gcc-3.0.3 gcc-3.0.4
3279 Tell your recipients how to apply the patches. This should include
3280 which working directory to use, and which @command{patch} options to
3281 use; the option @samp{-p1} is recommended. Test your procedure by
3282 pretending to be a recipient and applying your patches to a copy of
3285 @xref{Avoiding Common Mistakes}, for how to avoid common mistakes when
3288 @node Tips for Patch Consumers
3289 @section Tips for Patch Consumers
3290 @cindex patch consumer tips
3292 A patch producer should tell recipients how to apply the patches, so
3293 the first rule of thumb for a patch consumer is to follow the
3294 instructions supplied with the patch.
3296 @sc{gnu} @command{diff} can analyze files with arbitrarily long lines
3297 and files that end in incomplete lines. However, older versions of
3298 @command{patch} cannot patch such files. If you are having trouble
3299 applying such patches, try upgrading to a recent version of @sc{gnu}
3302 @node Avoiding Common Mistakes
3303 @section Avoiding Common Mistakes
3304 @cindex common mistakes with patches
3305 @cindex patch, common mistakes
3307 When producing a patch for multiple files, apply @command{diff} to
3308 directories whose names do not have slashes. This reduces confusion
3309 when the patch consumer specifies the @option{-p@var{number}} option,
3310 since this option can have surprising results when the old and new
3311 file names have different numbers of slashes. For example, do not
3312 send a patch with a header that looks like this:
3315 diff -Naur v2.0.29/prog/README prog/README
3316 --- v2.0.29/prog/README 2002-03-10 23:30:39.942229878 -0800
3317 +++ prog/README 2002-03-17 20:49:32.442260588 -0800
3321 because the two file names have different numbers of slashes, and
3322 different versions of @command{patch} interpret the file names
3323 differently. To avoid confusion, send output that looks like this
3327 diff -Naur v2.0.29/prog/README v2.0.30/prog/README
3328 --- v2.0.29/prog/README 2002-03-10 23:30:39.942229878 -0800
3329 +++ v2.0.30/prog/README 2002-03-17 20:49:32.442260588 -0800
3332 Make sure you have specified the file names correctly, either in a
3333 context diff header or with an @samp{Index:} line. Take care to not send out
3334 reversed patches, since these make people wonder whether they have
3335 already applied the patch.
3337 Avoid sending patches that compare backup file names like
3338 @file{README.orig} or @file{README~}, since this might confuse
3339 @command{patch} into patching a backup file instead of the real file.
3340 Instead, send patches that compare the same base file names in
3341 different directories, e.g.@: @file{old/README} and @file{new/README}.
3343 To save people from partially applying a patch before other patches that
3344 should have gone before it, you can make the first patch in the patch
3345 file update a file with a name like @file{patchlevel.h} or
3346 @file{version.c}, which contains a patch level or version number. If
3347 the input file contains the wrong version number, @command{patch} will
3348 complain immediately.
3350 An even clearer way to prevent this problem is to put a @samp{Prereq:}
3351 line before the patch. If the leading text in the patch file contains a
3352 line that starts with @samp{Prereq:}, @command{patch} takes the next word
3353 from that line (normally a version number) and checks whether the next
3354 input file contains that word, preceded and followed by either
3355 white space or a newline. If not, @command{patch} prompts you for
3356 confirmation before proceeding. This makes it difficult to accidentally
3357 apply patches in the wrong order.
3359 @node Generating Smaller Patches
3360 @section Generating Smaller Patches
3361 @cindex patches, shrinking
3363 The simplest way to generate a patch is to use @samp{diff -Naur}
3364 (@pxref{Tips for Patch Producers}), but you might be able to reduce
3365 the size of the patch by renaming or removing some files before making
3366 the patch. If the older version of the package contains any files
3367 that the newer version does not, or if any files have been renamed
3368 between the two versions, make a list of @command{rm} and @command{mv}
3369 commands for the user to execute in the old version directory before
3370 applying the patch. Then run those commands yourself in the scratch
3373 If there are any files that you don't need to include in the patch
3374 because they can easily be rebuilt from other files (for example,
3375 @file{TAGS} and output from @command{yacc} and @command{makeinfo}),
3376 exclude them from the patch by giving @command{diff} the @option{-x
3377 @var{pattern}} option (@pxref{Comparing Directories}). If you want
3378 your patch to modify a derived file because your recipients lack tools
3379 to build it, make sure that the patch for the derived file follows any
3380 patches for files that it depends on, so that the recipients' time
3381 stamps will not confuse @command{make}.
3383 Now you can create the patch using @samp{diff -Naur}. Make sure to
3384 specify the scratch directory first and the newer directory second.
3386 Add to the top of the patch a note telling the user any @command{rm} and
3387 @command{mv} commands to run before applying the patch. Then you can
3388 remove the scratch directory.
3390 You can also shrink the patch size by using fewer lines of context,
3391 but bear in mind that @command{patch} typically needs at least two
3392 lines for proper operation when patches do not exactly match the input
3396 @chapter Invoking @command{cmp}
3397 @cindex invoking @command{cmp}
3398 @cindex @command{cmp} invocation
3400 The @command{cmp} command compares two files, and if they differ,
3401 tells the first byte and line number where they differ. Bytes and
3402 lines are numbered starting with 1. The arguments of @command{cmp}
3406 cmp @var{options}@dots{} @var{from-file} @r{[}@var{to-file} @r{[}@var{from-skip} @r{[}@var{to-skip}@r{]}@r{]}@r{]}
3409 The file name @file{-} is always the standard input. @command{cmp}
3410 also uses the standard input if one file name is omitted. The
3411 @var{from-skip} and @var{to-skip} operands specify how many bytes to
3412 ignore at the start of each file; they are equivalent to the
3413 @option{--ignore-initial=@var{from-skip}:@var{to-skip}} option.
3415 An exit status of 0 means no differences were found, 1 means some
3416 differences were found, and 2 means trouble.
3419 * cmp Options:: Summary of options to @command{cmp}.
3423 @section Options to @command{cmp}
3424 @cindex @command{cmp} options
3425 @cindex options for @command{cmp}
3427 Below is a summary of all of the options that @sc{gnu} @command{cmp} accepts.
3428 Most options have two equivalent names, one of which is a single letter
3429 preceded by @samp{-}, and the other of which is a long name preceded by
3430 @samp{--}. Multiple single letter options (unless they take an
3431 argument) can be combined into a single command line word: @option{-bl} is
3432 equivalent to @option{-b -l}.
3436 @itemx --print-bytes
3437 Print the differing bytes. Display control bytes as a
3438 @samp{^} followed by a letter of the alphabet and precede bytes
3439 that have the high bit set with @samp{M-} (which stands for ``meta'').
3442 Output a summary of usage and then exit.
3445 @itemx --ignore-initial=@var{skip}
3446 Ignore any differences in the first @var{skip} bytes of the input
3447 files. Treat files with fewer than @var{skip} bytes as if they are
3448 empty. If @var{skip} is of the form
3449 @option{@var{from-skip}:@var{to-skip}}, skip the first @var{from-skip}
3450 bytes of the first input file and the first @var{to-skip} bytes of the
3455 Print the (decimal) byte numbers and (octal) values of all differing bytes.
3457 @item -n @var{count}
3458 @itemx --bytes=@var{count}
3459 Compare at most @var{count} input bytes.
3464 Do not print anything; only return an exit status indicating whether
3469 Output version information and then exit.
3472 In the above table, operands that are byte counts are normally
3473 decimal, but may be preceded by @samp{0} for octal and @samp{0x} for
3476 A byte count can be followed by a suffix to specify a multiple of that
3477 count; in this case an omitted integer is understood to be 1. A bare
3478 size letter, or one followed by @samp{iB}, specifies a multiple using
3479 powers of 1024. A size letter followed by @samp{B} specifies powers
3480 of 1000 instead. For example, @option{-n 4M} and @option{-n 4MiB} are
3481 equivalent to @option{-n 4194304}, whereas @option{-n 4MB} is
3482 equivalent to @option{-n 4000000}. This notation is upward compatible
3483 with the @uref{http://www.bipm.fr/enus/3_SI/si-prefixes.html, SI
3484 prefixes} for decimal multiples and with the
3485 @uref{http://physics.nist.gov/cuu/Units/binary.html, IEC 60027-2
3486 prefixes for binary multiples}.
3488 The following suffixes are defined. Large sizes like @code{1Y} may be
3489 rejected by your computer due to limitations of its arithmetic.
3493 @cindex kilobyte, definition of
3494 kilobyte: @math{10^3 = 1000}.
3498 @cindex kibibyte, definition of
3499 kibibyte: @math{2^10 = 1024}. @samp{K} is special: the SI prefix is
3500 @samp{k} and the IEC 60027-2 prefix is @samp{Ki}, but tradition and
3501 @sc{posix} use @samp{k} to mean @samp{KiB}.
3503 @cindex megabyte, definition of
3504 megabyte: @math{10^6 = 1,000,000}.
3507 @cindex mebibyte, definition of
3508 mebibyte: @math{2^20 = 1,048,576}.
3510 @cindex gigabyte, definition of
3511 gigabyte: @math{10^9 = 1,000,000,000}.
3514 @cindex gibibyte, definition of
3515 gibibyte: @math{2^30 = 1,073,741,824}.
3517 @cindex terabyte, definition of
3518 terabyte: @math{10^12 = 1,000,000,000,000}.
3521 @cindex tebibyte, definition of
3522 tebibyte: @math{2^40 = 1,099,511,627,776}.
3524 @cindex petabyte, definition of
3525 petabyte: @math{10^15 = 1,000,000,000,000,000}.
3528 @cindex pebibyte, definition of
3529 pebibyte: @math{2^50 = 1,125,899,906,842,624}.
3531 @cindex exabyte, definition of
3532 exabyte: @math{10^18 = 1,000,000,000,000,000,000}.
3535 @cindex exbibyte, definition of
3536 exbibyte: @math{2^60 = 1,152,921,504,606,846,976}.
3538 @cindex zettabyte, definition of
3539 zettabyte: @math{10^21 = 1,000,000,000,000,000,000,000}
3542 @math{2^70 = 1,180,591,620,717,411,303,424}.
3543 (@samp{Zi} is a GNU extension to IEC 60027-2.)
3545 @cindex yottabyte, definition of
3546 yottabyte: @math{10^24 = 1,000,000,000,000,000,000,000,000}.
3549 @math{2^80 = 1,208,925,819,614,629,174,706,176}.
3550 (@samp{Yi} is a GNU extension to IEC 60027-2.)
3554 @chapter Invoking @command{diff}
3555 @cindex invoking @command{diff}
3556 @cindex @command{diff} invocation
3558 The format for running the @command{diff} command is:
3561 diff @var{options}@dots{} @var{files}@dots{}
3564 In the simplest case, two file names @var{from-file} and
3565 @var{to-file} are given, and @command{diff} compares the contents of
3566 @var{from-file} and @var{to-file}. A file name of @file{-} stands for
3567 text read from the standard input. As a special case, @samp{diff - -}
3568 compares a copy of standard input to itself.
3570 If one file is a directory and the other is not, @command{diff} compares
3571 the file in the directory whose name is that of the non-directory.
3572 The non-directory file must not be @file{-}.
3574 If two file names are given and both are directories,
3575 @command{diff} compares corresponding files in both directories, in
3576 alphabetical order; this comparison is not recursive unless the
3577 @option{-r} or @option{--recursive} option is given. @command{diff} never
3578 compares the actual contents of a directory as if it were a file. The
3579 file that is fully specified may not be standard input, because standard
3580 input is nameless and the notion of ``file with the same name'' does not
3583 If the @option{--from-file=@var{file}} option is given, the number of
3584 file names is arbitrary, and @var{file} is compared to each named file.
3585 Similarly, if the @option{--to-file=@var{file}} option is given, each
3586 named file is compared to @var{file}.
3588 @command{diff} options begin with @samp{-}, so normally file names
3589 may not begin with @samp{-}. However, @option{--} as an
3590 argument by itself treats the remaining arguments as file names even if
3591 they begin with @samp{-}.
3593 An exit status of 0 means no differences were found, 1 means some
3594 differences were found, and 2 means trouble.
3597 * diff Options:: Summary of options to @command{diff}.
3601 @section Options to @command{diff}
3602 @cindex @command{diff} options
3603 @cindex options for @command{diff}
3605 Below is a summary of all of the options that @sc{gnu} @command{diff} accepts.
3606 Most options have two equivalent names, one of which is a single letter
3607 preceded by @samp{-}, and the other of which is a long name preceded by
3608 @samp{--}. Multiple single letter options (unless they take an
3609 argument) can be combined into a single command line word: @option{-ac} is
3610 equivalent to @option{-a -c}. Long named options can be abbreviated to
3611 any unique prefix of their name. Brackets ([ and ]) indicate that an
3612 option takes an optional argument.
3617 Treat all files as text and compare them line-by-line, even if they
3618 do not seem to be text. @xref{Binary}.
3621 @itemx --ignore-space-change
3622 Ignore changes in amount of white space. @xref{White Space}.
3625 @itemx --ignore-blank-lines
3626 Ignore changes that just insert or delete blank lines. @xref{Blank
3630 Read and write data in binary mode. @xref{Binary}.
3633 Use the context output format, showing three lines of context.
3634 @xref{Context Format}.
3636 @item -C @var{lines}
3637 @itemx --context@r{[}=@var{lines}@r{]}
3638 Use the context output format, showing @var{lines} (an integer) lines of
3639 context, or three if @var{lines} is not given. @xref{Context Format}.
3640 For proper operation, @command{patch} typically needs at least two lines of
3643 On older systems, @command{diff} supports an obsolete option
3644 @option{-@var{lines}} that has effect when combined with @option{-c}
3645 or @option{-p}. @sc{posix} 1003.1-2001 (@pxref{Standards
3646 conformance}) does not allow this; use @option{-C @var{lines}}
3649 @item --changed-group-format=@var{format}
3650 Use @var{format} to output a line group containing differing lines from
3651 both files in if-then-else format. @xref{Line Group Formats}.
3655 Change the algorithm perhaps find a smaller set of changes. This makes
3656 @command{diff} slower (sometimes much slower). @xref{diff Performance}.
3659 @itemx --ifdef=@var{name}
3660 Make merged @samp{#ifdef} format output, conditional on the preprocessor
3661 macro @var{name}. @xref{If-then-else}.
3665 Make output that is a valid @command{ed} script. @xref{ed Scripts}.
3668 @itemx --ignore-tab-expansion
3669 Ignore changes due to tab expansion.
3674 Make output that looks vaguely like an @command{ed} script but has changes
3675 in the order they appear in the file. @xref{Forward ed}.
3677 @item -F @var{regexp}
3678 @itemx --show-function-line=@var{regexp}
3679 In context and unified format, for each hunk of differences, show some
3680 of the last preceding line that matches @var{regexp}. @xref{Specified
3683 @item --from-file=@var{file}
3684 Compare @var{file} to each operand; @var{file} may be a directory.
3687 Output a summary of usage and then exit.
3689 @item --horizon-lines=@var{lines}
3690 Do not discard the last @var{lines} lines of the common prefix
3691 and the first @var{lines} lines of the common suffix.
3692 @xref{diff Performance}.
3695 @itemx --ignore-case
3696 Ignore changes in case; consider upper- and lower-case letters
3697 equivalent. @xref{Case Folding}.
3699 @item -I @var{regexp}
3700 @itemx --ignore-matching-lines=@var{regexp}
3701 Ignore changes that just insert or delete lines that match @var{regexp}.
3702 @xref{Specified Folding}.
3704 @item --ignore-file-name-case
3705 Ignore case when comparing file names during recursive comparison.
3706 @xref{Comparing Directories}.
3710 Pass the output through @command{pr} to paginate it. @xref{Pagination}.
3712 @item --label=@var{label}
3713 Use @var{label} instead of the file name in the context format
3714 (@pxref{Context Format}) and unified format (@pxref{Unified Format})
3715 headers. @xref{RCS}.
3718 Print only the left column of two common lines in side by side format.
3719 @xref{Side by Side Format}.
3721 @item --line-format=@var{format}
3722 Use @var{format} to output all input lines in if-then-else format.
3723 @xref{Line Formats}.
3727 Output @sc{rcs}-format diffs; like @option{-f} except that each command
3728 specifies the number of lines affected. @xref{RCS}.
3732 In directory comparison, if a file is found in only one directory,
3733 treat it as present but empty in the other directory. @xref{Comparing
3736 @item --new-group-format=@var{format}
3737 Use @var{format} to output a group of lines taken from just the second
3738 file in if-then-else format. @xref{Line Group Formats}.
3740 @item --new-line-format=@var{format}
3741 Use @var{format} to output a line taken from just the second file in
3742 if-then-else format. @xref{Line Formats}.
3744 @item --old-group-format=@var{format}
3745 Use @var{format} to output a group of lines taken from just the first
3746 file in if-then-else format. @xref{Line Group Formats}.
3748 @item --old-line-format=@var{format}
3749 Use @var{format} to output a line taken from just the first file in
3750 if-then-else format. @xref{Line Formats}.
3753 @itemx --show-c-function
3754 Show which C function each change is in. @xref{C Function Headings}.
3758 Report only whether the files differ, not the details of the
3759 differences. @xref{Brief}.
3763 When comparing directories, recursively compare any subdirectories
3764 found. @xref{Comparing Directories}.
3767 @itemx --report-identical-files
3768 Report when two files are the same. @xref{Comparing Directories}.
3771 @itemx --starting-file=@var{file}
3772 When comparing directories, start with the file @var{file}. This is
3773 used for resuming an aborted comparison. @xref{Comparing Directories}.
3775 @item --speed-large-files
3776 Use heuristics to speed handling of large files that have numerous
3777 scattered small changes. @xref{diff Performance}.
3779 @item --strip-trailing-cr
3780 Strip any trailing carriage return at the end of an input line.
3783 @item --suppress-common-lines
3784 Do not print common lines in side by side format.
3785 @xref{Side by Side Format}.
3788 @itemx --expand-tabs
3789 Expand tabs to spaces in the output, to preserve the alignment of tabs
3790 in the input files. @xref{Tabs}.
3793 @itemx --initial-tab
3794 Output a tab rather than a space before the text of a line in normal or
3795 context format. This causes the alignment of tabs in the line to look
3796 normal. @xref{Tabs}.
3798 @item --to-file=@var{file}
3799 Compare each operand to @var{file}; @var{file} may be a directory.
3802 Use the unified output format, showing three lines of context.
3803 @xref{Unified Format}.
3805 @item --unchanged-group-format=@var{format}
3806 Use @var{format} to output a group of common lines taken from both files
3807 in if-then-else format. @xref{Line Group Formats}.
3809 @item --unchanged-line-format=@var{format}
3810 Use @var{format} to output a line common to both files in if-then-else
3811 format. @xref{Line Formats}.
3813 @item --unidirectional-new-file
3814 When comparing directories, if a file appears only in the second
3815 directory of the two, treat it as present but empty in the other.
3816 @xref{Comparing Directories}.
3818 @item -U @var{lines}
3819 @itemx --unified@r{[}=@var{lines}@r{]}
3820 Use the unified output format, showing @var{lines} (an integer) lines of
3821 context, or three if @var{lines} is not given. @xref{Unified Format}.
3822 For proper operation, @command{patch} typically needs at least two lines of
3825 On older systems, @command{diff} supports an obsolete option
3826 @option{-@var{lines}} that has effect when combined with @option{-u}.
3827 @sc{posix} 1003.1-2001 (@pxref{Standards conformance}) does not allow
3828 this; use @option{-U @var{lines}} instead.
3832 Output version information and then exit.
3835 @itemx --ignore-all-space
3836 Ignore white space when comparing lines. @xref{White Space}.
3838 @item -W @var{columns}
3839 @itemx --width=@var{columns}
3840 Output at most @var{columns} (default 130) print columns per line in
3841 side by side format. @xref{Side by Side Format}.
3843 @item -x @var{pattern}
3844 @itemx --exclude=@var{pattern}
3845 When comparing directories, ignore files and subdirectories whose basenames
3846 match @var{pattern}. @xref{Comparing Directories}.
3849 @itemx --exclude-from=@var{file}
3850 When comparing directories, ignore files and subdirectories whose basenames
3851 match any pattern contained in @var{file}. @xref{Comparing Directories}.
3854 @itemx --side-by-side
3855 Use the side by side output format. @xref{Side by Side Format}.
3858 @node Invoking diff3
3859 @chapter Invoking @command{diff3}
3860 @cindex invoking @command{diff3}
3861 @cindex @command{diff3} invocation
3863 The @command{diff3} command compares three files and outputs descriptions
3864 of their differences. Its arguments are as follows:
3867 diff3 @var{options}@dots{} @var{mine} @var{older} @var{yours}
3870 The files to compare are @var{mine}, @var{older}, and @var{yours}.
3871 At most one of these three file names may be @file{-},
3872 which tells @command{diff3} to read the standard input for that file.
3874 An exit status of 0 means @command{diff3} was successful, 1 means some
3875 conflicts were found, and 2 means trouble.
3878 * diff3 Options:: Summary of options to @command{diff3}.
3882 @section Options to @command{diff3}
3883 @cindex @command{diff3} options
3884 @cindex options for @command{diff3}
3886 Below is a summary of all of the options that @sc{gnu} @command{diff3}
3887 accepts. Multiple single letter options (unless they take an argument)
3888 can be combined into a single command line argument.
3893 Treat all files as text and compare them line-by-line, even if they
3894 do not appear to be text. @xref{Binary}.
3898 Incorporate all unmerged changes from @var{older} to @var{yours} into
3899 @var{mine}, surrounding conflicts with bracket lines.
3900 @xref{Marking Conflicts}.
3902 @item --diff-program=@var{program}
3903 Use the compatible comparison program @var{program} to compare files
3904 instead of @command{diff}.
3908 Generate an @command{ed} script that incorporates all the changes from
3909 @var{older} to @var{yours} into @var{mine}. @xref{Which Changes}.
3912 @itemx --show-overlap
3913 Like @option{-e}, except bracket lines from overlapping changes' first
3915 @xref{Marking Conflicts}.
3916 With @option{-E}, an overlapping change looks like this:
3920 @r{lines from @var{mine}}
3922 @r{lines from @var{yours}}
3927 Output a summary of usage and then exit.
3930 Generate @samp{w} and @samp{q} commands at the end of the @command{ed}
3931 script for System V compatibility. This option must be combined with
3932 one of the @option{-AeExX3} options, and may not be combined with @option{-m}.
3933 @xref{Saving the Changed File}.
3935 @item -L @var{label}
3936 @itemx --label=@var{label}
3937 Use the label @var{label} for the brackets output by the @option{-A},
3938 @option{-E} and @option{-X} options. This option may be given up to three
3939 times, one for each input file. The default labels are the names of
3940 the input files. Thus @samp{diff3 -L X -L Y -L Z -m A B C} acts like
3941 @samp{diff3 -m A B C}, except that the output looks like it came from
3942 files named @samp{X}, @samp{Y} and @samp{Z} rather than from files
3943 named @samp{A}, @samp{B} and @samp{C}. @xref{Marking Conflicts}.
3947 Apply the edit script to the first file and send the result to standard
3948 output. Unlike piping the output from @command{diff3} to @command{ed}, this
3949 works even for binary files and incomplete lines. @option{-A} is assumed
3950 if no edit script option is specified. @xref{Bypassing ed}.
3953 @itemx --initial-tab
3954 Output a tab rather than two spaces before the text of a line in normal format.
3955 This causes the alignment of tabs in the line to look normal. @xref{Tabs}.
3959 Output version information and then exit.
3962 @itemx --overlap-only
3963 Like @option{-e}, except output only the overlapping changes.
3964 @xref{Which Changes}.
3967 Like @option{-E}, except output only the overlapping changes.
3968 In other words, like @option{-x}, except bracket changes as in @option{-E}.
3969 @xref{Marking Conflicts}.
3973 Like @option{-e}, except output only the nonoverlapping changes.
3974 @xref{Which Changes}.
3977 @node Invoking patch
3978 @chapter Invoking @command{patch}
3979 @cindex invoking @command{patch}
3980 @cindex @command{patch} invocation
3982 Normally @command{patch} is invoked like this:
3985 patch <@var{patchfile}
3988 The full format for invoking @command{patch} is:
3991 patch @var{options}@dots{} @r{[}@var{origfile} @r{[}@var{patchfile}@r{]}@r{]}
3994 You can also specify where to read the patch from with the @option{-i
3995 @var{patchfile}} or @option{--input=@var{patchfile}} option.
3996 If you do not specify @var{patchfile}, or if @var{patchfile} is
3997 @file{-}, @command{patch} reads the patch (that is, the @command{diff} output)
3998 from the standard input.
4000 If you do not specify an input file on the command line, @command{patch}
4001 tries to intuit from the @dfn{leading text} (any text in the patch
4002 that comes before the @command{diff} output) which file to edit.
4003 @xref{Multiple Patches}.
4005 By default, @command{patch} replaces the original input file with the
4006 patched version, possibly after renaming the original file into a
4007 backup file (@pxref{Backup Names}, for a description of how
4008 @command{patch} names backup files). You can also specify where to
4009 put the output with the @option{-o @var{file}} or
4010 @option{--output=@var{file}} option; however, do not use this option
4011 if @var{file} is one of the input files.
4014 * patch Options:: Summary table of options to @command{patch}.
4018 @section Options to @command{patch}
4019 @cindex @command{patch} options
4020 @cindex options for @command{patch}
4022 Here is a summary of all of the options that @sc{gnu} @command{patch}
4023 accepts. @xref{patch and Tradition}, for which of these options are
4024 safe to use in older versions of @command{patch}.
4026 Multiple single-letter options that do not take an argument can be
4027 combined into a single command line argument with only one dash.
4032 Back up the original contents of each file, even if backups would
4033 normally not be made. @xref{Backups}.
4035 @item -B @var{prefix}
4036 @itemx --prefix=@var{prefix}
4037 Prepend @var{prefix} to backup file names. @xref{Backup Names}.
4039 @item --backup-if-mismatch
4040 Back up the original contents of each file if the patch does not
4041 exactly match the file. This is the default behavior when not
4042 conforming to @sc{posix}. @xref{Backups}.
4045 Read and write all files in binary mode, except for standard output
4046 and @file{/dev/tty}. This option has no effect on
4047 @sc{posix}-conforming systems like @sc{gnu}/Linux. On systems where
4048 this option makes a difference, the patch should be generated by
4049 @samp{diff -a --binary}. @xref{Binary}.
4053 Interpret the patch file as a context diff. @xref{patch Input}.
4055 @item -d @var{directory}
4056 @itemx --directory=@var{directory}
4057 Make directory @var{directory} the current directory for interpreting
4058 both file names in the patch file, and file names given as arguments to
4059 other options. @xref{patch Directories}.
4062 @itemx --ifdef=@var{name}
4063 Make merged if-then-else output using @var{name}. @xref{If-then-else}.
4066 Print the results of applying the patches without actually changing
4067 any files. @xref{Dry Runs}.
4071 Interpret the patch file as an @command{ed} script. @xref{patch Input}.
4074 @itemx --remove-empty-files
4075 Remove output files that are empty after the patches have been applied.
4076 @xref{Creating and Removing}.
4080 Assume that the user knows exactly what he or she is doing, and do not
4081 ask any questions. @xref{patch Messages}.
4083 @item -F @var{lines}
4084 @itemx --fuzz=@var{lines}
4085 Set the maximum fuzz factor to @var{lines}. @xref{Inexact}.
4088 @itemx --get=@var{num}
4089 If @var{num} is positive, get input files from a revision control
4090 system as necessary; if zero, do not get the files; if negative, ask
4091 the user whether to get the files. @xref{Revision Control}.
4094 Output a summary of usage and then exit.
4096 @item -i @var{patchfile}
4097 @itemx --input=@var{patchfile}
4098 Read the patch from @var{patchfile} rather than from standard input.
4099 @xref{patch Options}.
4102 @itemx --ignore-white-space
4103 Let any sequence of blanks (spaces or tabs) in the patch file match
4104 any sequence of blanks in the input file. @xref{Changed White Space}.
4108 Interpret the patch file as a normal diff. @xref{patch Input}.
4112 Ignore patches that @command{patch} thinks are reversed or already applied.
4113 See also @option{-R}. @xref{Reversed Patches}.
4115 @item --no-backup-if-mismatch
4116 Do not back up the original contents of files. This is the default
4117 behavior when conforming to @sc{posix}. @xref{Backups}.
4120 @itemx --output=@var{file}
4121 Use @var{file} as the output file name. @xref{patch Options}.
4123 @item -p@var{number}
4124 @itemx --strip=@var{number}
4125 Set the file name strip count to @var{number}. @xref{patch Directories}.
4128 Conform to @sc{posix}, as if the @env{POSIXLY_CORRECT} environment
4129 variable had been set. @xref{patch and POSIX}.
4131 @item --quoting-style=@var{word}
4132 Use style @var{word} to quote names in diagnostics, as if the
4133 @env{QUOTING_STYLE} environment variable had been set to @var{word}.
4134 @xref{patch Quoting Style}.
4136 @item -r @var{reject-file}
4137 @itemx --reject-file=@var{reject-file}
4138 Use @var{reject-file} as the reject file name. @xref{Reject Names}.
4142 Assume that this patch was created with the old and new files swapped.
4143 @xref{Reversed Patches}.
4148 Work silently unless an error occurs. @xref{patch Messages}.
4152 Do not ask any questions. @xref{patch Messages}.
4156 Set the modification and access times of patched files from time
4157 stamps given in context diff headers, assuming that the context diff
4158 headers use local time. @xref{Patching Time Stamps}.
4162 Interpret the patch file as a unified diff. @xref{patch Input}.
4166 Output version information and then exit.
4168 @item -V @var{backup-style}
4169 @itemx --version=control=@var{backup-style}
4170 Select the naming convention for backup file names. @xref{Backup Names}.
4173 Print more diagnostics than usual. @xref{patch Messages}.
4175 @item -x @var{number}
4176 @itemx --debug=@var{number}
4177 Set internal debugging flags. Of interest only to @command{patch}
4180 @item -Y @var{prefix}
4181 @itemx --basename-prefix=@var{prefix}
4182 Prepend @var{prefix} to base names of backup files. @xref{Backup Names}.
4184 @item -z @var{suffix}
4185 @itemx --suffix=@var{suffix}
4186 Use @var{suffix} as the backup extension instead of @samp{.orig} or
4187 @samp{~}. @xref{Backup Names}.
4191 Set the modification and access times of patched files from time
4192 stamps given in context diff headers, assuming that the context diff
4193 headers use @sc{utc}. @xref{Patching Time Stamps}.
4197 @node Invoking sdiff
4198 @chapter Invoking @command{sdiff}
4199 @cindex invoking @command{sdiff}
4200 @cindex @command{sdiff} invocation
4202 The @command{sdiff} command merges two files and interactively outputs the
4203 results. Its arguments are as follows:
4206 sdiff -o @var{outfile} @var{options}@dots{} @var{from-file} @var{to-file}
4209 This merges @var{from-file} with @var{to-file}, with output to @var{outfile}.
4210 If @var{from-file} is a directory and @var{to-file} is not, @command{sdiff}
4211 compares the file in @var{from-file} whose file name is that of @var{to-file},
4212 and vice versa. @var{from-file} and @var{to-file} may not both be
4215 @command{sdiff} options begin with @samp{-}, so normally @var{from-file}
4216 and @var{to-file} may not begin with @samp{-}. However, @option{--} as an
4217 argument by itself treats the remaining arguments as file names even if
4218 they begin with @samp{-}. You may not use @file{-} as an input file.
4220 @command{sdiff} without @option{-o} (or @option{--output}) produces a
4221 side-by-side difference. This usage is obsolete; use the @option{-y}
4222 or @option{--side-by-side} option of @command{diff} instead.
4224 An exit status of 0 means no differences were found, 1 means some
4225 differences were found, and 2 means trouble.
4228 * sdiff Options:: Summary of options to @command{diff}.
4232 @section Options to @command{sdiff}
4233 @cindex @command{sdiff} options
4234 @cindex options for @command{sdiff}
4236 Below is a summary of all of the options that @sc{gnu} @command{sdiff} accepts.
4237 Each option has two equivalent names, one of which is a single
4238 letter preceded by @samp{-}, and the other of which is a long name
4239 preceded by @samp{--}. Multiple single letter options (unless they take
4240 an argument) can be combined into a single command line argument. Long
4241 named options can be abbreviated to any unique prefix of their name.
4246 Treat all files as text and compare them line-by-line, even if they
4247 do not appear to be text. @xref{Binary}.
4250 @itemx --ignore-space-change
4251 Ignore changes in amount of white space. @xref{White Space}.
4254 @itemx --ignore-blank-lines
4255 Ignore changes that just insert or delete blank lines. @xref{Blank
4260 Change the algorithm to perhaps find a smaller set of changes. This
4261 makes @command{sdiff} slower (sometimes much slower). @xref{diff
4264 @item --diff-program=@var{program}
4265 Use the compatible comparison program @var{program} to compare files
4266 instead of @command{diff}.
4269 @itemx --ignore-tab-expansion
4270 Ignore changes due to tab expansion.
4274 Output a summary of usage and then exit.
4277 @itemx --ignore-case
4278 Ignore changes in case; consider upper- and lower-case to be the same.
4279 @xref{Case Folding}.
4281 @item -I @var{regexp}
4282 @itemx --ignore-matching-lines=@var{regexp}
4283 Ignore changes that just insert or delete lines that match @var{regexp}.
4284 @xref{Specified Folding}.
4287 @itemx --left-column
4288 Print only the left column of two common lines.
4289 @xref{Side by Side Format}.
4292 @itemx --output=@var{file}
4293 Put merged output into @var{file}. This option is required for merging.
4296 @itemx --suppress-common-lines
4297 Do not print common lines. @xref{Side by Side Format}.
4299 @item --speed-large-files
4300 Use heuristics to speed handling of large files that have numerous
4301 scattered small changes. @xref{diff Performance}.
4303 @item --strip-trailing-cr
4304 Strip any trailing carriage return at the end of an input line.
4308 @itemx --expand-tabs
4309 Expand tabs to spaces in the output, to preserve the alignment of tabs
4310 in the input files. @xref{Tabs}.
4314 Output version information and then exit.
4316 @item -w @var{columns}
4317 @itemx --width=@var{columns}
4318 Output at most @var{columns} (default 130) print columns per line.
4319 @xref{Side by Side Format}. Note that for historical reasons, this
4320 option is @option{-W} in @command{diff}, @option{-w} in @command{sdiff}.
4323 @itemx --ignore-all-space
4324 Ignore white space when comparing lines. @xref{White Space}.
4325 Note that for historical reasons, this option is @option{-w} in @command{diff},
4326 @option{-W} in @command{sdiff}.
4329 @node Standards conformance
4330 @chapter Standards conformance
4333 @vindex POSIXLY_CORRECT
4334 In a few cases, the @sc{gnu} utilities' default behavior is
4335 incompatible with the @sc{posix} standard. To suppress these
4336 incompatibilities, define the @env{POSIXLY_CORRECT} environment
4337 variable. Unless you are checking for @sc{posix} conformance, you
4338 probably do not need to define @env{POSIXLY_CORRECT}.
4340 Normally options and operands can appear in any order, and programs act
4341 as if all the options appear before any operands. For example,
4342 @samp{diff lao tzu -C 2} acts like @samp{diff -C 2 lao tzu}, since
4343 @samp{2} is an option-argument of @option{-C}. However, if the
4344 @env{POSIXLY_CORRECT} environment variable is set, options must appear
4345 before operands, unless otherwise specified for a particular command.
4347 Newer versions of @sc{posix} are occasionally incompatible with older
4348 versions. For example, older versions of @sc{posix} allowed the
4349 command @samp{diff -c -10} to have the same meaning as @samp{diff -C
4350 10}, but @sc{posix} 1003.1-2001 @samp{diff} no longer allows
4351 digit-string options like @option{-10}.
4353 @vindex _POSIX2_VERSION
4354 The @sc{gnu} utilities normally conform to the version of @sc{posix}
4355 that is standard for your system. To cause them to conform to a
4356 different version of @sc{posix}, define the @env{_POSIX2_VERSION}
4357 environment variable to a value of the form @var{yyyymm} specifying
4358 the year and month the standard was adopted. Two values are currently
4359 supported for @env{_POSIX2_VERSION}: @samp{199209} stands for
4360 @sc{posix} 1003.2-1992, and @samp{200112} stands for @sc{posix}
4361 1003.1-2001. For example, if you are running older software that
4362 assumes an older version of @sc{posix} and uses @samp{diff -c -10},
4363 you can work around the compatibility problems by setting
4364 @samp{_POSIX2_VERSION=199209} in your environment.
4367 @chapter Future Projects
4369 Here are some ideas for improving @sc{gnu} @command{diff} and
4370 @command{patch}. The @sc{gnu} project has identified some
4371 improvements as potential programming projects for volunteers. You
4372 can also help by reporting any bugs that you find.
4374 If you are a programmer and would like to contribute something to the
4375 @sc{gnu} project, please consider volunteering for one of these
4376 projects. If you are seriously contemplating work, please write to
4377 @email{gnu@@gnu.org} to coordinate with other volunteers.
4380 * Shortcomings:: Suggested projects for improvements.
4381 * Bugs:: Reporting bugs.
4385 @section Suggested Projects for Improving @sc{gnu} @command{diff} and @command{patch}
4386 @cindex projects for directories
4388 One should be able to use @sc{gnu} @command{diff} to generate a patch from any
4389 pair of directory trees, and given the patch and a copy of one such
4390 tree, use @command{patch} to generate a faithful copy of the other.
4391 Unfortunately, some changes to directory trees cannot be expressed using
4392 current patch formats; also, @command{patch} does not handle some of the
4393 existing formats. These shortcomings motivate the following suggested
4397 * Internationalization:: Handling multibyte and varying-width characters.
4398 * Changing Structure:: Handling changes to the directory structure.
4399 * Special Files:: Handling symbolic links, device special files, etc.
4400 * Unusual File Names:: Handling file names that contain unusual characters.
4401 * Time Stamp Order:: Outputting diffs in time stamp order.
4402 * Ignoring Changes:: Ignoring certain changes while showing others.
4403 * Speedups:: Improving performance.
4406 @node Internationalization
4407 @subsection Handling Multibyte and Varying-Width Characters
4408 @cindex multibyte characters
4409 @cindex varying-width characters
4411 @command{diff}, @command{diff3} and @command{sdiff} treat each line of
4412 input as a string of unibyte characters. This can mishandle multibyte
4413 characters in some cases. For example, when asked to ignore spaces,
4414 @command{diff} does not properly ignore a multibyte space character.
4416 Also, @command{diff} currently assumes that each byte is one column
4417 wide, and this assumption is incorrect in some locales, e.g., locales
4418 that use UTF-8 encoding. This causes problems with the @option{-y} or
4419 @option{--side-by-side} option of @command{diff}.
4421 These problems need to be fixed without unduly affecting the
4422 performance of the utilities in unibyte environments.
4424 The IBM GNU/Linux Technology Center Internationalization Team has
4425 proposed some patches to support internationalized @command{diff}
4426 @uref{http://oss.software.ibm.com/developer/opensource/linux/patches/i18n/diffutils-2.7.2-i18n-0.1.patch.gz}.
4427 Unfortunately, these patches are incomplete and are to an older
4428 version of @command{diff}, so more work needs to be done in this area.
4430 @node Changing Structure
4431 @subsection Handling Changes to the Directory Structure
4432 @cindex directory structure changes
4434 @command{diff} and @command{patch} do not handle some changes to directory
4435 structure. For example, suppose one directory tree contains a directory
4436 named @samp{D} with some subsidiary files, and another contains a file
4437 with the same name @samp{D}. @samp{diff -r} does not output enough
4438 information for @command{patch} to transform the directory subtree into
4441 There should be a way to specify that a file has been removed without
4442 having to include its entire contents in the patch file. There should
4443 also be a way to tell @command{patch} that a file was renamed, even if
4444 there is no way for @command{diff} to generate such information.
4445 There should be a way to tell @command{patch} that a file's time stamp
4446 has changed, even if its contents have not changed.
4448 These problems can be fixed by extending the @command{diff} output format
4449 to represent changes in directory structure, and extending @command{patch}
4450 to understand these extensions.
4453 @subsection Files that are Neither Directories Nor Regular Files
4454 @cindex special files
4456 Some files are neither directories nor regular files: they are unusual
4457 files like symbolic links, device special files, named pipes, and
4458 sockets. Currently, @command{diff} treats symbolic links like regular files;
4459 it treats other special files like regular files if they are specified
4460 at the top level, but simply reports their presence when comparing
4461 directories. This means that @command{patch} cannot represent changes
4462 to such files. For example, if you change which file a symbolic link
4463 points to, @command{diff} outputs the difference between the two files,
4464 instead of the change to the symbolic link.
4466 @c This might not be a good idea; is it wise for root to install devices
4468 @command{diff} should optionally report changes to special files specially,
4469 and @command{patch} should be extended to understand these extensions.
4471 @node Unusual File Names
4472 @subsection File Names that Contain Unusual Characters
4473 @cindex file names with unusual characters
4475 When a file name contains an unusual character like a newline or
4476 white space, @samp{diff -r} generates a patch that @command{patch} cannot
4477 parse. The problem is with format of @command{diff} output, not just with
4478 @command{patch}, because with odd enough file names one can cause
4479 @command{diff} to generate a patch that is syntactically correct but
4480 patches the wrong files. The format of @command{diff} output should be
4481 extended to handle all possible file names.
4483 @node Time Stamp Order
4484 @subsection Outputting Diffs in Time Stamp Order
4486 Applying @command{patch} to a multiple-file diff can result in files
4487 whose time stamps are out of order. @sc{gnu} @command{patch} has
4488 options to restore the time stamps of the updated files
4489 (@pxref{Patching Time Stamps}), but sometimes it is useful to generate
4490 a patch that works even if the recipient does not have @sc{gnu} patch,
4491 or does not use these options. One way to do this would be to
4492 implement a @command{diff} option to output diffs in time stamp order.
4494 @node Ignoring Changes
4495 @subsection Ignoring Certain Changes
4497 It would be nice to have a feature for specifying two strings, one in
4498 @var{from-file} and one in @var{to-file}, which should be considered to
4499 match. Thus, if the two strings are @samp{foo} and @samp{bar}, then if
4500 two lines differ only in that @samp{foo} in file 1 corresponds to
4501 @samp{bar} in file 2, the lines are treated as identical.
4503 It is not clear how general this feature can or should be, or
4504 what syntax should be used for it.
4506 A partial substitute is to filter one or both files before comparing,
4510 sed 's/foo/bar/g' file1 | diff - file2
4513 However, this outputs the filtered text, not the original.
4516 @subsection Improving Performance
4518 When comparing two large directory structures, one of which was
4519 originally copied from the other with time stamps preserved (e.g.,
4520 with @samp{cp -pR}), it would greatly improve performance if an option
4521 told @command{diff} to assume that two files with the same size and
4522 time stamps have the same content. @xref{diff Performance}.
4525 @section Reporting Bugs
4527 @cindex reporting bugs
4529 If you think you have found a bug in @sc{gnu} @command{cmp},
4530 @command{diff}, @command{diff3}, or @command{sdiff}, please report it
4531 by electronic mail to the
4532 @uref{http://mail.gnu.org/mailman/listinfo/bug-gnu-utils,GNU utilities
4533 bug report mailing list} @email{bug-gnu-utils@@gnu.org}. Please send
4534 bug reports for @sc{gnu} @command{patch} to
4535 @email{bug-patch@@gnu.org}. Send as precise a description of the
4536 problem as you can, including the output of the @option{--version}
4537 option and sample input files that produce the bug, if applicable. If
4538 you have a nontrivial fix for the bug, please send it as well. If you
4539 have a patch, please send it too. It may simplify the maintainer's
4540 job if the patch is relative to a recent test release, which you can
4541 find in the directory @uref{ftp://alpha.gnu.org/gnu/diffutils/}.
4543 @node Copying This Manual
4544 @appendix Copying This Manual
4547 * GNU Free Documentation License:: License for copying this manual.