| 1 | This is diff.info, produced by makeinfo version 4.2 from diff.texi.
|
|---|
| 2 |
|
|---|
| 3 | This manual is for GNU Diffutils (version 2.8.1, 5 April 2002), and
|
|---|
| 4 | documents the GNU `diff', `diff3', `sdiff', and `cmp' commands for
|
|---|
| 5 | showing the differences between files and the GNU `patch' command for
|
|---|
| 6 | using their output to update files.
|
|---|
| 7 |
|
|---|
| 8 | Copyright (C) 1992, 1993, 1994, 1998, 2001, 2002 Free Software
|
|---|
| 9 | Foundation, 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 |
|
|---|
| 23 | INFO-DIR-SECTION Individual utilities
|
|---|
| 24 | START-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.
|
|---|
| 30 | END-INFO-DIR-ENTRY
|
|---|
| 31 |
|
|---|
| 32 | INFO-DIR-SECTION GNU packages
|
|---|
| 33 | START-INFO-DIR-ENTRY
|
|---|
| 34 | * Diff: (diff). Comparing and merging files.
|
|---|
| 35 | END-INFO-DIR-ENTRY
|
|---|
| 36 |
|
|---|
| 37 |
|
|---|
| 38 | File: diff.info, Node: Top, Next: Overview, Up: (dir)
|
|---|
| 39 |
|
|---|
| 40 | Comparing and Merging Files
|
|---|
| 41 | ***************************
|
|---|
| 42 |
|
|---|
| 43 | This manual is for GNU Diffutils (version 2.8.1, 5 April 2002), and
|
|---|
| 44 | documents the GNU `diff', `diff3', `sdiff', and `cmp' commands for
|
|---|
| 45 | showing the differences between files and the GNU `patch' command for
|
|---|
| 46 | using their output to update files.
|
|---|
| 47 |
|
|---|
| 48 | Copyright (C) 1992, 1993, 1994, 1998, 2001, 2002 Free Software
|
|---|
| 49 | Foundation, 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 |
|
|---|
| 95 | File: diff.info, Node: Overview, Next: Comparison, Prev: Top, Up: Top
|
|---|
| 96 |
|
|---|
| 97 | Overview
|
|---|
| 98 | ********
|
|---|
| 99 |
|
|---|
| 100 | Computer users often find occasion to ask how two files differ.
|
|---|
| 101 | Perhaps one file is a newer version of the other file. Or maybe the
|
|---|
| 102 | two files started out as identical copies but were changed by different
|
|---|
| 103 | people.
|
|---|
| 104 |
|
|---|
| 105 | You can use the `diff' command to show differences between two
|
|---|
| 106 | files, or each corresponding file in two directories. `diff' outputs
|
|---|
| 107 | differences between files line by line in any of several formats,
|
|---|
| 108 | selectable by command line options. This set of differences is often
|
|---|
| 109 | called a "diff" or "patch". For files that are identical, `diff'
|
|---|
| 110 | normally produces no output; for binary (non-text) files, `diff'
|
|---|
| 111 | normally reports only that they are different.
|
|---|
| 112 |
|
|---|
| 113 | You can use the `cmp' command to show the byte and line numbers
|
|---|
| 114 | where two files differ. `cmp' can also show all the bytes that differ
|
|---|
| 115 | between the two files, side by side. A way to compare two files
|
|---|
| 116 | character by character is the Emacs command `M-x compare-windows'.
|
|---|
| 117 | *Note Other Window: (emacs)Other Window, for more information on that
|
|---|
| 118 | command.
|
|---|
| 119 |
|
|---|
| 120 | You can use the `diff3' command to show differences among three
|
|---|
| 121 | files. When two people have made independent changes to a common
|
|---|
| 122 | original, `diff3' can report the differences between the original and
|
|---|
| 123 | the two changed versions, and can produce a merged file that contains
|
|---|
| 124 | both 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
|
|---|
| 129 | updates to text files (such as program source code) to other people.
|
|---|
| 130 | This method is especially useful when the differences are small compared
|
|---|
| 131 | to the complete files. Given `diff' output, you can use the `patch'
|
|---|
| 132 | program to update, or "patch", a copy of the file. If you think of
|
|---|
| 133 | `diff' as subtracting one file from another to produce their
|
|---|
| 134 | difference, you can think of `patch' as adding the difference to one
|
|---|
| 135 | file to reproduce the other.
|
|---|
| 136 |
|
|---|
| 137 | This manual first concentrates on making diffs, and later shows how
|
|---|
| 138 | to use diffs to update files.
|
|---|
| 139 |
|
|---|
| 140 | GNU `diff' was written by Paul Eggert, Mike Haertel, David Hayes,
|
|---|
| 141 | Richard Stallman, and Len Tower. Wayne Davison designed and
|
|---|
| 142 | implemented the unified output format. The basic algorithm is described
|
|---|
| 143 | in "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
|
|---|
| 145 | Comparison Program", Webb Miller and Eugene W. Myers,
|
|---|
| 146 | `Software--Practice and Experience' Vol. 15 No. 11, 1985, pp. 1025-1040.
|
|---|
| 147 | The algorithm was independently discovered as described in "Algorithms
|
|---|
| 148 | for Approximate String Matching", E. Ukkonen, `Information and Control'
|
|---|
| 149 | Vol. 64, 1985, pp. 100-118.
|
|---|
| 150 |
|
|---|
| 151 | GNU `diff3' was written by Randy Smith. GNU `sdiff' was written by
|
|---|
| 152 | Thomas Lord. GNU `cmp' was written by Torbjorn Granlund and David
|
|---|
| 153 | MacKenzie.
|
|---|
| 154 |
|
|---|
| 155 | `patch' was written mainly by Larry Wall and Paul Eggert; several
|
|---|
| 156 | GNU enhancements were contributed by Wayne Davison and David MacKenzie.
|
|---|
| 157 | Parts of this manual are adapted from a manual page written by Larry
|
|---|
| 158 | Wall, with his permission.
|
|---|
| 159 |
|
|---|
| 160 |
|
|---|
| 161 | File: diff.info, Node: Comparison, Next: Output Formats, Prev: Overview, Up: Top
|
|---|
| 162 |
|
|---|
| 163 | What Comparison Means
|
|---|
| 164 | *********************
|
|---|
| 165 |
|
|---|
| 166 | There are several ways to think about the differences between two
|
|---|
| 167 | files. One way to think of the differences is as a series of lines
|
|---|
| 168 | that were deleted from, inserted in, or changed in one file to produce
|
|---|
| 169 | the other file. `diff' compares two files line by line, finds groups of
|
|---|
| 170 | lines that differ, and reports each group of differing lines. It can
|
|---|
| 171 | report the differing lines in several formats, which have different
|
|---|
| 172 | purposes.
|
|---|
| 173 |
|
|---|
| 174 | GNU `diff' can show whether files are different without detailing
|
|---|
| 175 | the differences. It also provides ways to suppress certain kinds of
|
|---|
| 176 | differences that are not important to you. Most commonly, such
|
|---|
| 177 | differences are changes in the amount of white space between words or
|
|---|
| 178 | lines. `diff' also provides ways to suppress differences in alphabetic
|
|---|
| 179 | case or in lines that match a regular expression that you provide.
|
|---|
| 180 | These options can accumulate; for example, you can ignore changes in
|
|---|
| 181 | both white space and alphabetic case.
|
|---|
| 182 |
|
|---|
| 183 | Another way to think of the differences between two files is as a
|
|---|
| 184 | sequence of pairs of bytes that can be either identical or different.
|
|---|
| 185 | `cmp' reports the differences between two files byte by byte, instead
|
|---|
| 186 | of line by line. As a result, it is often more useful than `diff' for
|
|---|
| 187 | comparing binary files. For text files, `cmp' is useful mainly when
|
|---|
| 188 | you want to know only whether two files are identical, or whether one
|
|---|
| 189 | file is a prefix of the other.
|
|---|
| 190 |
|
|---|
| 191 | To illustrate the effect that considering changes byte by byte can
|
|---|
| 192 | have compared with considering them line by line, think of what happens
|
|---|
| 193 | if a single newline character is added to the beginning of a file. If
|
|---|
| 194 | that file is then compared with an otherwise identical file that lacks
|
|---|
| 195 | the newline at the beginning, `diff' will report that a blank line has
|
|---|
| 196 | been added to the file, while `cmp' will report that almost every byte
|
|---|
| 197 | of the two files differs.
|
|---|
| 198 |
|
|---|
| 199 | `diff3' normally compares three input files line by line, finds
|
|---|
| 200 | groups of lines that differ, and reports each group of differing lines.
|
|---|
| 201 | Its output is designed to make it easy to inspect two different sets of
|
|---|
| 202 | changes 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 |
|
|---|
| 215 | File: diff.info, Node: Hunks, Next: White Space, Up: Comparison
|
|---|
| 216 |
|
|---|
| 217 | Hunks
|
|---|
| 218 | =====
|
|---|
| 219 |
|
|---|
| 220 | When comparing two files, `diff' finds sequences of lines common to
|
|---|
| 221 | both files, interspersed with groups of differing lines called "hunks".
|
|---|
| 222 | Comparing two identical files yields one sequence of common lines and
|
|---|
| 223 | no hunks, because no lines differ. Comparing two entirely different
|
|---|
| 224 | files yields no common lines and one large hunk that contains all lines
|
|---|
| 225 | of both files. In general, there are many ways to match up lines
|
|---|
| 226 | between two given files. `diff' tries to minimize the total hunk size
|
|---|
| 227 | by finding large sequences of common lines interspersed with small
|
|---|
| 228 | hunks 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 |
|
|---|
| 242 | But if `diff' notices the common line `b' instead, it produces this
|
|---|
| 243 | output:
|
|---|
| 244 |
|
|---|
| 245 | 1c1
|
|---|
| 246 | < a
|
|---|
| 247 | ---
|
|---|
| 248 | > c
|
|---|
| 249 | 3c3
|
|---|
| 250 | < c
|
|---|
| 251 | ---
|
|---|
| 252 | > a
|
|---|
| 253 |
|
|---|
| 254 | It is also possible to find `a' as the common line. `diff' does not
|
|---|
| 255 | always find an optimal matching between the files; it takes shortcuts
|
|---|
| 256 | to run faster. But its output is usually close to the shortest
|
|---|
| 257 | possible. You can adjust this tradeoff with the `--minimal' option
|
|---|
| 258 | (*note diff Performance::).
|
|---|
| 259 |
|
|---|
| 260 |
|
|---|
| 261 | File: diff.info, Node: White Space, Next: Blank Lines, Prev: Hunks, Up: Comparison
|
|---|
| 262 |
|
|---|
| 263 | Suppressing Differences in Blank and Tab Spacing
|
|---|
| 264 | ================================================
|
|---|
| 265 |
|
|---|
| 266 | The `-E' and `--ignore-tab-expansion' options ignore the distinction
|
|---|
| 267 | between tabs and spaces on input. A tab is considered to be equivalent
|
|---|
| 268 | to the number of spaces to the next tab stop. `diff' assumes that tab
|
|---|
| 269 | stops are set every 8 print columns.
|
|---|
| 270 |
|
|---|
| 271 | The `-b' and `--ignore-space-change' options are stronger. They
|
|---|
| 272 | ignore white space at line end, and consider all other sequences of one
|
|---|
| 273 | or more white space characters to be equivalent. With these options,
|
|---|
| 274 | `diff' considers the following two lines to be equivalent, where `$'
|
|---|
| 275 | denotes 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
|
|---|
| 281 | ignore difference even if one line has white space where the other line
|
|---|
| 282 | has none. "White space" characters include tab, newline, vertical tab,
|
|---|
| 283 | form feed, carriage return, and space; some locales may define
|
|---|
| 284 | additional characters to be white space. With these options, `diff'
|
|---|
| 285 | considers the following two lines to be equivalent, where `$' denotes
|
|---|
| 286 | the 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 |
|
|---|
| 292 | File: diff.info, Node: Blank Lines, Next: Case Folding, Prev: White Space, Up: Comparison
|
|---|
| 293 |
|
|---|
| 294 | Suppressing Differences in Blank Lines
|
|---|
| 295 | ======================================
|
|---|
| 296 |
|
|---|
| 297 | The `-B' and `--ignore-blank-lines' options ignore insertions or
|
|---|
| 298 | deletions of blank lines. These options affect only lines that are
|
|---|
| 299 | completely empty; they do not affect lines that look empty but contain
|
|---|
| 300 | space or tab characters. With these options, for example, a file
|
|---|
| 301 | containing
|
|---|
| 302 | 1. A point is that which has no part.
|
|---|
| 303 |
|
|---|
| 304 | 2. A line is breadthless length.
|
|---|
| 305 | -- Euclid, The Elements, I
|
|---|
| 306 |
|
|---|
| 307 | is 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 |
|
|---|
| 315 | File: diff.info, Node: Case Folding, Next: Specified Folding, Prev: Blank Lines, Up: Comparison
|
|---|
| 316 |
|
|---|
| 317 | Suppressing Case Differences
|
|---|
| 318 | ============================
|
|---|
| 319 |
|
|---|
| 320 | GNU `diff' can treat lower case letters as equivalent to their upper
|
|---|
| 321 | case counterparts, so that, for example, it considers `Funky Stuff',
|
|---|
| 322 | `funky STUFF', and `fUNKy stuFf' to all be the same. To request this,
|
|---|
| 323 | use the `-i' or `--ignore-case' option.
|
|---|
| 324 |
|
|---|
| 325 |
|
|---|
| 326 | File: diff.info, Node: Specified Folding, Next: Brief, Prev: Case Folding, Up: Comparison
|
|---|
| 327 |
|
|---|
| 328 | Suppressing 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
|
|---|
| 334 | expressions that contain shell metacharacters to prevent the shell from
|
|---|
| 335 | expanding them. For example, `diff -I '^[[:digit:]]'' ignores all
|
|---|
| 336 | changes to lines beginning with a digit.
|
|---|
| 337 |
|
|---|
| 338 | However, `-I' only ignores the insertion or deletion of lines that
|
|---|
| 339 | contain the regular expression if every changed line in the hunk--every
|
|---|
| 340 | insertion and every deletion--matches the regular expression. In other
|
|---|
| 341 | words, for each nonignorable change, `diff' prints the complete set of
|
|---|
| 342 | changes in its vicinity, including the ignorable ones.
|
|---|
| 343 |
|
|---|
| 344 | You can specify more than one regular expression for lines to ignore
|
|---|
| 345 | by using more than one `-I' option. `diff' tries to match each line
|
|---|
| 346 | against each regular expression.
|
|---|
| 347 |
|
|---|
| 348 |
|
|---|
| 349 | File: diff.info, Node: Brief, Next: Binary, Prev: Specified Folding, Up: Comparison
|
|---|
| 350 |
|
|---|
| 351 | Summarizing Which Files Differ
|
|---|
| 352 | ==============================
|
|---|
| 353 |
|
|---|
| 354 | When you only want to find out whether files are different, and you
|
|---|
| 355 | don't care what the differences are, you can use the summary output
|
|---|
| 356 | format. In this format, instead of showing the differences between the
|
|---|
| 357 | files, `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
|
|---|
| 361 | directories. It is also much faster than doing the normal line by line
|
|---|
| 362 | comparisons, because `diff' can stop analyzing the files as soon as it
|
|---|
| 363 | knows that there are any differences.
|
|---|
| 364 |
|
|---|
| 365 | You can also get a brief indication of whether two files differ by
|
|---|
| 366 | using `cmp'. For files that are identical, `cmp' produces no output.
|
|---|
| 367 | When the files differ, by default, `cmp' outputs the byte and line
|
|---|
| 368 | number where the first difference occurs. You can use the `-s' option
|
|---|
| 369 | to suppress that information, so that `cmp' produces no output and
|
|---|
| 370 | reports whether the files differ using only its exit status (*note
|
|---|
| 371 | Invoking cmp::).
|
|---|
| 372 |
|
|---|
| 373 | Unlike `diff', `cmp' cannot compare directories; it can only compare
|
|---|
| 374 | two files.
|
|---|
| 375 |
|
|---|
| 376 |
|
|---|
| 377 | File: diff.info, Node: Binary, Prev: Brief, Up: Comparison
|
|---|
| 378 |
|
|---|
| 379 | Binary Files and Forcing Text Comparisons
|
|---|
| 380 | =========================================
|
|---|
| 381 |
|
|---|
| 382 | If `diff' thinks that either of the two files it is comparing is
|
|---|
| 383 | binary (a non-text file), it normally treats that pair of files much as
|
|---|
| 384 | if the summary output format had been selected (*note Brief::), and
|
|---|
| 385 | reports only that the binary files are different. This is because line
|
|---|
| 386 | by line comparisons are usually not meaningful for binary files.
|
|---|
| 387 |
|
|---|
| 388 | `diff' determines whether a file is text or binary by checking the
|
|---|
| 389 | first few bytes in the file; the exact number of bytes is system
|
|---|
| 390 | dependent, but it is typically several thousand. If every byte in that
|
|---|
| 391 | part of the file is non-null, `diff' considers the file to be text;
|
|---|
| 392 | otherwise it considers the file to be binary.
|
|---|
| 393 |
|
|---|
| 394 | Sometimes you might want to force `diff' to consider files to be
|
|---|
| 395 | text. For example, you might be comparing text files that contain null
|
|---|
| 396 | characters; `diff' would erroneously decide that those are non-text
|
|---|
| 397 | files. Or you might be comparing documents that are in a format used
|
|---|
| 398 | by a word processing system that uses null characters to indicate
|
|---|
| 399 | special formatting. You can force `diff' to consider all files to be
|
|---|
| 400 | text 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
|
|---|
| 402 | fact contain text, they will probably contain few newline characters,
|
|---|
| 403 | and the `diff' output will consist of hunks showing differences between
|
|---|
| 404 | long lines of whatever characters the files contain.
|
|---|
| 405 |
|
|---|
| 406 | You can also force `diff' to consider all files to be binary files,
|
|---|
| 407 | and 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
|
|---|
| 411 | resulting `diff' output does not capture all the differences. This
|
|---|
| 412 | trouble causes `diff' to exit with status 2. However, this trouble
|
|---|
| 413 | cannot occur with the `--a' or `--text' option, or with the `-q' or
|
|---|
| 414 | `--brief' option, as these options both cause `diff' to treat binary
|
|---|
| 415 | files 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'
|
|---|
| 419 | option to force `diff' to read and write binary data instead. This
|
|---|
| 420 | option has no effect on a POSIX-compliant system like GNU or
|
|---|
| 421 | traditional Unix. However, many personal computer operating systems
|
|---|
| 422 | represent the end of a line with a carriage return followed by a
|
|---|
| 423 | newline. On such systems, `diff' normally ignores these carriage
|
|---|
| 424 | returns on input and generates them at the end of each output line, but
|
|---|
| 425 | with the `--binary' option `diff' treats each carriage return as just
|
|---|
| 426 | another input character, and does not generate a carriage return at the
|
|---|
| 427 | end of each output line. This can be useful when dealing with non-text
|
|---|
| 428 | files that are meant to be interchanged with POSIX-compliant systems.
|
|---|
| 429 |
|
|---|
| 430 | The `--strip-trailing-cr' causes `diff' to treat input lines that
|
|---|
| 431 | end in carriage return followed by newline as if they end in plain
|
|---|
| 432 | newline. This can be useful when comparing text that is imperfectly
|
|---|
| 433 | imported from many personal computer operating systems. This option
|
|---|
| 434 | affects how lines are read, which in turn affects how they are compared
|
|---|
| 435 | and output.
|
|---|
| 436 |
|
|---|
| 437 | If you want to compare two files byte by byte, you can use the `cmp'
|
|---|
| 438 | program with the `-l' option to show the values of each differing byte
|
|---|
| 439 | in the two files. With GNU `cmp', you can also use the `-b' option to
|
|---|
| 440 | show the ASCII representation of those bytes. *Note Invoking cmp::,
|
|---|
| 441 | for more information.
|
|---|
| 442 |
|
|---|
| 443 | If `diff3' thinks that any of the files it is comparing is binary (a
|
|---|
| 444 | non-text file), it normally reports an error, because such comparisons
|
|---|
| 445 | are usually not useful. `diff3' uses the same test as `diff' to decide
|
|---|
| 446 | whether a file is binary. As with `diff', if the input files contain a
|
|---|
| 447 | few non-text bytes but otherwise are like text files, you can force
|
|---|
| 448 | `diff3' to consider all files to be text files and compare them line by
|
|---|
| 449 | line by using the `-a' or `--text' options.
|
|---|
| 450 |
|
|---|
| 451 |
|
|---|
| 452 | File: diff.info, Node: Output Formats, Next: Incomplete Lines, Prev: Comparison, Up: Top
|
|---|
| 453 |
|
|---|
| 454 | `diff' Output Formats
|
|---|
| 455 | *********************
|
|---|
| 456 |
|
|---|
| 457 | `diff' has several mutually exclusive options for output format.
|
|---|
| 458 | The following sections describe each format, illustrating how `diff'
|
|---|
| 459 | reports the differences between two sample input files.
|
|---|
| 460 |
|
|---|
| 461 | * Menu:
|
|---|
| 462 |
|
|---|
| 463 | * Sample diff Input:: Sample `diff' input files for examples.
|
|---|
| 464 | * Normal:: Showing differences without surrounding text.
|
|---|
| 465 | * Context:: Showing differences with the surrounding text.
|
|---|
| 466 | * Side by Side:: Showing differences in two columns.
|
|---|
| 467 | * Scripts:: Generating scripts for other programs.
|
|---|
| 468 | * If-then-else:: Merging files with if-then-else.
|
|---|
| 469 |
|
|---|
| 470 |
|
|---|
| 471 | File: diff.info, Node: Sample diff Input, Next: Normal, Up: Output Formats
|
|---|
| 472 |
|
|---|
| 473 | Two Sample Input Files
|
|---|
| 474 | ======================
|
|---|
| 475 |
|
|---|
| 476 | Here are two sample files that we will use in numerous examples to
|
|---|
| 477 | illustrate the output of `diff' and how various options can change it.
|
|---|
| 478 |
|
|---|
| 479 | This is the file `lao':
|
|---|
| 480 |
|
|---|
| 481 | The Way that can be told of is not the eternal Way;
|
|---|
| 482 | The name that can be named is not the eternal name.
|
|---|
| 483 | The Nameless is the origin of Heaven and Earth;
|
|---|
| 484 | The Named is the mother of all things.
|
|---|
| 485 | Therefore let there always be non-being,
|
|---|
| 486 | so we may see their subtlety,
|
|---|
| 487 | And let there always be being,
|
|---|
| 488 | so we may see their outcome.
|
|---|
| 489 | The two are the same,
|
|---|
| 490 | But after they are produced,
|
|---|
| 491 | they have different names.
|
|---|
| 492 |
|
|---|
| 493 | This is the file `tzu':
|
|---|
| 494 |
|
|---|
| 495 | The Nameless is the origin of Heaven and Earth;
|
|---|
| 496 | The named is the mother of all things.
|
|---|
| 497 |
|
|---|
| 498 | Therefore let there always be non-being,
|
|---|
| 499 | so we may see their subtlety,
|
|---|
| 500 | And let there always be being,
|
|---|
| 501 | so we may see their outcome.
|
|---|
| 502 | The two are the same,
|
|---|
| 503 | But after they are produced,
|
|---|
| 504 | they have different names.
|
|---|
| 505 | They both may be called deep and profound.
|
|---|
| 506 | Deeper and more profound,
|
|---|
| 507 | The door of all subtleties!
|
|---|
| 508 |
|
|---|
| 509 | In this example, the first hunk contains just the first two lines of
|
|---|
| 510 | `lao', the second hunk contains the fourth line of `lao' opposing the
|
|---|
| 511 | second and third lines of `tzu', and the last hunk contains just the
|
|---|
| 512 | last three lines of `tzu'.
|
|---|
| 513 |
|
|---|
| 514 |
|
|---|
| 515 | File: diff.info, Node: Normal, Next: Context, Prev: Sample diff Input, Up: Output Formats
|
|---|
| 516 |
|
|---|
| 517 | Showing Differences Without Context
|
|---|
| 518 | ===================================
|
|---|
| 519 |
|
|---|
| 520 | The "normal" `diff' output format shows each hunk of differences
|
|---|
| 521 | without any surrounding context. Sometimes such output is the clearest
|
|---|
| 522 | way to see how lines have changed, without the clutter of nearby
|
|---|
| 523 | unchanged lines (although you can get similar results with the context
|
|---|
| 524 | or unified formats by using 0 lines of context). However, this format
|
|---|
| 525 | is no longer widely used for sending out patches; for that purpose, the
|
|---|
| 526 | context format (*note Context Format::) and the unified format (*note
|
|---|
| 527 | Unified Format::) are superior. Normal format is the default for
|
|---|
| 528 | compatibility with older versions of `diff' and the POSIX standard.
|
|---|
| 529 | Use the `--normal' option to select this output format explicitly.
|
|---|
| 530 |
|
|---|
| 531 | * Menu:
|
|---|
| 532 |
|
|---|
| 533 | * Detailed Normal:: A detailed description of normal output format.
|
|---|
| 534 | * Example Normal:: Sample output in the normal format.
|
|---|
| 535 |
|
|---|
| 536 |
|
|---|
| 537 | File: diff.info, Node: Detailed Normal, Next: Example Normal, Up: Normal
|
|---|
| 538 |
|
|---|
| 539 | Detailed Description of Normal Format
|
|---|
| 540 | -------------------------------------
|
|---|
| 541 |
|
|---|
| 542 | The normal output format consists of one or more hunks of
|
|---|
| 543 | differences; each hunk shows one area where the files differ. Normal
|
|---|
| 544 | format hunks look like this:
|
|---|
| 545 |
|
|---|
| 546 | CHANGE-COMMAND
|
|---|
| 547 | < FROM-FILE-LINE
|
|---|
| 548 | < FROM-FILE-LINE...
|
|---|
| 549 | ---
|
|---|
| 550 | > TO-FILE-LINE
|
|---|
| 551 | > TO-FILE-LINE...
|
|---|
| 552 |
|
|---|
| 553 | There are three types of change commands. Each consists of a line
|
|---|
| 554 | number or comma-separated range of lines in the first file, a single
|
|---|
| 555 | character indicating the kind of change to make, and a line number or
|
|---|
| 556 | comma-separated range of lines in the second file. All line numbers are
|
|---|
| 557 | the original line numbers in each file. The types of change commands
|
|---|
| 558 | are:
|
|---|
| 559 |
|
|---|
| 560 | `LaR'
|
|---|
| 561 | Add the lines in range R of the second file after line L of the
|
|---|
| 562 | first file. For example, `8a12,15' means append lines 12-15 of
|
|---|
| 563 | file 2 after line 8 of file 1; or, if changing file 2 into file 1,
|
|---|
| 564 | delete lines 12-15 of file 2.
|
|---|
| 565 |
|
|---|
| 566 | `FcT'
|
|---|
| 567 | Replace the lines in range F of the first file with lines in range
|
|---|
| 568 | T of the second file. This is like a combined add and delete, but
|
|---|
| 569 | more compact. For example, `5,7c8,10' means change lines 5-7 of
|
|---|
| 570 | file 1 to read as lines 8-10 of file 2; or, if changing file 2 into
|
|---|
| 571 | file 1, change lines 8-10 of file 2 to read as lines 5-7 of file 1.
|
|---|
| 572 |
|
|---|
| 573 | `RdL'
|
|---|
| 574 | Delete the lines in range R from the first file; line L is where
|
|---|
| 575 | they would have appeared in the second file had they not been
|
|---|
| 576 | deleted. For example, `5,7d3' means delete lines 5-7 of file 1;
|
|---|
| 577 | or, if changing file 2 into file 1, append lines 5-7 of file 1
|
|---|
| 578 | after line 3 of file 2.
|
|---|
| 579 |
|
|---|
| 580 |
|
|---|
| 581 | File: diff.info, Node: Example Normal, Prev: Detailed Normal, Up: Normal
|
|---|
| 582 |
|
|---|
| 583 | An Example of Normal Format
|
|---|
| 584 | ---------------------------
|
|---|
| 585 |
|
|---|
| 586 | Here is the output of the command `diff lao tzu' (*note Sample diff
|
|---|
| 587 | Input::, for the complete contents of the two files). Notice that it
|
|---|
| 588 | shows only the lines that are different between the two files.
|
|---|
| 589 |
|
|---|
| 590 | 1,2d0
|
|---|
| 591 | < The Way that can be told of is not the eternal Way;
|
|---|
| 592 | < The name that can be named is not the eternal name.
|
|---|
| 593 | 4c2,3
|
|---|
| 594 | < The Named is the mother of all things.
|
|---|
| 595 | ---
|
|---|
| 596 | > The named is the mother of all things.
|
|---|
| 597 | >
|
|---|
| 598 | 11a11,13
|
|---|
| 599 | > They both may be called deep and profound.
|
|---|
| 600 | > Deeper and more profound,
|
|---|
| 601 | > The door of all subtleties!
|
|---|
| 602 |
|
|---|
| 603 |
|
|---|
| 604 | File: diff.info, Node: Context, Next: Side by Side, Prev: Normal, Up: Output Formats
|
|---|
| 605 |
|
|---|
| 606 | Showing Differences in Their Context
|
|---|
| 607 | ====================================
|
|---|
| 608 |
|
|---|
| 609 | Usually, when you are looking at the differences between files, you
|
|---|
| 610 | will also want to see the parts of the files near the lines that
|
|---|
| 611 | differ, to help you understand exactly what has changed. These nearby
|
|---|
| 612 | parts of the files are called the "context".
|
|---|
| 613 |
|
|---|
| 614 | GNU `diff' provides two output formats that show context around the
|
|---|
| 615 | differing lines: "context format" and "unified format". It can
|
|---|
| 616 | optionally show in which function or section of the file the differing
|
|---|
| 617 | lines are found.
|
|---|
| 618 |
|
|---|
| 619 | If you are distributing new versions of files to other people in the
|
|---|
| 620 | form of `diff' output, you should use one of the output formats that
|
|---|
| 621 | show context so that they can apply the diffs even if they have made
|
|---|
| 622 | small changes of their own to the files. `patch' can apply the diffs
|
|---|
| 623 | in this case by searching in the files for the lines of context around
|
|---|
| 624 | the differing lines; if those lines are actually a few lines away from
|
|---|
| 625 | where the diff says they are, `patch' can adjust the line numbers
|
|---|
| 626 | accordingly and still apply the diff correctly. *Note Imperfect::, for
|
|---|
| 627 | more information on using `patch' to apply imperfect diffs.
|
|---|
| 628 |
|
|---|
| 629 | * Menu:
|
|---|
| 630 |
|
|---|
| 631 | * Context Format:: An output format that shows surrounding lines.
|
|---|
| 632 | * Unified Format:: A more compact output format that shows context.
|
|---|
| 633 | * Sections:: Showing which sections of the files differences are in.
|
|---|
| 634 | * Alternate Names:: Showing alternate file names in context headers.
|
|---|
| 635 |
|
|---|
| 636 |
|
|---|
| 637 | File: diff.info, Node: Context Format, Next: Unified Format, Up: Context
|
|---|
| 638 |
|
|---|
| 639 | Context Format
|
|---|
| 640 | --------------
|
|---|
| 641 |
|
|---|
| 642 | The context output format shows several lines of context around the
|
|---|
| 643 | lines that differ. It is the standard format for distributing updates
|
|---|
| 644 | to source code.
|
|---|
| 645 |
|
|---|
| 646 | To select this output format, use the `-C LINES',
|
|---|
| 647 | `--context[=LINES]', or `-c' option. The argument LINES that some of
|
|---|
| 648 | these options take is the number of lines of context to show. If you
|
|---|
| 649 | do not specify LINES, it defaults to three. For proper operation,
|
|---|
| 650 | `patch' typically needs at least two lines of context.
|
|---|
| 651 |
|
|---|
| 652 | * Menu:
|
|---|
| 653 |
|
|---|
| 654 | * Detailed Context:: A detailed description of the context output format.
|
|---|
| 655 | * Example Context:: Sample output in context format.
|
|---|
| 656 | * Less Context:: Another sample with less context.
|
|---|
| 657 |
|
|---|
| 658 |
|
|---|
| 659 | File: diff.info, Node: Detailed Context, Next: Example Context, Up: Context Format
|
|---|
| 660 |
|
|---|
| 661 | Detailed Description of Context Format
|
|---|
| 662 | ......................................
|
|---|
| 663 |
|
|---|
| 664 | The context output format starts with a two-line header, which looks
|
|---|
| 665 | like this:
|
|---|
| 666 |
|
|---|
| 667 | *** FROM-FILE FROM-FILE-MODIFICATION-TIME
|
|---|
| 668 | --- TO-FILE TO-FILE-MODIFICATION TIME
|
|---|
| 669 |
|
|---|
| 670 | The time stamp normally looks like `2002-02-21 23:30:39.942229878
|
|---|
| 671 | -0800' to indicate the date, time with fractional seconds, and time
|
|---|
| 672 | zone in Internet RFC 2822 format
|
|---|
| 673 | (ftp://ftp.isi.edu/in-notes/rfc2822.txt). However, a traditional time
|
|---|
| 674 | stamp like `Thu Feb 21 23:30:39 2002' is used if the `LC_TIME' locale
|
|---|
| 675 | category is either `C' or `POSIX'.
|
|---|
| 676 |
|
|---|
| 677 | You can change the header's content with the `--label=LABEL' option;
|
|---|
| 678 | see *Note Alternate Names::.
|
|---|
| 679 |
|
|---|
| 680 | Next come one or more hunks of differences; each hunk shows one area
|
|---|
| 681 | where the files differ. Context format hunks look like this:
|
|---|
| 682 |
|
|---|
| 683 | ***************
|
|---|
| 684 | *** FROM-FILE-LINE-RANGE ****
|
|---|
| 685 | FROM-FILE-LINE
|
|---|
| 686 | FROM-FILE-LINE...
|
|---|
| 687 | --- TO-FILE-LINE-RANGE ----
|
|---|
| 688 | TO-FILE-LINE
|
|---|
| 689 | TO-FILE-LINE...
|
|---|
| 690 |
|
|---|
| 691 | The lines of context around the lines that differ start with two
|
|---|
| 692 | space characters. The lines that differ between the two files start
|
|---|
| 693 | with one of the following indicator characters, followed by a space
|
|---|
| 694 | character:
|
|---|
| 695 |
|
|---|
| 696 | `!'
|
|---|
| 697 | A line that is part of a group of one or more lines that changed
|
|---|
| 698 | between the two files. There is a corresponding group of lines
|
|---|
| 699 | marked with `!' in the part of this hunk for the other file.
|
|---|
| 700 |
|
|---|
| 701 | `+'
|
|---|
| 702 | An "inserted" line in the second file that corresponds to nothing
|
|---|
| 703 | in the first file.
|
|---|
| 704 |
|
|---|
| 705 | `-'
|
|---|
| 706 | A "deleted" line in the first file that corresponds to nothing in
|
|---|
| 707 | the second file.
|
|---|
| 708 |
|
|---|
| 709 | If all of the changes in a hunk are insertions, the lines of
|
|---|
| 710 | FROM-FILE are omitted. If all of the changes are deletions, the lines
|
|---|
| 711 | of TO-FILE are omitted.
|
|---|
| 712 |
|
|---|
| 713 |
|
|---|
| 714 | File: diff.info, Node: Example Context, Next: Less Context, Prev: Detailed Context, Up: Context Format
|
|---|
| 715 |
|
|---|
| 716 | An Example of Context Format
|
|---|
| 717 | ............................
|
|---|
| 718 |
|
|---|
| 719 | Here is the output of `diff -c lao tzu' (*note Sample diff Input::,
|
|---|
| 720 | for the complete contents of the two files). Notice that up to three
|
|---|
| 721 | lines that are not different are shown around each line that is
|
|---|
|
|---|