source: trunk/essentials/sys-apps/diffutils/doc/diff.info@ 3506

Last change on this file since 3506 was 2556, checked in by bird, 20 years ago

diffutils 2.8.1

File size: 197.9 KB
Line 
1This is diff.info, produced by makeinfo version 4.2 from diff.texi.
2
3This manual is for GNU Diffutils (version 2.8.1, 5 April 2002), and
4documents the GNU `diff', `diff3', `sdiff', and `cmp' commands for
5showing the differences between files and the GNU `patch' command for
6using their output to update files.
7
8 Copyright (C) 1992, 1993, 1994, 1998, 2001, 2002 Free Software
9Foundation, Inc.
10
11 Permission is granted to copy, distribute and/or modify this
12 document under the terms of the GNU Free Documentation License,
13 Version 1.1 or any later version published by the Free Software
14 Foundation; with no Invariant Sections, with the Front-Cover texts
15 being "A GNU Manual," and with the Back-Cover Texts as in (a)
16 below. A copy of the license is included in the section entitled
17 "GNU Free Documentation License."
18
19 (a) The FSF's Back-Cover Text is: "You have freedom to copy and
20 modify this GNU Manual, like GNU software. Copies published by
21 the Free Software Foundation raise funds for GNU development."
22
23INFO-DIR-SECTION Individual utilities
24START-INFO-DIR-ENTRY
25* cmp: (diff)Invoking cmp. Compare 2 files byte by byte.
26* diff: (diff)Invoking diff. Compare 2 files line by line.
27* diff3: (diff)Invoking diff3. Compare 3 files line by line.
28* patch: (diff)Invoking patch. Apply a patch to a file.
29* sdiff: (diff)Invoking sdiff. Merge 2 files side-by-side.
30END-INFO-DIR-ENTRY
31
32INFO-DIR-SECTION GNU packages
33START-INFO-DIR-ENTRY
34* Diff: (diff). Comparing and merging files.
35END-INFO-DIR-ENTRY
36
37
38File: diff.info, Node: Top, Next: Overview, Up: (dir)
39
40Comparing and Merging Files
41***************************
42
43This manual is for GNU Diffutils (version 2.8.1, 5 April 2002), and
44documents the GNU `diff', `diff3', `sdiff', and `cmp' commands for
45showing the differences between files and the GNU `patch' command for
46using their output to update files.
47
48 Copyright (C) 1992, 1993, 1994, 1998, 2001, 2002 Free Software
49Foundation, Inc.
50
51 Permission is granted to copy, distribute and/or modify this
52 document under the terms of the GNU Free Documentation License,
53 Version 1.1 or any later version published by the Free Software
54 Foundation; with no Invariant Sections, with the Front-Cover texts
55 being "A GNU Manual," and with the Back-Cover Texts as in (a)
56 below. A copy of the license is included in the section entitled
57 "GNU Free Documentation License."
58
59 (a) The FSF's Back-Cover Text is: "You have freedom to copy and
60 modify this GNU Manual, like GNU software. Copies published by
61 the Free Software Foundation raise funds for GNU development."
62
63* Menu:
64
65* Overview:: Preliminary information.
66* Comparison:: What file comparison means.
67
68* Output Formats:: Formats for two-way difference reports.
69* Incomplete Lines:: Lines that lack trailing newlines.
70* Comparing Directories:: Comparing files and directories.
71* Adjusting Output:: Making `diff' output prettier.
72* diff Performance:: Making `diff' smarter or faster.
73
74* Comparing Three Files:: Formats for three-way difference reports.
75* diff3 Merging:: Merging from a common ancestor.
76
77* Interactive Merging:: Interactive merging with `sdiff'.
78
79* Merging with patch:: Using `patch' to change old files into new ones.
80* Making Patches:: Tips for making and using patch distributions.
81
82* Invoking cmp:: Compare two files byte by byte.
83* Invoking diff:: Compare two files line by line.
84* Invoking diff3:: Compare three files line by line.
85* Invoking patch:: Apply a diff file to an original.
86* Invoking sdiff:: Side-by-side merge of file differences.
87
88* Standards conformance:: Conformance to the POSIX standard.
89* Projects:: If you've found a bug or other shortcoming.
90
91* Copying This Manual:: How to make copies of this manual.
92* Index:: Index.
93
94
95File: diff.info, Node: Overview, Next: Comparison, Prev: Top, Up: Top
96
97Overview
98********
99
100 Computer users often find occasion to ask how two files differ.
101Perhaps one file is a newer version of the other file. Or maybe the
102two files started out as identical copies but were changed by different
103people.
104
105 You can use the `diff' command to show differences between two
106files, or each corresponding file in two directories. `diff' outputs
107differences between files line by line in any of several formats,
108selectable by command line options. This set of differences is often
109called a "diff" or "patch". For files that are identical, `diff'
110normally produces no output; for binary (non-text) files, `diff'
111normally reports only that they are different.
112
113 You can use the `cmp' command to show the byte and line numbers
114where two files differ. `cmp' can also show all the bytes that differ
115between the two files, side by side. A way to compare two files
116character by character is the Emacs command `M-x compare-windows'.
117*Note Other Window: (emacs)Other Window, for more information on that
118command.
119
120 You can use the `diff3' command to show differences among three
121files. When two people have made independent changes to a common
122original, `diff3' can report the differences between the original and
123the two changed versions, and can produce a merged file that contains
124both persons' changes together with warnings about conflicts.
125
126 You can use the `sdiff' command to merge two files interactively.
127
128 You can use the set of differences produced by `diff' to distribute
129updates to text files (such as program source code) to other people.
130This method is especially useful when the differences are small compared
131to the complete files. Given `diff' output, you can use the `patch'
132program to update, or "patch", a copy of the file. If you think of
133`diff' as subtracting one file from another to produce their
134difference, you can think of `patch' as adding the difference to one
135file to reproduce the other.
136
137 This manual first concentrates on making diffs, and later shows how
138to use diffs to update files.
139
140 GNU `diff' was written by Paul Eggert, Mike Haertel, David Hayes,
141Richard Stallman, and Len Tower. Wayne Davison designed and
142implemented the unified output format. The basic algorithm is described
143in "An O(ND) Difference Algorithm and its Variations", Eugene W. Myers,
144`Algorithmica' Vol. 1 No. 2, 1986, pp. 251-266; and in "A File
145Comparison Program", Webb Miller and Eugene W. Myers,
146`Software--Practice and Experience' Vol. 15 No. 11, 1985, pp. 1025-1040.
147The algorithm was independently discovered as described in "Algorithms
148for Approximate String Matching", E. Ukkonen, `Information and Control'
149Vol. 64, 1985, pp. 100-118.
150
151 GNU `diff3' was written by Randy Smith. GNU `sdiff' was written by
152Thomas Lord. GNU `cmp' was written by Torbjorn Granlund and David
153MacKenzie.
154
155 `patch' was written mainly by Larry Wall and Paul Eggert; several
156GNU enhancements were contributed by Wayne Davison and David MacKenzie.
157Parts of this manual are adapted from a manual page written by Larry
158Wall, with his permission.
159
160
161File: diff.info, Node: Comparison, Next: Output Formats, Prev: Overview, Up: Top
162
163What Comparison Means
164*********************
165
166 There are several ways to think about the differences between two
167files. One way to think of the differences is as a series of lines
168that were deleted from, inserted in, or changed in one file to produce
169the other file. `diff' compares two files line by line, finds groups of
170lines that differ, and reports each group of differing lines. It can
171report the differing lines in several formats, which have different
172purposes.
173
174 GNU `diff' can show whether files are different without detailing
175the differences. It also provides ways to suppress certain kinds of
176differences that are not important to you. Most commonly, such
177differences are changes in the amount of white space between words or
178lines. `diff' also provides ways to suppress differences in alphabetic
179case or in lines that match a regular expression that you provide.
180These options can accumulate; for example, you can ignore changes in
181both white space and alphabetic case.
182
183 Another way to think of the differences between two files is as a
184sequence of pairs of bytes that can be either identical or different.
185`cmp' reports the differences between two files byte by byte, instead
186of line by line. As a result, it is often more useful than `diff' for
187comparing binary files. For text files, `cmp' is useful mainly when
188you want to know only whether two files are identical, or whether one
189file is a prefix of the other.
190
191 To illustrate the effect that considering changes byte by byte can
192have compared with considering them line by line, think of what happens
193if a single newline character is added to the beginning of a file. If
194that file is then compared with an otherwise identical file that lacks
195the newline at the beginning, `diff' will report that a blank line has
196been added to the file, while `cmp' will report that almost every byte
197of the two files differs.
198
199 `diff3' normally compares three input files line by line, finds
200groups of lines that differ, and reports each group of differing lines.
201Its output is designed to make it easy to inspect two different sets of
202changes to the same file.
203
204* Menu:
205
206* Hunks:: Groups of differing lines.
207* White Space:: Suppressing differences in white space.
208* Blank Lines:: Suppressing differences in blank lines.
209* Case Folding:: Suppressing differences in alphabetic case.
210* Specified Folding:: Suppressing differences that match regular expressions.
211* Brief:: Summarizing which files are different.
212* Binary:: Comparing binary files or forcing text comparisons.
213
214
215File: diff.info, Node: Hunks, Next: White Space, Up: Comparison
216
217Hunks
218=====
219
220 When comparing two files, `diff' finds sequences of lines common to
221both files, interspersed with groups of differing lines called "hunks".
222Comparing two identical files yields one sequence of common lines and
223no hunks, because no lines differ. Comparing two entirely different
224files yields no common lines and one large hunk that contains all lines
225of both files. In general, there are many ways to match up lines
226between two given files. `diff' tries to minimize the total hunk size
227by finding large sequences of common lines interspersed with small
228hunks of differing lines.
229
230 For example, suppose the file `F' contains the three lines `a', `b',
231`c', and the file `G' contains the same three lines in reverse order
232`c', `b', `a'. If `diff' finds the line `c' as common, then the command
233`diff F G' produces this output:
234
235 1,2d0
236 < a
237 < b
238 3a2,3
239 > b
240 > a
241
242But if `diff' notices the common line `b' instead, it produces this
243output:
244
245 1c1
246 < a
247 ---
248 > c
249 3c3
250 < c
251 ---
252 > a
253
254It is also possible to find `a' as the common line. `diff' does not
255always find an optimal matching between the files; it takes shortcuts
256to run faster. But its output is usually close to the shortest
257possible. You can adjust this tradeoff with the `--minimal' option
258(*note diff Performance::).
259
260
261File: diff.info, Node: White Space, Next: Blank Lines, Prev: Hunks, Up: Comparison
262
263Suppressing Differences in Blank and Tab Spacing
264================================================
265
266 The `-E' and `--ignore-tab-expansion' options ignore the distinction
267between tabs and spaces on input. A tab is considered to be equivalent
268to the number of spaces to the next tab stop. `diff' assumes that tab
269stops are set every 8 print columns.
270
271 The `-b' and `--ignore-space-change' options are stronger. They
272ignore white space at line end, and consider all other sequences of one
273or more white space characters to be equivalent. With these options,
274`diff' considers the following two lines to be equivalent, where `$'
275denotes the line end:
276
277 Here lyeth muche rychnesse in lytell space. -- John Heywood$
278 Here lyeth muche rychnesse in lytell space. -- John Heywood $
279
280 The `-w' and `--ignore-all-space' options are stronger still. They
281ignore difference even if one line has white space where the other line
282has none. "White space" characters include tab, newline, vertical tab,
283form feed, carriage return, and space; some locales may define
284additional characters to be white space. With these options, `diff'
285considers the following two lines to be equivalent, where `$' denotes
286the line end and `^M' denotes a carriage return:
287
288 Here lyeth muche rychnesse in lytell space.-- John Heywood$
289 He relyeth much erychnes seinly tells pace. --John Heywood ^M$
290
291
292File: diff.info, Node: Blank Lines, Next: Case Folding, Prev: White Space, Up: Comparison
293
294Suppressing Differences in Blank Lines
295======================================
296
297 The `-B' and `--ignore-blank-lines' options ignore insertions or
298deletions of blank lines. These options affect only lines that are
299completely empty; they do not affect lines that look empty but contain
300space or tab characters. With these options, for example, a file
301containing
302 1. A point is that which has no part.
303
304 2. A line is breadthless length.
305 -- Euclid, The Elements, I
306
307is considered identical to a file containing
308 1. A point is that which has no part.
309 2. A line is breadthless length.
310
311
312 -- Euclid, The Elements, I
313
314
315File: diff.info, Node: Case Folding, Next: Specified Folding, Prev: Blank Lines, Up: Comparison
316
317Suppressing Case Differences
318============================
319
320 GNU `diff' can treat lower case letters as equivalent to their upper
321case counterparts, so that, for example, it considers `Funky Stuff',
322`funky STUFF', and `fUNKy stuFf' to all be the same. To request this,
323use the `-i' or `--ignore-case' option.
324
325
326File: diff.info, Node: Specified Folding, Next: Brief, Prev: Case Folding, Up: Comparison
327
328Suppressing Lines Matching a Regular Expression
329===============================================
330
331 To ignore insertions and deletions of lines that match a
332`grep'-style regular expression, use the `-I REGEXP' or
333`--ignore-matching-lines=REGEXP' option. You should escape regular
334expressions that contain shell metacharacters to prevent the shell from
335expanding them. For example, `diff -I '^[[:digit:]]'' ignores all
336changes to lines beginning with a digit.
337
338 However, `-I' only ignores the insertion or deletion of lines that
339contain the regular expression if every changed line in the hunk--every
340insertion and every deletion--matches the regular expression. In other
341words, for each nonignorable change, `diff' prints the complete set of
342changes in its vicinity, including the ignorable ones.
343
344 You can specify more than one regular expression for lines to ignore
345by using more than one `-I' option. `diff' tries to match each line
346against each regular expression.
347
348
349File: diff.info, Node: Brief, Next: Binary, Prev: Specified Folding, Up: Comparison
350
351Summarizing Which Files Differ
352==============================
353
354 When you only want to find out whether files are different, and you
355don't care what the differences are, you can use the summary output
356format. In this format, instead of showing the differences between the
357files, `diff' simply reports whether files differ. The `-q' and
358`--brief' options select this output format.
359
360 This format is especially useful when comparing the contents of two
361directories. It is also much faster than doing the normal line by line
362comparisons, because `diff' can stop analyzing the files as soon as it
363knows that there are any differences.
364
365 You can also get a brief indication of whether two files differ by
366using `cmp'. For files that are identical, `cmp' produces no output.
367When the files differ, by default, `cmp' outputs the byte and line
368number where the first difference occurs. You can use the `-s' option
369to suppress that information, so that `cmp' produces no output and
370reports whether the files differ using only its exit status (*note
371Invoking cmp::).
372
373 Unlike `diff', `cmp' cannot compare directories; it can only compare
374two files.
375
376
377File: diff.info, Node: Binary, Prev: Brief, Up: Comparison
378
379Binary Files and Forcing Text Comparisons
380=========================================
381
382 If `diff' thinks that either of the two files it is comparing is
383binary (a non-text file), it normally treats that pair of files much as
384if the summary output format had been selected (*note Brief::), and
385reports only that the binary files are different. This is because line
386by line comparisons are usually not meaningful for binary files.
387
388 `diff' determines whether a file is text or binary by checking the
389first few bytes in the file; the exact number of bytes is system
390dependent, but it is typically several thousand. If every byte in that
391part of the file is non-null, `diff' considers the file to be text;
392otherwise it considers the file to be binary.
393
394 Sometimes you might want to force `diff' to consider files to be
395text. For example, you might be comparing text files that contain null
396characters; `diff' would erroneously decide that those are non-text
397files. Or you might be comparing documents that are in a format used
398by a word processing system that uses null characters to indicate
399special formatting. You can force `diff' to consider all files to be
400text files, and compare them line by line, by using the `-a' or
401`--text' option. If the files you compare using this option do not in
402fact contain text, they will probably contain few newline characters,
403and the `diff' output will consist of hunks showing differences between
404long lines of whatever characters the files contain.
405
406 You can also force `diff' to consider all files to be binary files,
407and report only whether they differ (but not how). Use the `-q' or
408`--brief' option for this.
409
410 Differing binary files are considered to cause trouble because the
411resulting `diff' output does not capture all the differences. This
412trouble causes `diff' to exit with status 2. However, this trouble
413cannot occur with the `--a' or `--text' option, or with the `-q' or
414`--brief' option, as these options both cause `diff' to treat binary
415files like text files.
416
417 In operating systems that distinguish between text and binary files,
418`diff' normally reads and writes all data as text. Use the `--binary'
419option to force `diff' to read and write binary data instead. This
420option has no effect on a POSIX-compliant system like GNU or
421traditional Unix. However, many personal computer operating systems
422represent the end of a line with a carriage return followed by a
423newline. On such systems, `diff' normally ignores these carriage
424returns on input and generates them at the end of each output line, but
425with the `--binary' option `diff' treats each carriage return as just
426another input character, and does not generate a carriage return at the
427end of each output line. This can be useful when dealing with non-text
428files that are meant to be interchanged with POSIX-compliant systems.
429
430 The `--strip-trailing-cr' causes `diff' to treat input lines that
431end in carriage return followed by newline as if they end in plain
432newline. This can be useful when comparing text that is imperfectly
433imported from many personal computer operating systems. This option
434affects how lines are read, which in turn affects how they are compared
435and output.
436
437 If you want to compare two files byte by byte, you can use the `cmp'
438program with the `-l' option to show the values of each differing byte
439in the two files. With GNU `cmp', you can also use the `-b' option to
440show the ASCII representation of those bytes. *Note Invoking cmp::,
441for more information.
442
443 If `diff3' thinks that any of the files it is comparing is binary (a