118fd37a7SXin LI\input texinfo @c -*-texinfo-*- 218fd37a7SXin LI@comment $Id: diff.texi,v 1.25 2004/04/12 07:44:35 eggert Exp $ 318fd37a7SXin LI@comment %**start of header 418fd37a7SXin LI@setfilename diff.info 518fd37a7SXin LI@include version.texi 618fd37a7SXin LI@settitle Comparing and Merging Files 718fd37a7SXin LI@syncodeindex vr cp 818fd37a7SXin LI@setchapternewpage odd 918fd37a7SXin LI@comment %**end of header 1018fd37a7SXin LI@copying 1118fd37a7SXin LIThis manual is for GNU Diffutils 1218fd37a7SXin LI(version @value{VERSION}, @value{UPDATED}), 1318fd37a7SXin LIand documents the @acronym{GNU} @command{diff}, @command{diff3}, 1418fd37a7SXin LI@command{sdiff}, and @command{cmp} commands for showing the 1518fd37a7SXin LIdifferences between files and the @acronym{GNU} @command{patch} command for 1618fd37a7SXin LIusing their output to update files. 1718fd37a7SXin LI 1818fd37a7SXin LICopyright @copyright{} 1992, 1993, 1994, 1998, 2001, 2002, 2004 Free 1918fd37a7SXin LISoftware Foundation, Inc. 2018fd37a7SXin LI 2118fd37a7SXin LI@quotation 2218fd37a7SXin LIPermission is granted to copy, distribute and/or modify this document 2318fd37a7SXin LIunder the terms of the GNU Free Documentation License, Version 1.1 or 2418fd37a7SXin LIany later version published by the Free Software Foundation; with no 2518fd37a7SXin LIInvariant Sections, with the Front-Cover texts being ``A GNU Manual,'' 2618fd37a7SXin LIand with the Back-Cover Texts as in (a) below. A copy of the 2718fd37a7SXin LIlicense is included in the section entitled ``GNU Free Documentation 2818fd37a7SXin LILicense.'' 2918fd37a7SXin LI 3018fd37a7SXin LI(a) The FSF's Back-Cover Text is: ``You have freedom to copy and modify 3118fd37a7SXin LIthis GNU Manual, like GNU software. Copies published by the Free 3218fd37a7SXin LISoftware Foundation raise funds for GNU development.'' 3318fd37a7SXin LI@end quotation 3418fd37a7SXin LI@end copying 3518fd37a7SXin LI 3618fd37a7SXin LI@c Debian install-info (up through at least version 1.9.20) uses only the 3718fd37a7SXin LI@c first dircategory. Put this one first, as it is more useful in practice. 3818fd37a7SXin LI@dircategory Individual utilities 3918fd37a7SXin LI@direntry 4018fd37a7SXin LI* cmp: (diff)Invoking cmp. Compare 2 files byte by byte. 4118fd37a7SXin LI* diff: (diff)Invoking diff. Compare 2 files line by line. 4218fd37a7SXin LI* diff3: (diff)Invoking diff3. Compare 3 files line by line. 4318fd37a7SXin LI* patch: (diff)Invoking patch. Apply a patch to a file. 4418fd37a7SXin LI* sdiff: (diff)Invoking sdiff. Merge 2 files side-by-side. 4518fd37a7SXin LI@end direntry 4618fd37a7SXin LI 4718fd37a7SXin LI@dircategory Text creation and manipulation 4818fd37a7SXin LI@direntry 4918fd37a7SXin LI* Diff: (diff). Comparing and merging files. 5018fd37a7SXin LI@end direntry 5118fd37a7SXin LI 5218fd37a7SXin LI@titlepage 5318fd37a7SXin LI@title Comparing and Merging Files 5418fd37a7SXin LI@subtitle for Diffutils @value{VERSION} and @code{patch} 2.5.4 5518fd37a7SXin LI@subtitle @value{UPDATED} 5618fd37a7SXin LI@author David MacKenzie, Paul Eggert, and Richard Stallman 5718fd37a7SXin LI@page 5818fd37a7SXin LI@vskip 0pt plus 1filll 5918fd37a7SXin LI@insertcopying 6018fd37a7SXin LI@end titlepage 6118fd37a7SXin LI 6218fd37a7SXin LI@shortcontents 6318fd37a7SXin LI@contents 6418fd37a7SXin LI 6518fd37a7SXin LI@ifnottex 6618fd37a7SXin LI@node Top 6718fd37a7SXin LI@top Comparing and Merging Files 6818fd37a7SXin LI 6918fd37a7SXin LI@insertcopying 7018fd37a7SXin LI@end ifnottex 7118fd37a7SXin LI 7218fd37a7SXin LI@menu 7318fd37a7SXin LI* Overview:: Preliminary information. 7418fd37a7SXin LI* Comparison:: What file comparison means. 7518fd37a7SXin LI 7618fd37a7SXin LI* Output Formats:: Formats for two-way difference reports. 7718fd37a7SXin LI* Incomplete Lines:: Lines that lack trailing newlines. 7818fd37a7SXin LI* Comparing Directories:: Comparing files and directories. 7918fd37a7SXin LI* Adjusting Output:: Making @command{diff} output prettier. 8018fd37a7SXin LI* diff Performance:: Making @command{diff} smarter or faster. 8118fd37a7SXin LI 8218fd37a7SXin LI* Comparing Three Files:: Formats for three-way difference reports. 8318fd37a7SXin LI* diff3 Merging:: Merging from a common ancestor. 8418fd37a7SXin LI 8518fd37a7SXin LI* Interactive Merging:: Interactive merging with @command{sdiff}. 8618fd37a7SXin LI 8718fd37a7SXin LI* Merging with patch:: Using @command{patch} to change old files into new ones. 8818fd37a7SXin LI* Making Patches:: Tips for making and using patch distributions. 8918fd37a7SXin LI 9018fd37a7SXin LI* Invoking cmp:: Compare two files byte by byte. 9118fd37a7SXin LI* Invoking diff:: Compare two files line by line. 9218fd37a7SXin LI* Invoking diff3:: Compare three files line by line. 9318fd37a7SXin LI* Invoking patch:: Apply a diff file to an original. 9418fd37a7SXin LI* Invoking sdiff:: Side-by-side merge of file differences. 9518fd37a7SXin LI 9618fd37a7SXin LI* Standards conformance:: Conformance to the @acronym{POSIX} standard. 9718fd37a7SXin LI* Projects:: If you've found a bug or other shortcoming. 9818fd37a7SXin LI 9918fd37a7SXin LI* Copying This Manual:: How to make copies of this manual. 10018fd37a7SXin LI* Translations:: Available translations of this manual. 10118fd37a7SXin LI* Index:: Index. 10218fd37a7SXin LI@end menu 10318fd37a7SXin LI 10418fd37a7SXin LI@node Overview 10518fd37a7SXin LI@unnumbered Overview 10618fd37a7SXin LI@cindex overview of @command{diff} and @command{patch} 10718fd37a7SXin LI 10818fd37a7SXin LIComputer users often find occasion to ask how two files differ. Perhaps 10918fd37a7SXin LIone file is a newer version of the other file. Or maybe the two files 11018fd37a7SXin LIstarted out as identical copies but were changed by different people. 11118fd37a7SXin LI 11218fd37a7SXin LIYou can use the @command{diff} command to show differences between two 11318fd37a7SXin LIfiles, or each corresponding file in two directories. @command{diff} 11418fd37a7SXin LIoutputs differences between files line by line in any of several 11518fd37a7SXin LIformats, selectable by command line options. This set of differences is 11618fd37a7SXin LIoften called a @dfn{diff} or @dfn{patch}. For files that are identical, 11718fd37a7SXin LI@command{diff} normally produces no output; for binary (non-text) files, 11818fd37a7SXin LI@command{diff} normally reports only that they are different. 11918fd37a7SXin LI 12018fd37a7SXin LIYou can use the @command{cmp} command to show the byte and line numbers 12118fd37a7SXin LIwhere two files differ. @command{cmp} can also show all the bytes 12218fd37a7SXin LIthat differ between the two files, side by side. A way to compare 12318fd37a7SXin LItwo files character by character is the Emacs command @kbd{M-x 12418fd37a7SXin LIcompare-windows}. @xref{Other Window, , Other Window, emacs, The @acronym{GNU} 12518fd37a7SXin LIEmacs Manual}, for more information on that command. 12618fd37a7SXin LI 12718fd37a7SXin LIYou can use the @command{diff3} command to show differences among three 12818fd37a7SXin LIfiles. When two people have made independent changes to a common 12918fd37a7SXin LIoriginal, @command{diff3} can report the differences between the original 13018fd37a7SXin LIand the two changed versions, and can produce a merged file that 13118fd37a7SXin LIcontains both persons' changes together with warnings about conflicts. 13218fd37a7SXin LI 13318fd37a7SXin LIYou can use the @command{sdiff} command to merge two files interactively. 13418fd37a7SXin LI 13518fd37a7SXin LIYou can use the set of differences produced by @command{diff} to distribute 13618fd37a7SXin LIupdates to text files (such as program source code) to other people. 13718fd37a7SXin LIThis method is especially useful when the differences are small compared 13818fd37a7SXin LIto the complete files. Given @command{diff} output, you can use the 13918fd37a7SXin LI@command{patch} program to update, or @dfn{patch}, a copy of the file. If you 14018fd37a7SXin LIthink of @command{diff} as subtracting one file from another to produce 14118fd37a7SXin LItheir difference, you can think of @command{patch} as adding the difference 14218fd37a7SXin LIto one file to reproduce the other. 14318fd37a7SXin LI 14418fd37a7SXin LIThis manual first concentrates on making diffs, and later shows how to 14518fd37a7SXin LIuse diffs to update files. 14618fd37a7SXin LI 14718fd37a7SXin LI@acronym{GNU} @command{diff} was written by Paul Eggert, Mike Haertel, 14818fd37a7SXin LIDavid Hayes, Richard Stallman, and Len Tower. Wayne Davison designed and 14918fd37a7SXin LIimplemented the unified output format. The basic algorithm is described 15018fd37a7SXin LIby Eugene W. Myers in ``An O(ND) Difference Algorithm and its Variations'', 15118fd37a7SXin LI@cite{Algorithmica} Vol.@: 1 No.@: 2, 1986, pp.@: 251--266; and in ``A File 15218fd37a7SXin LIComparison Program'', Webb Miller and Eugene W. Myers, 15318fd37a7SXin LI@cite{Software---Practice and Experience} Vol.@: 15 No.@: 11, 1985, 15418fd37a7SXin LIpp.@: 1025--1040. 15518fd37a7SXin LI@c From: "Gene Myers" <gene@cs.arizona.edu> 15618fd37a7SXin LI@c They are about the same basic algorithm; the Algorithmica 15718fd37a7SXin LI@c paper gives a rigorous treatment and the sub-algorithm for 15818fd37a7SXin LI@c delivering scripts and should be the primary reference, but 15918fd37a7SXin LI@c both should be mentioned. 16018fd37a7SXin LIThe algorithm was independently discovered as described by E. Ukkonen in 16118fd37a7SXin LI``Algorithms for Approximate String Matching'', 16218fd37a7SXin LI@cite{Information and Control} Vol.@: 64, 1985, pp.@: 100--118. 16318fd37a7SXin LI@c From: "Gene Myers" <gene@cs.arizona.edu> 16418fd37a7SXin LI@c Date: Wed, 29 Sep 1993 08:27:55 MST 16518fd37a7SXin LI@c Ukkonen should be given credit for also discovering the algorithm used 16618fd37a7SXin LI@c in GNU diff. 16718fd37a7SXin LIUnless the @option{--minimal} option is used, @command{diff} uses a 16818fd37a7SXin LIheuristic by Paul Eggert that limits the cost to @math{O(N^1.5 log N)} 16918fd37a7SXin LIat the price of producing suboptimal output for large inputs with many 17018fd37a7SXin LIdifferences. Related algorithms are surveyed by Alfred V. Aho in 17118fd37a7SXin LIsection 6.3 of ``Algorithms for Finding Patterns in Strings'', 17218fd37a7SXin LI@cite{Handbook of Theoretical Computer Science} (Jan Van Leeuwen, 17318fd37a7SXin LIed.), Vol.@: A, @cite{Algorithms and Complexity}, Elsevier/MIT Press, 17418fd37a7SXin LI1990, pp.@: 255--300. 17518fd37a7SXin LI 17618fd37a7SXin LI@acronym{GNU} @command{diff3} was written by Randy Smith. @acronym{GNU} 17718fd37a7SXin LI@command{sdiff} was written by Thomas Lord. @acronym{GNU} @command{cmp} 17818fd37a7SXin LIwas written by Torbj@"orn Granlund and David MacKenzie. 17918fd37a7SXin LI 18018fd37a7SXin LI@acronym{GNU} @command{patch} was written mainly by Larry Wall and Paul Eggert; 18118fd37a7SXin LIseveral @acronym{GNU} enhancements were contributed by Wayne Davison and 18218fd37a7SXin LIDavid MacKenzie. Parts of this manual are adapted from a manual page 18318fd37a7SXin LIwritten by Larry Wall, with his permission. 18418fd37a7SXin LI 18518fd37a7SXin LI@node Comparison 18618fd37a7SXin LI@chapter What Comparison Means 18718fd37a7SXin LI@cindex introduction 18818fd37a7SXin LI 18918fd37a7SXin LIThere are several ways to think about the differences between two files. 19018fd37a7SXin LIOne way to think of the differences is as a series of lines that were 19118fd37a7SXin LIdeleted from, inserted in, or changed in one file to produce the other 19218fd37a7SXin LIfile. @command{diff} compares two files line by line, finds groups of 19318fd37a7SXin LIlines that differ, and reports each group of differing lines. It can 19418fd37a7SXin LIreport the differing lines in several formats, which have different 19518fd37a7SXin LIpurposes. 19618fd37a7SXin LI 19718fd37a7SXin LI@acronym{GNU} @command{diff} can show whether files are different 19818fd37a7SXin LIwithout detailing the differences. It also provides ways to suppress 19918fd37a7SXin LIcertain kinds of differences that are not important to you. Most 20018fd37a7SXin LIcommonly, such differences are changes in the amount of white space 20118fd37a7SXin LIbetween words or lines. @command{diff} also provides ways to suppress 20218fd37a7SXin LIdifferences in alphabetic case or in lines that match a regular 20318fd37a7SXin LIexpression that you provide. These options can accumulate; for 20418fd37a7SXin LIexample, you can ignore changes in both white space and alphabetic 20518fd37a7SXin LIcase. 20618fd37a7SXin LI 20718fd37a7SXin LIAnother way to think of the differences between two files is as a 20818fd37a7SXin LIsequence of pairs of bytes that can be either identical or 20918fd37a7SXin LIdifferent. @command{cmp} reports the differences between two files 21018fd37a7SXin LIbyte by byte, instead of line by line. As a result, it is often 21118fd37a7SXin LImore useful than @command{diff} for comparing binary files. For text 21218fd37a7SXin LIfiles, @command{cmp} is useful mainly when you want to know only whether 21318fd37a7SXin LItwo files are identical, or whether one file is a prefix of the other. 21418fd37a7SXin LI 21518fd37a7SXin LITo illustrate the effect that considering changes byte by byte 21618fd37a7SXin LIcan have compared with considering them line by line, think of what 21718fd37a7SXin LIhappens if a single newline character is added to the beginning of a 21818fd37a7SXin LIfile. If that file is then compared with an otherwise identical file 21918fd37a7SXin LIthat lacks the newline at the beginning, @command{diff} will report that a 22018fd37a7SXin LIblank line has been added to the file, while @command{cmp} will report that 22118fd37a7SXin LIalmost every byte of the two files differs. 22218fd37a7SXin LI 22318fd37a7SXin LI@command{diff3} normally compares three input files line by line, finds 22418fd37a7SXin LIgroups of lines that differ, and reports each group of differing lines. 22518fd37a7SXin LIIts output is designed to make it easy to inspect two different sets of 22618fd37a7SXin LIchanges to the same file. 22718fd37a7SXin LI 22818fd37a7SXin LI@menu 22918fd37a7SXin LI* Hunks:: Groups of differing lines. 23018fd37a7SXin LI* White Space:: Suppressing differences in white space. 23118fd37a7SXin LI* Blank Lines:: Suppressing differences whose lines are all blank. 23218fd37a7SXin LI* Specified Lines:: Suppressing differences whose lines all match a pattern. 23318fd37a7SXin LI* Case Folding:: Suppressing differences in alphabetic case. 23418fd37a7SXin LI* Brief:: Summarizing which files are different. 23518fd37a7SXin LI* Binary:: Comparing binary files or forcing text comparisons. 23618fd37a7SXin LI@end menu 23718fd37a7SXin LI 23818fd37a7SXin LI@node Hunks 23918fd37a7SXin LI@section Hunks 24018fd37a7SXin LI@cindex hunks 24118fd37a7SXin LI 24218fd37a7SXin LIWhen comparing two files, @command{diff} finds sequences of lines common to 24318fd37a7SXin LIboth files, interspersed with groups of differing lines called 24418fd37a7SXin LI@dfn{hunks}. Comparing two identical files yields one sequence of 24518fd37a7SXin LIcommon lines and no hunks, because no lines differ. Comparing two 24618fd37a7SXin LIentirely different files yields no common lines and one large hunk that 24718fd37a7SXin LIcontains all lines of both files. In general, there are many ways to 24818fd37a7SXin LImatch up lines between two given files. @command{diff} tries to minimize 24918fd37a7SXin LIthe total hunk size by finding large sequences of common lines 25018fd37a7SXin LIinterspersed with small hunks of differing lines. 25118fd37a7SXin LI 25218fd37a7SXin LIFor example, suppose the file @file{F} contains the three lines 25318fd37a7SXin LI@samp{a}, @samp{b}, @samp{c}, and the file @file{G} contains the same 25418fd37a7SXin LIthree lines in reverse order @samp{c}, @samp{b}, @samp{a}. If 25518fd37a7SXin LI@command{diff} finds the line @samp{c} as common, then the command 25618fd37a7SXin LI@samp{diff F G} produces this output: 25718fd37a7SXin LI 25818fd37a7SXin LI@example 25918fd37a7SXin LI1,2d0 26018fd37a7SXin LI< a 26118fd37a7SXin LI< b 26218fd37a7SXin LI3a2,3 26318fd37a7SXin LI> b 26418fd37a7SXin LI> a 26518fd37a7SXin LI@end example 26618fd37a7SXin LI 26718fd37a7SXin LI@noindent 26818fd37a7SXin LIBut if @command{diff} notices the common line @samp{b} instead, it produces 26918fd37a7SXin LIthis output: 27018fd37a7SXin LI 27118fd37a7SXin LI@example 27218fd37a7SXin LI1c1 27318fd37a7SXin LI< a 27418fd37a7SXin LI--- 27518fd37a7SXin LI> c 27618fd37a7SXin LI3c3 27718fd37a7SXin LI< c 27818fd37a7SXin LI--- 27918fd37a7SXin LI> a 28018fd37a7SXin LI@end example 28118fd37a7SXin LI 28218fd37a7SXin LI@noindent 28318fd37a7SXin LIIt is also possible to find @samp{a} as the common line. @command{diff} 28418fd37a7SXin LIdoes not always find an optimal matching between the files; it takes 28518fd37a7SXin LIshortcuts to run faster. But its output is usually close to the 28618fd37a7SXin LIshortest possible. You can adjust this tradeoff with the 28718fd37a7SXin LI@option{-d} or @option{--minimal} option (@pxref{diff Performance}). 28818fd37a7SXin LI 28918fd37a7SXin LI@node White Space 29018fd37a7SXin LI@section Suppressing Differences in Blank and Tab Spacing 29118fd37a7SXin LI@cindex blank and tab difference suppression 29218fd37a7SXin LI@cindex tab and blank difference suppression 29318fd37a7SXin LI 29418fd37a7SXin LIThe @option{-E} or @option{--ignore-tab-expansion} option ignores the 29518fd37a7SXin LIdistinction between tabs and spaces on input. A tab is considered to be 29618fd37a7SXin LIequivalent to the number of spaces to the next tab stop (@pxref{Tabs}). 29718fd37a7SXin LI 29818fd37a7SXin LIThe @option{-b} or @option{--ignore-space-change} option is stronger. 29918fd37a7SXin LIIt ignores white space at line end, and considers all other sequences of 30018fd37a7SXin LIone or more white space characters within a line to be equivalent. With this 30118fd37a7SXin LIoption, @command{diff} considers the following two lines to be equivalent, 30218fd37a7SXin LIwhere @samp{$} denotes the line end: 30318fd37a7SXin LI 30418fd37a7SXin LI@example 30518fd37a7SXin LIHere lyeth muche rychnesse in lytell space. -- John Heywood$ 30618fd37a7SXin LIHere lyeth muche rychnesse in lytell space. -- John Heywood $ 30718fd37a7SXin LI@end example 30818fd37a7SXin LI 30918fd37a7SXin LIThe @option{-w} or @option{--ignore-all-space} option is stronger still. 31018fd37a7SXin LIIt ignores differences even if one line has white space where 31118fd37a7SXin LIthe other line has none. @dfn{White space} characters include 31218fd37a7SXin LItab, newline, vertical tab, form feed, carriage return, and space; 31318fd37a7SXin LIsome locales may define additional characters to be white space. 31418fd37a7SXin LIWith this option, @command{diff} considers the 31518fd37a7SXin LIfollowing two lines to be equivalent, where @samp{$} denotes the line 31618fd37a7SXin LIend and @samp{^M} denotes a carriage return: 31718fd37a7SXin LI 31818fd37a7SXin LI@example 31918fd37a7SXin LIHere lyeth muche rychnesse in lytell space.-- John Heywood$ 32018fd37a7SXin LI He relyeth much erychnes seinly tells pace. --John Heywood ^M$ 32118fd37a7SXin LI@end example 32218fd37a7SXin LI 32318fd37a7SXin LI@node Blank Lines 32418fd37a7SXin LI@section Suppressing Differences Whose Lines Are All Blank 32518fd37a7SXin LI@cindex blank line difference suppression 32618fd37a7SXin LI 32718fd37a7SXin LIThe @option{-B} or @option{--ignore-blank-lines} option ignores changes 32818fd37a7SXin LIthat consist entirely of blank lines. With this option, for example, a 32918fd37a7SXin LIfile containing 33018fd37a7SXin LI@example 33118fd37a7SXin LI1. A point is that which has no part. 33218fd37a7SXin LI 33318fd37a7SXin LI2. A line is breadthless length. 33418fd37a7SXin LI-- Euclid, The Elements, I 33518fd37a7SXin LI@end example 33618fd37a7SXin LI@noindent 33718fd37a7SXin LIis considered identical to a file containing 33818fd37a7SXin LI@example 33918fd37a7SXin LI1. A point is that which has no part. 34018fd37a7SXin LI2. A line is breadthless length. 34118fd37a7SXin LI 34218fd37a7SXin LI 34318fd37a7SXin LI-- Euclid, The Elements, I 34418fd37a7SXin LI@end example 34518fd37a7SXin LI 34618fd37a7SXin LINormally this option affects only lines that are completely empty, but 34718fd37a7SXin LIif you also specify the @option{-b} or @option{--ignore-space-change} 34818fd37a7SXin LIoption, or the @option{-w} or @option{--ignore-all-space} option, 34918fd37a7SXin LIlines are also affected if they look empty but contain white space. 35018fd37a7SXin LIIn other words, @option{-B} is equivalent to @samp{-I '^$'} by 35118fd37a7SXin LIdefault, but it is equivalent to @option{-I '^[[:space:]]*$'} if 35218fd37a7SXin LI@option{-b} or @option{-w} is also specified. 35318fd37a7SXin LI 35418fd37a7SXin LI@node Specified Lines 35518fd37a7SXin LI@section Suppressing Differences Whose Lines All Match a Regular Expression 35618fd37a7SXin LI@cindex regular expression suppression 35718fd37a7SXin LI 35818fd37a7SXin LITo ignore insertions and deletions of lines that match a 35918fd37a7SXin LI@command{grep}-style regular expression, use the @option{-I 36018fd37a7SXin LI@var{regexp}} or @option{--ignore-matching-lines=@var{regexp}} option. 36118fd37a7SXin LIYou should escape 36218fd37a7SXin LIregular expressions that contain shell metacharacters to prevent the 36318fd37a7SXin LIshell from expanding them. For example, @samp{diff -I '^[[:digit:]]'} ignores 36418fd37a7SXin LIall changes to lines beginning with a digit. 36518fd37a7SXin LI 36618fd37a7SXin LIHowever, @option{-I} only ignores the insertion or deletion of lines that 36718fd37a7SXin LIcontain the regular expression if every changed line in the hunk---every 36818fd37a7SXin LIinsertion and every deletion---matches the regular expression. In other 36918fd37a7SXin LIwords, for each nonignorable change, @command{diff} prints the complete set 37018fd37a7SXin LIof changes in its vicinity, including the ignorable ones. 37118fd37a7SXin LI 37218fd37a7SXin LIYou can specify more than one regular expression for lines to ignore by 37318fd37a7SXin LIusing more than one @option{-I} option. @command{diff} tries to match each 37418fd37a7SXin LIline against each regular expression. 37518fd37a7SXin LI 37618fd37a7SXin LI@node Case Folding 37718fd37a7SXin LI@section Suppressing Case Differences 37818fd37a7SXin LI@cindex case difference suppression 37918fd37a7SXin LI 38018fd37a7SXin LI@acronym{GNU} @command{diff} can treat lower case letters as 38118fd37a7SXin LIequivalent to their upper case counterparts, so that, for example, it 38218fd37a7SXin LIconsiders @samp{Funky Stuff}, @samp{funky STUFF}, and @samp{fUNKy 38318fd37a7SXin LIstuFf} to all be the same. To request this, use the @option{-i} or 38418fd37a7SXin LI@option{--ignore-case} option. 38518fd37a7SXin LI 38618fd37a7SXin LI@node Brief 38718fd37a7SXin LI@section Summarizing Which Files Differ 38818fd37a7SXin LI@cindex summarizing which files differ 38918fd37a7SXin LI@cindex brief difference reports 39018fd37a7SXin LI 39118fd37a7SXin LIWhen you only want to find out whether files are different, and you 39218fd37a7SXin LIdon't care what the differences are, you can use the summary output 39318fd37a7SXin LIformat. In this format, instead of showing the differences between the 39418fd37a7SXin LIfiles, @command{diff} simply reports whether files differ. The @option{-q} 39518fd37a7SXin LIor @option{--brief} option selects this output format. 39618fd37a7SXin LI 39718fd37a7SXin LIThis format is especially useful when comparing the contents of two 39818fd37a7SXin LIdirectories. It is also much faster than doing the normal line by line 39918fd37a7SXin LIcomparisons, because @command{diff} can stop analyzing the files as soon as 40018fd37a7SXin LIit knows that there are any differences. 40118fd37a7SXin LI 40218fd37a7SXin LIYou can also get a brief indication of whether two files differ by using 40318fd37a7SXin LI@command{cmp}. For files that are identical, @command{cmp} produces no 40418fd37a7SXin LIoutput. When the files differ, by default, @command{cmp} outputs the byte 40518fd37a7SXin LIand line number where the first difference occurs, or reports that one 40618fd37a7SXin LIfile is a prefix of the other. You can use 40718fd37a7SXin LIthe @option{-s}, @option{--quiet}, or @option{--silent} option to 40818fd37a7SXin LIsuppress that information, so that @command{cmp} 40918fd37a7SXin LIproduces no output and reports whether the files differ using only its 41018fd37a7SXin LIexit status (@pxref{Invoking cmp}). 41118fd37a7SXin LI 41218fd37a7SXin LI@c Fix this. 41318fd37a7SXin LIUnlike @command{diff}, @command{cmp} cannot compare directories; it can only 41418fd37a7SXin LIcompare two files. 41518fd37a7SXin LI 41618fd37a7SXin LI@node Binary 41718fd37a7SXin LI@section Binary Files and Forcing Text Comparisons 41818fd37a7SXin LI@cindex binary file diff 41918fd37a7SXin LI@cindex text versus binary diff 42018fd37a7SXin LI 42118fd37a7SXin LIIf @command{diff} thinks that either of the two files it is comparing is 42218fd37a7SXin LIbinary (a non-text file), it normally treats that pair of files much as 42318fd37a7SXin LIif the summary output format had been selected (@pxref{Brief}), and 42418fd37a7SXin LIreports only that the binary files are different. This is because line 42518fd37a7SXin LIby line comparisons are usually not meaningful for binary files. 42618fd37a7SXin LI 42718fd37a7SXin LI@command{diff} determines whether a file is text or binary by checking the 42818fd37a7SXin LIfirst few bytes in the file; the exact number of bytes is system 42918fd37a7SXin LIdependent, but it is typically several thousand. If every byte in 43018fd37a7SXin LIthat part of the file is non-null, @command{diff} considers the file to be 43118fd37a7SXin LItext; otherwise it considers the file to be binary. 43218fd37a7SXin LI 43318fd37a7SXin LISometimes you might want to force @command{diff} to consider files to be 43418fd37a7SXin LItext. For example, you might be comparing text files that contain 43518fd37a7SXin LInull characters; @command{diff} would erroneously decide that those are 43618fd37a7SXin LInon-text files. Or you might be comparing documents that are in a 43718fd37a7SXin LIformat used by a word processing system that uses null characters to 43818fd37a7SXin LIindicate special formatting. You can force @command{diff} to consider all 43918fd37a7SXin LIfiles to be text files, and compare them line by line, by using the 44018fd37a7SXin LI@option{-a} or @option{--text} option. If the files you compare using this 44118fd37a7SXin LIoption do not in fact contain text, they will probably contain few 44218fd37a7SXin LInewline characters, and the @command{diff} output will consist of hunks 44318fd37a7SXin LIshowing differences between long lines of whatever characters the files 44418fd37a7SXin LIcontain. 44518fd37a7SXin LI 44618fd37a7SXin LIYou can also force @command{diff} to report only whether files differ 44718fd37a7SXin LI(but not how). Use the @option{-q} or @option{--brief} option for 44818fd37a7SXin LIthis. 44918fd37a7SXin LI 45018fd37a7SXin LINormally, differing binary files count as trouble because the 45118fd37a7SXin LIresulting @command{diff} output does not capture all the differences. 45218fd37a7SXin LIThis trouble causes @command{diff} to exit with status 2. However, 45318fd37a7SXin LIthis trouble cannot occur with the @option{-a} or @option{--text} 45418fd37a7SXin LIoption, or with the @option{-q} or @option{--brief} option, as these 45518fd37a7SXin LIoptions both cause @command{diff} to generate a form of output that 45618fd37a7SXin LIrepresents differences as requested. 45718fd37a7SXin LI 45818fd37a7SXin LIIn operating systems that distinguish between text and binary files, 45918fd37a7SXin LI@command{diff} normally reads and writes all data as text. Use the 46018fd37a7SXin LI@option{--binary} option to force @command{diff} to read and write binary 46118fd37a7SXin LIdata instead. This option has no effect on a @acronym{POSIX}-compliant system 46218fd37a7SXin LIlike @acronym{GNU} or traditional Unix. However, many personal computer 46318fd37a7SXin LIoperating systems represent the end of a line with a carriage return 46418fd37a7SXin LIfollowed by a newline. On such systems, @command{diff} normally ignores 46518fd37a7SXin LIthese carriage returns on input and generates them at the end of each 46618fd37a7SXin LIoutput line, but with the @option{--binary} option @command{diff} treats 46718fd37a7SXin LIeach carriage return as just another input character, and does not 46818fd37a7SXin LIgenerate a carriage return at the end of each output line. This can be 46918fd37a7SXin LIuseful when dealing with non-text files that are meant to be 47018fd37a7SXin LIinterchanged with @acronym{POSIX}-compliant systems. 47118fd37a7SXin LI 47218fd37a7SXin LIThe @option{--strip-trailing-cr} causes @command{diff} to treat input 47318fd37a7SXin LIlines that end in carriage return followed by newline as if they end 47418fd37a7SXin LIin plain newline. This can be useful when comparing text that is 47518fd37a7SXin LIimperfectly imported from many personal computer operating systems. 47618fd37a7SXin LIThis option affects how lines are read, which in turn affects how they 47718fd37a7SXin LIare compared and output. 47818fd37a7SXin LI 47918fd37a7SXin LIIf you want to compare two files byte by byte, you can use the 48018fd37a7SXin LI@command{cmp} program with the @option{-l} or @option{--verbose} 48118fd37a7SXin LIoption to show the values of each differing byte in the two files. 48218fd37a7SXin LIWith @acronym{GNU} @command{cmp}, you can also use the @option{-b} or 48318fd37a7SXin LI@option{--print-bytes} option to show the @acronym{ASCII} representation of 48418fd37a7SXin LIthose bytes. @xref{Invoking cmp}, for more information. 48518fd37a7SXin LI 48618fd37a7SXin LIIf @command{diff3} thinks that any of the files it is comparing is binary 48718fd37a7SXin LI(a non-text file), it normally reports an error, because such 48818fd37a7SXin LIcomparisons are usually not useful. @command{diff3} uses the same test as 48918fd37a7SXin LI@command{diff} to decide whether a file is binary. As with @command{diff}, if 49018fd37a7SXin LIthe input files contain a few non-text bytes but otherwise are like 49118fd37a7SXin LItext files, you can force @command{diff3} to consider all files to be text 49218fd37a7SXin LIfiles and compare them line by line by using the @option{-a} or 49318fd37a7SXin LI@option{--text} option. 49418fd37a7SXin LI 49518fd37a7SXin LI@node Output Formats 49618fd37a7SXin LI@chapter @command{diff} Output Formats 49718fd37a7SXin LI@cindex output formats 49818fd37a7SXin LI@cindex format of @command{diff} output 49918fd37a7SXin LI 50018fd37a7SXin LI@command{diff} has several mutually exclusive options for output format. 50118fd37a7SXin LIThe following sections describe each format, illustrating how 50218fd37a7SXin LI@command{diff} reports the differences between two sample input files. 50318fd37a7SXin LI 50418fd37a7SXin LI@menu 50518fd37a7SXin LI* Sample diff Input:: Sample @command{diff} input files for examples. 50618fd37a7SXin LI* Context:: Showing differences with the surrounding text. 50718fd37a7SXin LI* Side by Side:: Showing differences in two columns. 50818fd37a7SXin LI* Normal:: Showing differences without surrounding text. 50918fd37a7SXin LI* Scripts:: Generating scripts for other programs. 51018fd37a7SXin LI* If-then-else:: Merging files with if-then-else. 51118fd37a7SXin LI@end menu 51218fd37a7SXin LI 51318fd37a7SXin LI@node Sample diff Input 51418fd37a7SXin LI@section Two Sample Input Files 51518fd37a7SXin LI@cindex @command{diff} sample input 51618fd37a7SXin LI@cindex sample input for @command{diff} 51718fd37a7SXin LI 51818fd37a7SXin LIHere are two sample files that we will use in numerous examples to 51918fd37a7SXin LIillustrate the output of @command{diff} and how various options can change 52018fd37a7SXin LIit. 52118fd37a7SXin LI 52218fd37a7SXin LIThis is the file @file{lao}: 52318fd37a7SXin LI 52418fd37a7SXin LI@example 52518fd37a7SXin LIThe Way that can be told of is not the eternal Way; 52618fd37a7SXin LIThe name that can be named is not the eternal name. 52718fd37a7SXin LIThe Nameless is the origin of Heaven and Earth; 52818fd37a7SXin LIThe Named is the mother of all things. 52918fd37a7SXin LITherefore let there always be non-being, 53018fd37a7SXin LI so we may see their subtlety, 53118fd37a7SXin LIAnd let there always be being, 53218fd37a7SXin LI so we may see their outcome. 53318fd37a7SXin LIThe two are the same, 53418fd37a7SXin LIBut after they are produced, 53518fd37a7SXin LI they have different names. 53618fd37a7SXin LI@end example 53718fd37a7SXin LI 53818fd37a7SXin LIThis is the file @file{tzu}: 53918fd37a7SXin LI 54018fd37a7SXin LI@example 54118fd37a7SXin LIThe Nameless is the origin of Heaven and Earth; 54218fd37a7SXin LIThe named is the mother of all things. 54318fd37a7SXin LI 54418fd37a7SXin LITherefore let there always be non-being, 54518fd37a7SXin LI so we may see their subtlety, 54618fd37a7SXin LIAnd let there always be being, 54718fd37a7SXin LI so we may see their outcome. 54818fd37a7SXin LIThe two are the same, 54918fd37a7SXin LIBut after they are produced, 55018fd37a7SXin LI they have different names. 55118fd37a7SXin LIThey both may be called deep and profound. 55218fd37a7SXin LIDeeper and more profound, 55318fd37a7SXin LIThe door of all subtleties! 55418fd37a7SXin LI@end example 55518fd37a7SXin LI 55618fd37a7SXin LIIn this example, the first hunk contains just the first two lines of 55718fd37a7SXin LI@file{lao}, the second hunk contains the fourth line of @file{lao} 55818fd37a7SXin LIopposing the second and third lines of @file{tzu}, and the last hunk 55918fd37a7SXin LIcontains just the last three lines of @file{tzu}. 56018fd37a7SXin LI 56118fd37a7SXin LI@node Context 56218fd37a7SXin LI@section Showing Differences in Their Context 56318fd37a7SXin LI@cindex context output format 56418fd37a7SXin LI@cindex @samp{!} output format 56518fd37a7SXin LI 56618fd37a7SXin LIUsually, when you are looking at the differences between files, you will 56718fd37a7SXin LIalso want to see the parts of the files near the lines that differ, to 56818fd37a7SXin LIhelp you understand exactly what has changed. These nearby parts of the 56918fd37a7SXin LIfiles are called the @dfn{context}. 57018fd37a7SXin LI 57118fd37a7SXin LI@acronym{GNU} @command{diff} provides two output formats that show context 57218fd37a7SXin LIaround the differing lines: @dfn{context format} and @dfn{unified 57318fd37a7SXin LIformat}. It can optionally show in which function or section of the 57418fd37a7SXin LIfile the differing lines are found. 57518fd37a7SXin LI 57618fd37a7SXin LIIf you are distributing new versions of files to other people in the 57718fd37a7SXin LIform of @command{diff} output, you should use one of the output formats 57818fd37a7SXin LIthat show context so that they can apply the diffs even if they have 57918fd37a7SXin LImade small changes of their own to the files. @command{patch} can apply 58018fd37a7SXin LIthe diffs in this case by searching in the files for the lines of 58118fd37a7SXin LIcontext around the differing lines; if those lines are actually a few 58218fd37a7SXin LIlines away from where the diff says they are, @command{patch} can adjust 58318fd37a7SXin LIthe line numbers accordingly and still apply the diff correctly. 58418fd37a7SXin LI@xref{Imperfect}, for more information on using @command{patch} to apply 58518fd37a7SXin LIimperfect diffs. 58618fd37a7SXin LI 58718fd37a7SXin LI@menu 58818fd37a7SXin LI* Context Format:: An output format that shows surrounding lines. 58918fd37a7SXin LI* Unified Format:: A more compact output format that shows context. 59018fd37a7SXin LI* Sections:: Showing which sections of the files differences are in. 59118fd37a7SXin LI* Alternate Names:: Showing alternate file names in context headers. 59218fd37a7SXin LI@end menu 59318fd37a7SXin LI 59418fd37a7SXin LI@node Context Format 59518fd37a7SXin LI@subsection Context Format 59618fd37a7SXin LI 59718fd37a7SXin LIThe context output format shows several lines of context around the 59818fd37a7SXin LIlines that differ. It is the standard format for distributing updates 59918fd37a7SXin LIto source code. 60018fd37a7SXin LI 60118fd37a7SXin LITo select this output format, use the @option{-C @var{lines}}, 60218fd37a7SXin LI@option{--context@r{[}=@var{lines}@r{]}}, or @option{-c} option. The 60318fd37a7SXin LIargument @var{lines} that some of these options take is the number of 60418fd37a7SXin LIlines of context to show. If you do not specify @var{lines}, it 60518fd37a7SXin LIdefaults to three. For proper operation, @command{patch} typically needs 60618fd37a7SXin LIat least two lines of context. 60718fd37a7SXin LI 60818fd37a7SXin LI@menu 60918fd37a7SXin LI* Example Context:: Sample output in context format. 61018fd37a7SXin LI* Less Context:: Another sample with less context. 61118fd37a7SXin LI* Detailed Context:: A detailed description of the context output format. 61218fd37a7SXin LI@end menu 61318fd37a7SXin LI 61418fd37a7SXin LI@node Example Context 61518fd37a7SXin LI@subsubsection An Example of Context Format 61618fd37a7SXin LI 61718fd37a7SXin LIHere is the output of @samp{diff -c lao tzu} (@pxref{Sample diff Input}, 61818fd37a7SXin LIfor the complete contents of the two files). Notice that up to three 61918fd37a7SXin LIlines that are not different are shown around each line that is 62018fd37a7SXin LIdifferent; they are the context lines. Also notice that the first two 62118fd37a7SXin LIhunks have run together, because their contents overlap. 62218fd37a7SXin LI 62318fd37a7SXin LI@example 62418fd37a7SXin LI*** lao 2002-02-21 23:30:39.942229878 -0800 62518fd37a7SXin LI--- tzu 2002-02-21 23:30:50.442260588 -0800 62618fd37a7SXin LI*************** 62718fd37a7SXin LI*** 1,7 **** 62818fd37a7SXin LI- The Way that can be told of is not the eternal Way; 62918fd37a7SXin LI- The name that can be named is not the eternal name. 63018fd37a7SXin LI The Nameless is the origin of Heaven and Earth; 63118fd37a7SXin LI! The Named is the mother of all things. 63218fd37a7SXin LI Therefore let there always be non-being, 63318fd37a7SXin LI so we may see their subtlety, 63418fd37a7SXin LI And let there always be being, 63518fd37a7SXin LI--- 1,6 ---- 63618fd37a7SXin LI The Nameless is the origin of Heaven and Earth; 63718fd37a7SXin LI! The named is the mother of all things. 63818fd37a7SXin LI! 63918fd37a7SXin LI Therefore let there always be non-being, 64018fd37a7SXin LI so we may see their subtlety, 64118fd37a7SXin LI And let there always be being, 64218fd37a7SXin LI*************** 64318fd37a7SXin LI*** 9,11 **** 64418fd37a7SXin LI--- 8,13 ---- 64518fd37a7SXin LI The two are the same, 64618fd37a7SXin LI But after they are produced, 64718fd37a7SXin LI they have different names. 64818fd37a7SXin LI+ They both may be called deep and profound. 64918fd37a7SXin LI+ Deeper and more profound, 65018fd37a7SXin LI+ The door of all subtleties! 65118fd37a7SXin LI@end example 65218fd37a7SXin LI 65318fd37a7SXin LI@node Less Context 65418fd37a7SXin LI@subsubsection An Example of Context Format with Less Context 65518fd37a7SXin LI 65618fd37a7SXin LIHere is the output of @samp{diff -C 1 lao tzu} (@pxref{Sample diff 65718fd37a7SXin LIInput}, for the complete contents of the two files). Notice that at 65818fd37a7SXin LImost one context line is reported here. 65918fd37a7SXin LI 66018fd37a7SXin LI@example 66118fd37a7SXin LI*** lao 2002-02-21 23:30:39.942229878 -0800 66218fd37a7SXin LI--- tzu 2002-02-21 23:30:50.442260588 -0800 66318fd37a7SXin LI*************** 66418fd37a7SXin LI*** 1,5 **** 66518fd37a7SXin LI- The Way that can be told of is not the eternal Way; 66618fd37a7SXin LI- The name that can be named is not the eternal name. 66718fd37a7SXin LI The Nameless is the origin of Heaven and Earth; 66818fd37a7SXin LI! The Named is the mother of all things. 66918fd37a7SXin LI Therefore let there always be non-being, 67018fd37a7SXin LI--- 1,4 ---- 67118fd37a7SXin LI The Nameless is the origin of Heaven and Earth; 67218fd37a7SXin LI! The named is the mother of all things. 67318fd37a7SXin LI! 67418fd37a7SXin LI Therefore let there always be non-being, 67518fd37a7SXin LI*************** 67618fd37a7SXin LI*** 11 **** 67718fd37a7SXin LI--- 10,13 ---- 67818fd37a7SXin LI they have different names. 67918fd37a7SXin LI+ They both may be called deep and profound. 68018fd37a7SXin LI+ Deeper and more profound, 68118fd37a7SXin LI+ The door of all subtleties! 68218fd37a7SXin LI@end example 68318fd37a7SXin LI 68418fd37a7SXin LI@node Detailed Context 68518fd37a7SXin LI@subsubsection Detailed Description of Context Format 68618fd37a7SXin LI 68718fd37a7SXin LIThe context output format starts with a two-line header, which looks 68818fd37a7SXin LIlike this: 68918fd37a7SXin LI 69018fd37a7SXin LI@example 69118fd37a7SXin LI*** @var{from-file} @var{from-file-modification-time} 69218fd37a7SXin LI--- @var{to-file} @var{to-file-modification time} 69318fd37a7SXin LI@end example 69418fd37a7SXin LI 69518fd37a7SXin LI@noindent 69618fd37a7SXin LI@vindex LC_TIME 69718fd37a7SXin LI@cindex time stamp format, context diffs 69818fd37a7SXin LIThe time stamp normally looks like @samp{2002-02-21 23:30:39.942229878 69918fd37a7SXin LI-0800} to indicate the date, time with fractional seconds, and time 70018fd37a7SXin LIzone in @uref{ftp://ftp.isi.edu/in-notes/rfc2822.txt, Internet RFC 70118fd37a7SXin LI2822 format}. (The fractional seconds are omitted on hosts that do 70218fd37a7SXin LInot support fractional time stamps.) However, a traditional time 70318fd37a7SXin LIstamp like @samp{Thu Feb 21 23:30:39 2002} is used if the 70418fd37a7SXin LI@env{LC_TIME} locale category is either @samp{C} or @samp{POSIX}. 70518fd37a7SXin LI 70618fd37a7SXin LIYou can change the header's content with the 70718fd37a7SXin LI@option{--label=@var{label}} option; see @ref{Alternate Names}. 70818fd37a7SXin LI 70918fd37a7SXin LINext come one or more hunks of differences; each hunk shows one area 71018fd37a7SXin LIwhere the files differ. Context format hunks look like this: 71118fd37a7SXin LI 71218fd37a7SXin LI@example 71318fd37a7SXin LI*************** 71418fd37a7SXin LI*** @var{from-file-line-numbers} **** 71518fd37a7SXin LI @var{from-file-line} 71618fd37a7SXin LI @var{from-file-line}@dots{} 71718fd37a7SXin LI--- @var{to-file-line-numbers} ---- 71818fd37a7SXin LI @var{to-file-line} 71918fd37a7SXin LI @var{to-file-line}@dots{} 72018fd37a7SXin LI@end example 72118fd37a7SXin LI 72218fd37a7SXin LIIf a hunk contains two or more lines, its line numbers look like 72318fd37a7SXin LI@samp{@var{start},@var{end}}. Otherwise only its end line number 72418fd37a7SXin LIappears. An empty hunk is considered to end at the line that precedes 72518fd37a7SXin LIthe hunk. 72618fd37a7SXin LI 72718fd37a7SXin LIThe lines of context around the lines that differ start with two space 72818fd37a7SXin LIcharacters. The lines that differ between the two files start with one 72918fd37a7SXin LIof the following indicator characters, followed by a space character: 73018fd37a7SXin LI 73118fd37a7SXin LI@table @samp 73218fd37a7SXin LI@item ! 73318fd37a7SXin LIA line that is part of a group of one or more lines that changed between 73418fd37a7SXin LIthe two files. There is a corresponding group of lines marked with 73518fd37a7SXin LI@samp{!} in the part of this hunk for the other file. 73618fd37a7SXin LI 73718fd37a7SXin LI@item + 73818fd37a7SXin LIAn ``inserted'' line in the second file that corresponds to nothing in 73918fd37a7SXin LIthe first file. 74018fd37a7SXin LI 74118fd37a7SXin LI@item - 74218fd37a7SXin LIA ``deleted'' line in the first file that corresponds to nothing in the 74318fd37a7SXin LIsecond file. 74418fd37a7SXin LI@end table 74518fd37a7SXin LI 74618fd37a7SXin LIIf all of the changes in a hunk are insertions, the lines of 74718fd37a7SXin LI@var{from-file} are omitted. If all of the changes are deletions, the 74818fd37a7SXin LIlines of @var{to-file} are omitted. 74918fd37a7SXin LI 75018fd37a7SXin LI@node Unified Format 75118fd37a7SXin LI@subsection Unified Format 75218fd37a7SXin LI@cindex unified output format 75318fd37a7SXin LI@cindex @samp{+-} output format 75418fd37a7SXin LI 75518fd37a7SXin LIThe unified output format is a variation on the context format that is 75618fd37a7SXin LImore compact because it omits redundant context lines. To select this 75718fd37a7SXin LIoutput format, use the @option{-U @var{lines}}, 75818fd37a7SXin LI@option{--unified@r{[}=@var{lines}@r{]}}, or @option{-u} 75918fd37a7SXin LIoption. The argument @var{lines} is the number of lines of context to 76018fd37a7SXin LIshow. When it is not given, it defaults to three. 76118fd37a7SXin LI 76218fd37a7SXin LIAt present, only @acronym{GNU} @command{diff} can produce this format and 76318fd37a7SXin LIonly @acronym{GNU} @command{patch} can automatically apply diffs in this 76418fd37a7SXin LIformat. For proper operation, @command{patch} typically needs at 76518fd37a7SXin LIleast three lines of context. 76618fd37a7SXin LI 76718fd37a7SXin LI@menu 76818fd37a7SXin LI* Example Unified:: Sample output in unified format. 76918fd37a7SXin LI* Detailed Unified:: A detailed description of unified format. 77018fd37a7SXin LI@end menu 77118fd37a7SXin LI 77218fd37a7SXin LI@node Example Unified 77318fd37a7SXin LI@subsubsection An Example of Unified Format 77418fd37a7SXin LI 77518fd37a7SXin LIHere is the output of the command @samp{diff -u lao tzu} 77618fd37a7SXin LI(@pxref{Sample diff Input}, for the complete contents of the two files): 77718fd37a7SXin LI 77818fd37a7SXin LI@example 77918fd37a7SXin LI--- lao 2002-02-21 23:30:39.942229878 -0800 78018fd37a7SXin LI+++ tzu 2002-02-21 23:30:50.442260588 -0800 78118fd37a7SXin LI@@@@ -1,7 +1,6 @@@@ 78218fd37a7SXin LI-The Way that can be told of is not the eternal Way; 78318fd37a7SXin LI-The name that can be named is not the eternal name. 78418fd37a7SXin LI The Nameless is the origin of Heaven and Earth; 78518fd37a7SXin LI-The Named is the mother of all things. 78618fd37a7SXin LI+The named is the mother of all things. 78718fd37a7SXin LI+ 78818fd37a7SXin LI Therefore let there always be non-being, 78918fd37a7SXin LI so we may see their subtlety, 79018fd37a7SXin LI And let there always be being, 79118fd37a7SXin LI@@@@ -9,3 +8,6 @@@@ 79218fd37a7SXin LI The two are the same, 79318fd37a7SXin LI But after they are produced, 79418fd37a7SXin LI they have different names. 79518fd37a7SXin LI+They both may be called deep and profound. 79618fd37a7SXin LI+Deeper and more profound, 79718fd37a7SXin LI+The door of all subtleties! 79818fd37a7SXin LI@end example 79918fd37a7SXin LI 80018fd37a7SXin LI@node Detailed Unified 80118fd37a7SXin LI@subsubsection Detailed Description of Unified Format 80218fd37a7SXin LI 80318fd37a7SXin LIThe unified output format starts with a two-line header, which looks 80418fd37a7SXin LIlike this: 80518fd37a7SXin LI 80618fd37a7SXin LI@example 80718fd37a7SXin LI--- @var{from-file} @var{from-file-modification-time} 80818fd37a7SXin LI+++ @var{to-file} @var{to-file-modification-time} 80918fd37a7SXin LI@end example 81018fd37a7SXin LI 81118fd37a7SXin LI@noindent 81218fd37a7SXin LI@cindex time stamp format, unified diffs 81318fd37a7SXin LIThe time stamp looks like @samp{2002-02-21 23:30:39.942229878 -0800} 81418fd37a7SXin LIto indicate the date, time with fractional seconds, and time zone. 81518fd37a7SXin LIThe fractional seconds are omitted on hosts that do not support 81618fd37a7SXin LIfractional time stamps. 81718fd37a7SXin LI 81818fd37a7SXin LIYou can change the header's content with the 81918fd37a7SXin LI@option{--label=@var{label}} option; see @xref{Alternate Names}. 82018fd37a7SXin LI 82118fd37a7SXin LINext come one or more hunks of differences; each hunk shows one area 82218fd37a7SXin LIwhere the files differ. Unified format hunks look like this: 82318fd37a7SXin LI 82418fd37a7SXin LI@example 82518fd37a7SXin LI@@@@ @var{from-file-line-numbers} @var{to-file-line-numbers} @@@@ 82618fd37a7SXin LI @var{line-from-either-file} 82718fd37a7SXin LI @var{line-from-either-file}@dots{} 82818fd37a7SXin LI@end example 82918fd37a7SXin LI 83018fd37a7SXin LIIf a hunk contains just one line, only its start line number appears. 83118fd37a7SXin LIOtherwise its line numbers look like @samp{@var{start},@var{count}}. 83218fd37a7SXin LIAn empty hunk is considered to start at the line that follows the hunk. 83318fd37a7SXin LI 83418fd37a7SXin LIIf a hunk and its context contain two or more lines, its 83518fd37a7SXin LIline numbers look like @samp{@var{start},@var{count}}. Otherwise only 83618fd37a7SXin LIits end line number appears. An empty hunk is considered to end at 83718fd37a7SXin LIthe line that precedes the hunk. 83818fd37a7SXin LI 83918fd37a7SXin LIThe lines common to both files begin with a space character. The lines 84018fd37a7SXin LIthat actually differ between the two files have one of the following 84118fd37a7SXin LIindicator characters in the left print column: 84218fd37a7SXin LI 84318fd37a7SXin LI@table @samp 84418fd37a7SXin LI@item + 84518fd37a7SXin LIA line was added here to the first file. 84618fd37a7SXin LI 84718fd37a7SXin LI@item - 84818fd37a7SXin LIA line was removed here from the first file. 84918fd37a7SXin LI@end table 85018fd37a7SXin LI 85118fd37a7SXin LI@node Sections 85218fd37a7SXin LI@subsection Showing Which Sections Differences Are in 85318fd37a7SXin LI@cindex headings 85418fd37a7SXin LI@cindex section headings 85518fd37a7SXin LI 85618fd37a7SXin LISometimes you might want to know which part of the files each change 85718fd37a7SXin LIfalls in. If the files are source code, this could mean which 85818fd37a7SXin LIfunction was changed. If the files are documents, it could mean which 85918fd37a7SXin LIchapter or appendix was changed. @acronym{GNU} @command{diff} can 86018fd37a7SXin LIshow this by displaying the nearest section heading line that precedes 86118fd37a7SXin LIthe differing lines. Which lines are ``section headings'' is 86218fd37a7SXin LIdetermined by a regular expression. 86318fd37a7SXin LI 86418fd37a7SXin LI@menu 86518fd37a7SXin LI* Specified Headings:: Showing headings that match regular expressions. 86618fd37a7SXin LI* C Function Headings:: Showing headings of C functions. 86718fd37a7SXin LI@end menu 86818fd37a7SXin LI 86918fd37a7SXin LI@node Specified Headings 87018fd37a7SXin LI@subsubsection Showing Lines That Match Regular Expressions 87118fd37a7SXin LI@cindex specified headings 87218fd37a7SXin LI@cindex regular expression matching headings 87318fd37a7SXin LI 87418fd37a7SXin LITo show in which sections differences occur for files that are not 87518fd37a7SXin LIsource code for C or similar languages, use the @option{-F @var{regexp}} 87618fd37a7SXin LIor @option{--show-function-line=@var{regexp}} option. @command{diff} 87718fd37a7SXin LIconsiders lines that match the @command{grep}-style regular expression 87818fd37a7SXin LI@var{regexp} to be the beginning 87918fd37a7SXin LIof a section of the file. Here are suggested regular expressions for 88018fd37a7SXin LIsome common languages: 88118fd37a7SXin LI 88218fd37a7SXin LI@c Please add to this list, e.g. Fortran, Pascal, Perl, Python. 88318fd37a7SXin LI@table @samp 88418fd37a7SXin LI@item ^[[:alpha:]$_] 88518fd37a7SXin LIC, C++, Prolog 88618fd37a7SXin LI@item ^( 88718fd37a7SXin LILisp 88818fd37a7SXin LI@item ^@@node 88918fd37a7SXin LITexinfo 89018fd37a7SXin LI@end table 89118fd37a7SXin LI 89218fd37a7SXin LIThis option does not automatically select an output format; in order to 89318fd37a7SXin LIuse it, you must select the context format (@pxref{Context Format}) or 89418fd37a7SXin LIunified format (@pxref{Unified Format}). In other output formats it 89518fd37a7SXin LIhas no effect. 89618fd37a7SXin LI 89718fd37a7SXin LIThe @option{-F} or @option{--show-function-line} option finds the nearest 89818fd37a7SXin LIunchanged line that precedes each hunk of differences and matches the 89918fd37a7SXin LIgiven regular expression. Then it adds that line to the end of the 90018fd37a7SXin LIline of asterisks in the context format, or to the @samp{@@@@} line in 90118fd37a7SXin LIunified format. If no matching line exists, this option leaves the output for 90218fd37a7SXin LIthat hunk unchanged. If that line is more than 40 characters long, it 90318fd37a7SXin LIoutputs only the first 40 characters. You can specify more than one 90418fd37a7SXin LIregular expression for such lines; @command{diff} tries to match each line 90518fd37a7SXin LIagainst each regular expression, starting with the last one given. This 90618fd37a7SXin LImeans that you can use @option{-p} and @option{-F} together, if you wish. 90718fd37a7SXin LI 90818fd37a7SXin LI@node C Function Headings 90918fd37a7SXin LI@subsubsection Showing C Function Headings 91018fd37a7SXin LI@cindex C function headings 91118fd37a7SXin LI@cindex function headings, C 91218fd37a7SXin LI 91318fd37a7SXin LITo show in which functions differences occur for C and similar 91418fd37a7SXin LIlanguages, you can use the @option{-p} or @option{--show-c-function} option. 91518fd37a7SXin LIThis option automatically defaults to the context output format 91618fd37a7SXin LI(@pxref{Context Format}), with the default number of lines of context. 91718fd37a7SXin LIYou can override that number with @option{-C @var{lines}} elsewhere in the 91818fd37a7SXin LIcommand line. You can override both the format and the number with 91918fd37a7SXin LI@option{-U @var{lines}} elsewhere in the command line. 92018fd37a7SXin LI 92118fd37a7SXin LIThe @option{-p} or @option{--show-c-function} option is equivalent to 92218fd37a7SXin LI@option{-F '^[[:alpha:]$_]'} if the unified format is specified, otherwise 92318fd37a7SXin LI@option{-c -F '^[[:alpha:]$_]'} (@pxref{Specified Headings}). @acronym{GNU} 92418fd37a7SXin LI@command{diff} provides this option for the sake of convenience. 92518fd37a7SXin LI 92618fd37a7SXin LI@node Alternate Names 92718fd37a7SXin LI@subsection Showing Alternate File Names 92818fd37a7SXin LI@cindex alternate file names 92918fd37a7SXin LI@cindex file name alternates 93018fd37a7SXin LI 93118fd37a7SXin LIIf you are comparing two files that have meaningless or uninformative 93218fd37a7SXin LInames, you might want @command{diff} to show alternate names in the header 93318fd37a7SXin LIof the context and unified output formats. To do this, use the 93418fd37a7SXin LI@option{--label=@var{label}} option. The first time 93518fd37a7SXin LIyou give this option, its argument replaces the name and date of the 93618fd37a7SXin LIfirst file in the header; the second time, its argument replaces the 93718fd37a7SXin LIname and date of the second file. If you give this option more than 93818fd37a7SXin LItwice, @command{diff} reports an error. The @option{--label} option does not 93918fd37a7SXin LIaffect the file names in the @command{pr} header when the @option{-l} or 94018fd37a7SXin LI@option{--paginate} option is used (@pxref{Pagination}). 94118fd37a7SXin LI 94218fd37a7SXin LIHere are the first two lines of the output from @samp{diff -C 2 94318fd37a7SXin LI--label=original --label=modified lao tzu}: 94418fd37a7SXin LI 94518fd37a7SXin LI@example 94618fd37a7SXin LI*** original 94718fd37a7SXin LI--- modified 94818fd37a7SXin LI@end example 94918fd37a7SXin LI 95018fd37a7SXin LI@node Side by Side 95118fd37a7SXin LI@section Showing Differences Side by Side 95218fd37a7SXin LI@cindex side by side 95318fd37a7SXin LI@cindex two-column output 95418fd37a7SXin LI@cindex columnar output 95518fd37a7SXin LI 95618fd37a7SXin LI@command{diff} can produce a side by side difference listing of two files. 95718fd37a7SXin LIThe files are listed in two columns with a gutter between them. The 95818fd37a7SXin LIgutter contains one of the following markers: 95918fd37a7SXin LI 96018fd37a7SXin LI@table @asis 96118fd37a7SXin LI@item white space 96218fd37a7SXin LIThe corresponding lines are in common. That is, either the lines are 96318fd37a7SXin LIidentical, or the difference is ignored because of one of the 96418fd37a7SXin LI@option{--ignore} options (@pxref{White Space}). 96518fd37a7SXin LI 96618fd37a7SXin LI@item @samp{|} 96718fd37a7SXin LIThe corresponding lines differ, and they are either both complete 96818fd37a7SXin LIor both incomplete. 96918fd37a7SXin LI 97018fd37a7SXin LI@item @samp{<} 97118fd37a7SXin LIThe files differ and only the first file contains the line. 97218fd37a7SXin LI 97318fd37a7SXin LI@item @samp{>} 97418fd37a7SXin LIThe files differ and only the second file contains the line. 97518fd37a7SXin LI 97618fd37a7SXin LI@item @samp{(} 97718fd37a7SXin LIOnly the first file contains the line, but the difference is ignored. 97818fd37a7SXin LI 97918fd37a7SXin LI@item @samp{)} 98018fd37a7SXin LIOnly the second file contains the line, but the difference is ignored. 98118fd37a7SXin LI 98218fd37a7SXin LI@item @samp{\} 98318fd37a7SXin LIThe corresponding lines differ, and only the first line is incomplete. 98418fd37a7SXin LI 98518fd37a7SXin LI@item @samp{/} 98618fd37a7SXin LIThe corresponding lines differ, and only the second line is incomplete. 98718fd37a7SXin LI@end table 98818fd37a7SXin LI 98918fd37a7SXin LINormally, an output line is incomplete if and only if the lines that it 99018fd37a7SXin LIcontains are incomplete; @xref{Incomplete Lines}. However, when an 99118fd37a7SXin LIoutput line represents two differing lines, one might be incomplete 99218fd37a7SXin LIwhile the other is not. In this case, the output line is complete, 99318fd37a7SXin LIbut its the gutter is marked @samp{\} if the first line is incomplete, 99418fd37a7SXin LI@samp{/} if the second line is. 99518fd37a7SXin LI 99618fd37a7SXin LISide by side format is sometimes easiest to read, but it has limitations. 99718fd37a7SXin LIIt generates much wider output than usual, and truncates lines that are 99818fd37a7SXin LItoo long to fit. Also, it relies on lining up output more heavily than 99918fd37a7SXin LIusual, so its output looks particularly bad if you use varying 100018fd37a7SXin LIwidth fonts, nonstandard tab stops, or nonprinting characters. 100118fd37a7SXin LI 100218fd37a7SXin LIYou can use the @command{sdiff} command to interactively merge side by side 100318fd37a7SXin LIdifferences. @xref{Interactive Merging}, for more information on merging files. 100418fd37a7SXin LI 100518fd37a7SXin LI@menu 100618fd37a7SXin LI* Side by Side Format:: Controlling side by side output format. 100718fd37a7SXin LI* Example Side by Side:: Sample side by side output. 100818fd37a7SXin LI@end menu 100918fd37a7SXin LI 101018fd37a7SXin LI@node Side by Side Format 101118fd37a7SXin LI@subsection Controlling Side by Side Format 101218fd37a7SXin LI@cindex side by side format 101318fd37a7SXin LI 101418fd37a7SXin LIThe @option{-y} or @option{--side-by-side} option selects side by side 101518fd37a7SXin LIformat. Because side by side output lines contain two input lines, the 101618fd37a7SXin LIoutput is wider than usual: normally 130 print columns, which can fit 101718fd37a7SXin LIonto a traditional printer line. You can set the width of the output 101818fd37a7SXin LIwith the @option{-W @var{columns}} or @option{--width=@var{columns}} 101918fd37a7SXin LIoption. The output is split into two halves of equal width, separated by a 102018fd37a7SXin LIsmall gutter to mark differences; the right half is aligned to a tab 102118fd37a7SXin LIstop so that tabs line up. Input lines that are too long to fit in half 102218fd37a7SXin LIof an output line are truncated for output. 102318fd37a7SXin LI 102418fd37a7SXin LIThe @option{--left-column} option prints only the left column of two 102518fd37a7SXin LIcommon lines. The @option{--suppress-common-lines} option suppresses 102618fd37a7SXin LIcommon lines entirely. 102718fd37a7SXin LI 102818fd37a7SXin LI@node Example Side by Side 102918fd37a7SXin LI@subsection An Example of Side by Side Format 103018fd37a7SXin LI 103118fd37a7SXin LIHere is the output of the command @samp{diff -y -W 72 lao tzu} 103218fd37a7SXin LI(@pxref{Sample diff Input}, for the complete contents of the two files). 103318fd37a7SXin LI 103418fd37a7SXin LI@example 103518fd37a7SXin LIThe Way that can be told of is n < 103618fd37a7SXin LIThe name that can be named is no < 103718fd37a7SXin LIThe Nameless is the origin of He The Nameless is the origin of He 103818fd37a7SXin LIThe Named is the mother of all t | The named is the mother of all t 103918fd37a7SXin LI > 104018fd37a7SXin LITherefore let there always be no Therefore let there always be no 104118fd37a7SXin LI so we may see their subtlety, so we may see their subtlety, 104218fd37a7SXin LIAnd let there always be being, And let there always be being, 104318fd37a7SXin LI so we may see their outcome. so we may see their outcome. 104418fd37a7SXin LIThe two are the same, The two are the same, 104518fd37a7SXin LIBut after they are produced, But after they are produced, 104618fd37a7SXin LI they have different names. they have different names. 104718fd37a7SXin LI > They both may be called deep and 104818fd37a7SXin LI > Deeper and more profound, 104918fd37a7SXin LI > The door of all subtleties! 105018fd37a7SXin LI@end example 105118fd37a7SXin LI 105218fd37a7SXin LI@node Normal 105318fd37a7SXin LI@section Showing Differences Without Context 105418fd37a7SXin LI@cindex normal output format 105518fd37a7SXin LI@cindex @samp{<} output format 105618fd37a7SXin LI 105718fd37a7SXin LIThe ``normal'' @command{diff} output format shows each hunk of differences 105818fd37a7SXin LIwithout any surrounding context. Sometimes such output is the clearest 105918fd37a7SXin LIway to see how lines have changed, without the clutter of nearby 106018fd37a7SXin LIunchanged lines (although you can get similar results with the context 106118fd37a7SXin LIor unified formats by using 0 lines of context). However, this format 106218fd37a7SXin LIis no longer widely used for sending out patches; for that purpose, the 106318fd37a7SXin LIcontext format (@pxref{Context Format}) and the unified format 106418fd37a7SXin LI(@pxref{Unified Format}) are superior. Normal format is the default for 106518fd37a7SXin LIcompatibility with older versions of @command{diff} and the @acronym{POSIX} 106618fd37a7SXin LIstandard. Use the @option{--normal} option to select this output 106718fd37a7SXin LIformat explicitly. 106818fd37a7SXin LI 106918fd37a7SXin LI@menu 107018fd37a7SXin LI* Example Normal:: Sample output in the normal format. 107118fd37a7SXin LI* Detailed Normal:: A detailed description of normal output format. 107218fd37a7SXin LI@end menu 107318fd37a7SXin LI 107418fd37a7SXin LI@node Example Normal 107518fd37a7SXin LI@subsection An Example of Normal Format 107618fd37a7SXin LI 107718fd37a7SXin LIHere is the output of the command @samp{diff lao tzu} 107818fd37a7SXin LI(@pxref{Sample diff Input}, for the complete contents of the two files). 107918fd37a7SXin LINotice that it shows only the lines that are different between the two 108018fd37a7SXin LIfiles. 108118fd37a7SXin LI 108218fd37a7SXin LI@example 108318fd37a7SXin LI1,2d0 108418fd37a7SXin LI< The Way that can be told of is not the eternal Way; 108518fd37a7SXin LI< The name that can be named is not the eternal name. 108618fd37a7SXin LI4c2,3 108718fd37a7SXin LI< The Named is the mother of all things. 108818fd37a7SXin LI--- 108918fd37a7SXin LI> The named is the mother of all things. 109018fd37a7SXin LI> 109118fd37a7SXin LI11a11,13 109218fd37a7SXin LI> They both may be called deep and profound. 109318fd37a7SXin LI> Deeper and more profound, 109418fd37a7SXin LI> The door of all subtleties! 109518fd37a7SXin LI@end example 109618fd37a7SXin LI 109718fd37a7SXin LI@node Detailed Normal 109818fd37a7SXin LI@subsection Detailed Description of Normal Format 109918fd37a7SXin LI 110018fd37a7SXin LIThe normal output format consists of one or more hunks of differences; 110118fd37a7SXin LIeach hunk shows one area where the files differ. Normal format hunks 110218fd37a7SXin LIlook like this: 110318fd37a7SXin LI 110418fd37a7SXin LI@example 110518fd37a7SXin LI@var{change-command} 110618fd37a7SXin LI< @var{from-file-line} 110718fd37a7SXin LI< @var{from-file-line}@dots{} 110818fd37a7SXin LI--- 110918fd37a7SXin LI> @var{to-file-line} 111018fd37a7SXin LI> @var{to-file-line}@dots{} 111118fd37a7SXin LI@end example 111218fd37a7SXin LI 111318fd37a7SXin LIThere are three types of change commands. Each consists of a line 111418fd37a7SXin LInumber or comma-separated range of lines in the first file, a single 111518fd37a7SXin LIcharacter indicating the kind of change to make, and a line number or 111618fd37a7SXin LIcomma-separated range of lines in the second file. All line numbers are 111718fd37a7SXin LIthe original line numbers in each file. The types of change commands 111818fd37a7SXin LIare: 111918fd37a7SXin LI 112018fd37a7SXin LI@table @samp 112118fd37a7SXin LI@item @var{l}a@var{r} 112218fd37a7SXin LIAdd the lines in range @var{r} of the second file after line @var{l} of 112318fd37a7SXin LIthe first file. For example, @samp{8a12,15} means append lines 12--15 112418fd37a7SXin LIof file 2 after line 8 of file 1; or, if changing file 2 into file 1, 112518fd37a7SXin LIdelete lines 12--15 of file 2. 112618fd37a7SXin LI 112718fd37a7SXin LI@item @var{f}c@var{t} 112818fd37a7SXin LIReplace the lines in range @var{f} of the first file with lines in range 112918fd37a7SXin LI@var{t} of the second file. This is like a combined add and delete, but 113018fd37a7SXin LImore compact. For example, @samp{5,7c8,10} means change lines 5--7 of 113118fd37a7SXin LIfile 1 to read as lines 8--10 of file 2; or, if changing file 2 into 113218fd37a7SXin LIfile 1, change lines 8--10 of file 2 to read as lines 5--7 of file 1. 113318fd37a7SXin LI 113418fd37a7SXin LI@item @var{r}d@var{l} 113518fd37a7SXin LIDelete the lines in range @var{r} from the first file; line @var{l} is where 113618fd37a7SXin LIthey would have appeared in the second file had they not been deleted. 113718fd37a7SXin LIFor example, @samp{5,7d3} means delete lines 5--7 of file 1; or, if 113818fd37a7SXin LIchanging file 2 into file 1, append lines 5--7 of file 1 after line 3 of 113918fd37a7SXin LIfile 2. 114018fd37a7SXin LI@end table 114118fd37a7SXin LI 114218fd37a7SXin LI@node Scripts 114318fd37a7SXin LI@section Making Edit Scripts 114418fd37a7SXin LI@cindex script output formats 114518fd37a7SXin LI 114618fd37a7SXin LISeveral output modes produce command scripts for editing @var{from-file} 114718fd37a7SXin LIto produce @var{to-file}. 114818fd37a7SXin LI 114918fd37a7SXin LI@menu 115018fd37a7SXin LI* ed Scripts:: Using @command{diff} to produce commands for @command{ed}. 115118fd37a7SXin LI* Forward ed:: Making forward @command{ed} scripts. 115218fd37a7SXin LI* RCS:: A special @command{diff} output format used by @acronym{RCS}. 115318fd37a7SXin LI@end menu 115418fd37a7SXin LI 115518fd37a7SXin LI@node ed Scripts 115618fd37a7SXin LI@subsection @command{ed} Scripts 115718fd37a7SXin LI@cindex @command{ed} script output format 115818fd37a7SXin LI 115918fd37a7SXin LI@command{diff} can produce commands that direct the @command{ed} text editor 116018fd37a7SXin LIto change the first file into the second file. Long ago, this was the 116118fd37a7SXin LIonly output mode that was suitable for editing one file into another 116218fd37a7SXin LIautomatically; today, with @command{patch}, it is almost obsolete. Use the 116318fd37a7SXin LI@option{-e} or @option{--ed} option to select this output format. 116418fd37a7SXin LI 116518fd37a7SXin LILike the normal format (@pxref{Normal}), this output format does not 116618fd37a7SXin LIshow any context; unlike the normal format, it does not include the 116718fd37a7SXin LIinformation necessary to apply the diff in reverse (to produce the first 116818fd37a7SXin LIfile if all you have is the second file and the diff). 116918fd37a7SXin LI 117018fd37a7SXin LIIf the file @file{d} contains the output of @samp{diff -e old new}, then 117118fd37a7SXin LIthe command @samp{(cat d && echo w) | ed - old} edits @file{old} to make 117218fd37a7SXin LIit a copy of @file{new}. More generally, if @file{d1}, @file{d2}, 117318fd37a7SXin LI@dots{}, @file{dN} contain the outputs of @samp{diff -e old new1}, 117418fd37a7SXin LI@samp{diff -e new1 new2}, @dots{}, @samp{diff -e newN-1 newN}, 117518fd37a7SXin LIrespectively, then the command @samp{(cat d1 d2 @dots{} dN && echo w) | 117618fd37a7SXin LIed - old} edits @file{old} to make it a copy of @file{newN}. 117718fd37a7SXin LI 117818fd37a7SXin LI@menu 117918fd37a7SXin LI* Example ed:: A sample @command{ed} script. 118018fd37a7SXin LI* Detailed ed:: A detailed description of @command{ed} format. 118118fd37a7SXin LI@end menu 118218fd37a7SXin LI 118318fd37a7SXin LI@node Example ed 118418fd37a7SXin LI@subsubsection Example @command{ed} Script 118518fd37a7SXin LI 118618fd37a7SXin LIHere is the output of @samp{diff -e lao tzu} (@pxref{Sample 118718fd37a7SXin LIdiff Input}, for the complete contents of the two files): 118818fd37a7SXin LI 118918fd37a7SXin LI@example 119018fd37a7SXin LI11a 119118fd37a7SXin LIThey both may be called deep and profound. 119218fd37a7SXin LIDeeper and more profound, 119318fd37a7SXin LIThe door of all subtleties! 119418fd37a7SXin LI. 119518fd37a7SXin LI4c 119618fd37a7SXin LIThe named is the mother of all things. 119718fd37a7SXin LI 119818fd37a7SXin LI. 119918fd37a7SXin LI1,2d 120018fd37a7SXin LI@end example 120118fd37a7SXin LI 120218fd37a7SXin LI@node Detailed ed 120318fd37a7SXin LI@subsubsection Detailed Description of @command{ed} Format 120418fd37a7SXin LI 120518fd37a7SXin LIThe @command{ed} output format consists of one or more hunks of 120618fd37a7SXin LIdifferences. The changes closest to the ends of the files come first so 120718fd37a7SXin LIthat commands that change the number of lines do not affect how 120818fd37a7SXin LI@command{ed} interprets line numbers in succeeding commands. @command{ed} 120918fd37a7SXin LIformat hunks look like this: 121018fd37a7SXin LI 121118fd37a7SXin LI@example 121218fd37a7SXin LI@var{change-command} 121318fd37a7SXin LI@var{to-file-line} 121418fd37a7SXin LI@var{to-file-line}@dots{} 121518fd37a7SXin LI. 121618fd37a7SXin LI@end example 121718fd37a7SXin LI 121818fd37a7SXin LIBecause @command{ed} uses a single period on a line to indicate the 121918fd37a7SXin LIend of input, @acronym{GNU} @command{diff} protects lines of changes 122018fd37a7SXin LIthat contain a single period on a line by writing two periods instead, 122118fd37a7SXin LIthen writing a subsequent @command{ed} command to change the two 122218fd37a7SXin LIperiods into one. The @command{ed} format cannot represent an 122318fd37a7SXin LIincomplete line, so if the second file ends in a changed incomplete 122418fd37a7SXin LIline, @command{diff} reports an error and then pretends that a newline 122518fd37a7SXin LIwas appended. 122618fd37a7SXin LI 122718fd37a7SXin LIThere are three types of change commands. Each consists of a line 122818fd37a7SXin LInumber or comma-separated range of lines in the first file and a single 122918fd37a7SXin LIcharacter indicating the kind of change to make. All line numbers are 123018fd37a7SXin LIthe original line numbers in the file. The types of change commands 123118fd37a7SXin LIare: 123218fd37a7SXin LI 123318fd37a7SXin LI@table @samp 123418fd37a7SXin LI@item @var{l}a 123518fd37a7SXin LIAdd text from the second file after line @var{l} in the first file. For 123618fd37a7SXin LIexample, @samp{8a} means to add the following lines after line 8 of file 123718fd37a7SXin LI1. 123818fd37a7SXin LI 123918fd37a7SXin LI@item @var{r}c 124018fd37a7SXin LIReplace the lines in range @var{r} in the first file with the following 124118fd37a7SXin LIlines. Like a combined add and delete, but more compact. For example, 124218fd37a7SXin LI@samp{5,7c} means change lines 5--7 of file 1 to read as the text file 124318fd37a7SXin LI2. 124418fd37a7SXin LI 124518fd37a7SXin LI@item @var{r}d 124618fd37a7SXin LIDelete the lines in range @var{r} from the first file. For example, 124718fd37a7SXin LI@samp{5,7d} means delete lines 5--7 of file 1. 124818fd37a7SXin LI@end table 124918fd37a7SXin LI 125018fd37a7SXin LI@node Forward ed 125118fd37a7SXin LI@subsection Forward @command{ed} Scripts 125218fd37a7SXin LI@cindex forward @command{ed} script output format 125318fd37a7SXin LI 125418fd37a7SXin LI@command{diff} can produce output that is like an @command{ed} script, but 125518fd37a7SXin LIwith hunks in forward (front to back) order. The format of the commands 125618fd37a7SXin LIis also changed slightly: command characters precede the lines they 125718fd37a7SXin LImodify, spaces separate line numbers in ranges, and no attempt is made 125818fd37a7SXin LIto disambiguate hunk lines consisting of a single period. Like 125918fd37a7SXin LI@command{ed} format, forward @command{ed} format cannot represent incomplete 126018fd37a7SXin LIlines. 126118fd37a7SXin LI 126218fd37a7SXin LIForward @command{ed} format is not very useful, because neither @command{ed} 126318fd37a7SXin LInor @command{patch} can apply diffs in this format. It exists mainly for 126418fd37a7SXin LIcompatibility with older versions of @command{diff}. Use the @option{-f} or 126518fd37a7SXin LI@option{--forward-ed} option to select it. 126618fd37a7SXin LI 126718fd37a7SXin LI@node RCS 126818fd37a7SXin LI@subsection @acronym{RCS} Scripts 126918fd37a7SXin LI@cindex @acronym{RCS} script output format 127018fd37a7SXin LI 127118fd37a7SXin LIThe @acronym{RCS} output format is designed specifically for use by 127218fd37a7SXin LIthe Revision Control System, which is a set of free programs used for 127318fd37a7SXin LIorganizing different versions and systems of files. Use the 127418fd37a7SXin LI@option{-n} or @option{--rcs} option to select this output format. It 127518fd37a7SXin LIis like the forward @command{ed} format (@pxref{Forward ed}), but it 127618fd37a7SXin LIcan represent arbitrary changes to the contents of a file because it 127718fd37a7SXin LIavoids the forward @command{ed} format's problems with lines 127818fd37a7SXin LIconsisting of a single period and with incomplete lines. Instead of 127918fd37a7SXin LIending text sections with a line consisting of a single period, each 128018fd37a7SXin LIcommand specifies the number of lines it affects; a combination of the 128118fd37a7SXin LI@samp{a} and @samp{d} commands are used instead of @samp{c}. Also, if 128218fd37a7SXin LIthe second file ends in a changed incomplete line, then the output 128318fd37a7SXin LIalso ends in an incomplete line. 128418fd37a7SXin LI 128518fd37a7SXin LIHere is the output of @samp{diff -n lao tzu} (@pxref{Sample 128618fd37a7SXin LIdiff Input}, for the complete contents of the two files): 128718fd37a7SXin LI 128818fd37a7SXin LI@example 128918fd37a7SXin LId1 2 129018fd37a7SXin LId4 1 129118fd37a7SXin LIa4 2 129218fd37a7SXin LIThe named is the mother of all things. 129318fd37a7SXin LI 129418fd37a7SXin LIa11 3 129518fd37a7SXin LIThey both may be called deep and profound. 129618fd37a7SXin LIDeeper and more profound, 129718fd37a7SXin LIThe door of all subtleties! 129818fd37a7SXin LI@end example 129918fd37a7SXin LI 130018fd37a7SXin LI@node If-then-else 130118fd37a7SXin LI@section Merging Files with If-then-else 130218fd37a7SXin LI@cindex merged output format 130318fd37a7SXin LI@cindex if-then-else output format 130418fd37a7SXin LI@cindex C if-then-else output format 130518fd37a7SXin LI@cindex @command{ifdef} output format 130618fd37a7SXin LI 130718fd37a7SXin LIYou can use @command{diff} to merge two files of C source code. The output 130818fd37a7SXin LIof @command{diff} in this format contains all the lines of both files. 130918fd37a7SXin LILines common to both files are output just once; the differing parts are 131018fd37a7SXin LIseparated by the C preprocessor directives @code{#ifdef @var{name}} or 131118fd37a7SXin LI@code{#ifndef @var{name}}, @code{#else}, and @code{#endif}. When 131218fd37a7SXin LIcompiling the output, you select which version to use by either defining 131318fd37a7SXin LIor leaving undefined the macro @var{name}. 131418fd37a7SXin LI 131518fd37a7SXin LITo merge two files, use @command{diff} with the @option{-D @var{name}} or 131618fd37a7SXin LI@option{--ifdef=@var{name}} option. The argument @var{name} is the C 131718fd37a7SXin LIpreprocessor identifier to use in the @code{#ifdef} and @code{#ifndef} 131818fd37a7SXin LIdirectives. 131918fd37a7SXin LI 132018fd37a7SXin LIFor example, if you change an instance of @code{wait (&s)} to 132118fd37a7SXin LI@code{waitpid (-1, &s, 0)} and then merge the old and new files with 132218fd37a7SXin LIthe @option{--ifdef=HAVE_WAITPID} option, then the affected part of your code 132318fd37a7SXin LImight look like this: 132418fd37a7SXin LI 132518fd37a7SXin LI@example 132618fd37a7SXin LI do @{ 132718fd37a7SXin LI#ifndef HAVE_WAITPID 132818fd37a7SXin LI if ((w = wait (&s)) < 0 && errno != EINTR) 132918fd37a7SXin LI#else /* HAVE_WAITPID */ 133018fd37a7SXin LI if ((w = waitpid (-1, &s, 0)) < 0 && errno != EINTR) 133118fd37a7SXin LI#endif /* HAVE_WAITPID */ 133218fd37a7SXin LI return w; 133318fd37a7SXin LI @} while (w != child); 133418fd37a7SXin LI@end example 133518fd37a7SXin LI 133618fd37a7SXin LIYou can specify formats for languages other than C by using line group 133718fd37a7SXin LIformats and line formats, as described in the next sections. 133818fd37a7SXin LI 133918fd37a7SXin LI@menu 134018fd37a7SXin LI* Line Group Formats:: Formats for general if-then-else line groups. 134118fd37a7SXin LI* Line Formats:: Formats for each line in a line group. 134218fd37a7SXin LI* Example If-then-else:: Sample if-then-else format output. 134318fd37a7SXin LI* Detailed If-then-else:: A detailed description of if-then-else format. 134418fd37a7SXin LI@end menu 134518fd37a7SXin LI 134618fd37a7SXin LI@node Line Group Formats 134718fd37a7SXin LI@subsection Line Group Formats 134818fd37a7SXin LI@cindex line group formats 134918fd37a7SXin LI@cindex formats for if-then-else line groups 135018fd37a7SXin LI 135118fd37a7SXin LILine group formats let you specify formats suitable for many 135218fd37a7SXin LIapplications that allow if-then-else input, including programming 135318fd37a7SXin LIlanguages and text formatting languages. A line group format specifies 135418fd37a7SXin LIthe output format for a contiguous group of similar lines. 135518fd37a7SXin LI 135618fd37a7SXin LIFor example, the following command compares the TeX files @file{old} 135718fd37a7SXin LIand @file{new}, and outputs a merged file in which old regions are 135818fd37a7SXin LIsurrounded by @samp{\begin@{em@}}-@samp{\end@{em@}} lines, and new 135918fd37a7SXin LIregions are surrounded by @samp{\begin@{bf@}}-@samp{\end@{bf@}} lines. 136018fd37a7SXin LI 136118fd37a7SXin LI@example 136218fd37a7SXin LIdiff \ 136318fd37a7SXin LI --old-group-format='\begin@{em@} 136418fd37a7SXin LI%<\end@{em@} 136518fd37a7SXin LI' \ 136618fd37a7SXin LI --new-group-format='\begin@{bf@} 136718fd37a7SXin LI%>\end@{bf@} 136818fd37a7SXin LI' \ 136918fd37a7SXin LI old new 137018fd37a7SXin LI@end example 137118fd37a7SXin LI 137218fd37a7SXin LIThe following command is equivalent to the above example, but it is a 137318fd37a7SXin LIlittle more verbose, because it spells out the default line group formats. 137418fd37a7SXin LI 137518fd37a7SXin LI@example 137618fd37a7SXin LIdiff \ 137718fd37a7SXin LI --old-group-format='\begin@{em@} 137818fd37a7SXin LI%<\end@{em@} 137918fd37a7SXin LI' \ 138018fd37a7SXin LI --new-group-format='\begin@{bf@} 138118fd37a7SXin LI%>\end@{bf@} 138218fd37a7SXin LI' \ 138318fd37a7SXin LI --unchanged-group-format='%=' \ 138418fd37a7SXin LI --changed-group-format='\begin@{em@} 138518fd37a7SXin LI%<\end@{em@} 138618fd37a7SXin LI\begin@{bf@} 138718fd37a7SXin LI%>\end@{bf@} 138818fd37a7SXin LI' \ 138918fd37a7SXin LI old new 139018fd37a7SXin LI@end example 139118fd37a7SXin LI 139218fd37a7SXin LIHere is a more advanced example, which outputs a diff listing with 139318fd37a7SXin LIheaders containing line numbers in a ``plain English'' style. 139418fd37a7SXin LI 139518fd37a7SXin LI@example 139618fd37a7SXin LIdiff \ 139718fd37a7SXin LI --unchanged-group-format='' \ 139818fd37a7SXin LI --old-group-format='-------- %dn line%(n=1?:s) deleted at %df: 139918fd37a7SXin LI%<' \ 140018fd37a7SXin LI --new-group-format='-------- %dN line%(N=1?:s) added after %de: 140118fd37a7SXin LI%>' \ 140218fd37a7SXin LI --changed-group-format='-------- %dn line%(n=1?:s) changed at %df: 140318fd37a7SXin LI%<-------- to: 140418fd37a7SXin LI%>' \ 140518fd37a7SXin LI old new 140618fd37a7SXin LI@end example 140718fd37a7SXin LI 140818fd37a7SXin LITo specify a line group format, use @command{diff} with one of the options 140918fd37a7SXin LIlisted below. You can specify up to four line group formats, one for 141018fd37a7SXin LIeach kind of line group. You should quote @var{format}, because it 141118fd37a7SXin LItypically contains shell metacharacters. 141218fd37a7SXin LI 141318fd37a7SXin LI@table @option 141418fd37a7SXin LI@item --old-group-format=@var{format} 141518fd37a7SXin LIThese line groups are hunks containing only lines from the first file. 141618fd37a7SXin LIThe default old group format is the same as the changed group format if 141718fd37a7SXin LIit is specified; otherwise it is a format that outputs the line group as-is. 141818fd37a7SXin LI 141918fd37a7SXin LI@item --new-group-format=@var{format} 142018fd37a7SXin LIThese line groups are hunks containing only lines from the second 142118fd37a7SXin LIfile. The default new group format is same as the changed group 142218fd37a7SXin LIformat if it is specified; otherwise it is a format that outputs the 142318fd37a7SXin LIline group as-is. 142418fd37a7SXin LI 142518fd37a7SXin LI@item --changed-group-format=@var{format} 142618fd37a7SXin LIThese line groups are hunks containing lines from both files. The 142718fd37a7SXin LIdefault changed group format is the concatenation of the old and new 142818fd37a7SXin LIgroup formats. 142918fd37a7SXin LI 143018fd37a7SXin LI@item --unchanged-group-format=@var{format} 143118fd37a7SXin LIThese line groups contain lines common to both files. The default 143218fd37a7SXin LIunchanged group format is a format that outputs the line group as-is. 143318fd37a7SXin LI@end table 143418fd37a7SXin LI 143518fd37a7SXin LIIn a line group format, ordinary characters represent themselves; 143618fd37a7SXin LIconversion specifications start with @samp{%} and have one of the 143718fd37a7SXin LIfollowing forms. 143818fd37a7SXin LI 143918fd37a7SXin LI@table @samp 144018fd37a7SXin LI@item %< 144118fd37a7SXin LIstands for the lines from the first file, including the trailing newline. 144218fd37a7SXin LIEach line is formatted according to the old line format (@pxref{Line Formats}). 144318fd37a7SXin LI 144418fd37a7SXin LI@item %> 144518fd37a7SXin LIstands for the lines from the second file, including the trailing newline. 144618fd37a7SXin LIEach line is formatted according to the new line format. 144718fd37a7SXin LI 144818fd37a7SXin LI@item %= 144918fd37a7SXin LIstands for the lines common to both files, including the trailing newline. 145018fd37a7SXin LIEach line is formatted according to the unchanged line format. 145118fd37a7SXin LI 145218fd37a7SXin LI@item %% 145318fd37a7SXin LIstands for @samp{%}. 145418fd37a7SXin LI 145518fd37a7SXin LI@item %c'@var{C}' 145618fd37a7SXin LIwhere @var{C} is a single character, stands for @var{C}. 145718fd37a7SXin LI@var{C} may not be a backslash or an apostrophe. 145818fd37a7SXin LIFor example, @samp{%c':'} stands for a colon, even inside 145918fd37a7SXin LIthe then-part of an if-then-else format, which a colon would 146018fd37a7SXin LInormally terminate. 146118fd37a7SXin LI 146218fd37a7SXin LI@item %c'\@var{O}' 146318fd37a7SXin LIwhere @var{O} is a string of 1, 2, or 3 octal digits, 146418fd37a7SXin LIstands for the character with octal code @var{O}. 146518fd37a7SXin LIFor example, @samp{%c'\0'} stands for a null character. 146618fd37a7SXin LI 146718fd37a7SXin LI@item @var{F}@var{n} 146818fd37a7SXin LIwhere @var{F} is a @code{printf} conversion specification and @var{n} is one 146918fd37a7SXin LIof the following letters, stands for @var{n}'s value formatted with @var{F}. 147018fd37a7SXin LI 147118fd37a7SXin LI@table @samp 147218fd37a7SXin LI@item e 147318fd37a7SXin LIThe line number of the line just before the group in the old file. 147418fd37a7SXin LI 147518fd37a7SXin LI@item f 147618fd37a7SXin LIThe line number of the first line in the group in the old file; 147718fd37a7SXin LIequals @var{e} + 1. 147818fd37a7SXin LI 147918fd37a7SXin LI@item l 148018fd37a7SXin LIThe line number of the last line in the group in the old file. 148118fd37a7SXin LI 148218fd37a7SXin LI@item m 148318fd37a7SXin LIThe line number of the line just after the group in the old file; 148418fd37a7SXin LIequals @var{l} + 1. 148518fd37a7SXin LI 148618fd37a7SXin LI@item n 148718fd37a7SXin LIThe number of lines in the group in the old file; equals @var{l} - @var{f} + 1. 148818fd37a7SXin LI 148918fd37a7SXin LI@item E, F, L, M, N 149018fd37a7SXin LILikewise, for lines in the new file. 149118fd37a7SXin LI 149218fd37a7SXin LI@end table 149318fd37a7SXin LI 149418fd37a7SXin LI@vindex LC_NUMERIC 149518fd37a7SXin LIThe @code{printf} conversion specification can be @samp{%d}, 149618fd37a7SXin LI@samp{%o}, @samp{%x}, or @samp{%X}, specifying decimal, octal, 149718fd37a7SXin LIlower case hexadecimal, or upper case hexadecimal output 149818fd37a7SXin LIrespectively. After the @samp{%} the following options can appear in 149918fd37a7SXin LIsequence: a series of zero or more flags; an integer 150018fd37a7SXin LIspecifying the minimum field width; and a period followed by an 150118fd37a7SXin LIoptional integer specifying the minimum number of digits. 150218fd37a7SXin LIThe flags are @samp{-} for left-justification, @samp{'} for separating 150318fd37a7SXin LIthe digit into groups as specified by the @env{LC_NUMERIC} locale category, 150418fd37a7SXin LIand @samp{0} for padding with zeros instead of spaces. 150518fd37a7SXin LIFor example, @samp{%5dN} prints the number of new lines in the group 150618fd37a7SXin LIin a field of width 5 characters, using the @code{printf} format @code{"%5d"}. 150718fd37a7SXin LI 150818fd37a7SXin LI@item (@var{A}=@var{B}?@var{T}:@var{E}) 150918fd37a7SXin LIIf @var{A} equals @var{B} then @var{T} else @var{E}. 151018fd37a7SXin LI@var{A} and @var{B} are each either a decimal constant 151118fd37a7SXin LIor a single letter interpreted as above. 151218fd37a7SXin LIThis format spec is equivalent to @var{T} if 151318fd37a7SXin LI@var{A}'s value equals @var{B}'s; otherwise it is equivalent to @var{E}. 151418fd37a7SXin LI 151518fd37a7SXin LIFor example, @samp{%(N=0?no:%dN) line%(N=1?:s)} is equivalent to 151618fd37a7SXin LI@samp{no lines} if @var{N} (the number of lines in the group in the 151718fd37a7SXin LInew file) is 0, to @samp{1 line} if @var{N} is 1, and to @samp{%dN lines} 151818fd37a7SXin LIotherwise. 151918fd37a7SXin LI@end table 152018fd37a7SXin LI 152118fd37a7SXin LI@node Line Formats 152218fd37a7SXin LI@subsection Line Formats 152318fd37a7SXin LI@cindex line formats 152418fd37a7SXin LI 152518fd37a7SXin LILine formats control how each line taken from an input file is 152618fd37a7SXin LIoutput as part of a line group in if-then-else format. 152718fd37a7SXin LI 152818fd37a7SXin LIFor example, the following command outputs text with a one-character 152918fd37a7SXin LIchange indicator to the left of the text. The first character of output 153018fd37a7SXin LIis @samp{-} for deleted lines, @samp{|} for added lines, and a space for 153118fd37a7SXin LIunchanged lines. The formats contain newline characters where newlines 153218fd37a7SXin LIare desired on output. 153318fd37a7SXin LI 153418fd37a7SXin LI@example 153518fd37a7SXin LIdiff \ 153618fd37a7SXin LI --old-line-format='-%l 153718fd37a7SXin LI' \ 153818fd37a7SXin LI --new-line-format='|%l 153918fd37a7SXin LI' \ 154018fd37a7SXin LI --unchanged-line-format=' %l 154118fd37a7SXin LI' \ 154218fd37a7SXin LI old new 154318fd37a7SXin LI@end example 154418fd37a7SXin LI 154518fd37a7SXin LITo specify a line format, use one of the following options. You should 154618fd37a7SXin LIquote @var{format}, since it often contains shell metacharacters. 154718fd37a7SXin LI 154818fd37a7SXin LI@table @option 154918fd37a7SXin LI@item --old-line-format=@var{format} 155018fd37a7SXin LIformats lines just from the first file. 155118fd37a7SXin LI 155218fd37a7SXin LI@item --new-line-format=@var{format} 155318fd37a7SXin LIformats lines just from the second file. 155418fd37a7SXin LI 155518fd37a7SXin LI@item --unchanged-line-format=@var{format} 155618fd37a7SXin LIformats lines common to both files. 155718fd37a7SXin LI 155818fd37a7SXin LI@item --line-format=@var{format} 155918fd37a7SXin LIformats all lines; in effect, it sets all three above options simultaneously. 156018fd37a7SXin LI@end table 156118fd37a7SXin LI 156218fd37a7SXin LIIn a line format, ordinary characters represent themselves; 156318fd37a7SXin LIconversion specifications start with @samp{%} and have one of the 156418fd37a7SXin LIfollowing forms. 156518fd37a7SXin LI 156618fd37a7SXin LI@table @samp 156718fd37a7SXin LI@item %l 156818fd37a7SXin LIstands for the contents of the line, not counting its trailing 156918fd37a7SXin LInewline (if any). This format ignores whether the line is incomplete; 157018fd37a7SXin LI@xref{Incomplete Lines}. 157118fd37a7SXin LI 157218fd37a7SXin LI@item %L 157318fd37a7SXin LIstands for the contents of the line, including its trailing newline 157418fd37a7SXin LI(if any). If a line is incomplete, this format preserves its 157518fd37a7SXin LIincompleteness. 157618fd37a7SXin LI 157718fd37a7SXin LI@item %% 157818fd37a7SXin LIstands for @samp{%}. 157918fd37a7SXin LI 158018fd37a7SXin LI@item %c'@var{C}' 158118fd37a7SXin LIwhere @var{C} is a single character, stands for @var{C}. 158218fd37a7SXin LI@var{C} may not be a backslash or an apostrophe. 158318fd37a7SXin LIFor example, @samp{%c':'} stands for a colon. 158418fd37a7SXin LI 158518fd37a7SXin LI@item %c'\@var{O}' 158618fd37a7SXin LIwhere @var{O} is a string of 1, 2, or 3 octal digits, 158718fd37a7SXin LIstands for the character with octal code @var{O}. 158818fd37a7SXin LIFor example, @samp{%c'\0'} stands for a null character. 158918fd37a7SXin LI 159018fd37a7SXin LI@item @var{F}n 159118fd37a7SXin LIwhere @var{F} is a @code{printf} conversion specification, 159218fd37a7SXin LIstands for the line number formatted with @var{F}. 159318fd37a7SXin LIFor example, @samp{%.5dn} prints the line number using the 159418fd37a7SXin LI@code{printf} format @code{"%.5d"}. @xref{Line Group Formats}, for 159518fd37a7SXin LImore about printf conversion specifications. 159618fd37a7SXin LI 159718fd37a7SXin LI@end table 159818fd37a7SXin LI 159918fd37a7SXin LIThe default line format is @samp{%l} followed by a newline character. 160018fd37a7SXin LI 160118fd37a7SXin LIIf the input contains tab characters and it is important that they line 160218fd37a7SXin LIup on output, you should ensure that @samp{%l} or @samp{%L} in a line 160318fd37a7SXin LIformat is just after a tab stop (e.g.@: by preceding @samp{%l} or 160418fd37a7SXin LI@samp{%L} with a tab character), or you should use the @option{-t} or 160518fd37a7SXin LI@option{--expand-tabs} option. 160618fd37a7SXin LI 160718fd37a7SXin LITaken together, the line and line group formats let you specify many 160818fd37a7SXin LIdifferent formats. For example, the following command uses a format 160918fd37a7SXin LIsimilar to normal @command{diff} format. You can tailor this command 161018fd37a7SXin LIto get fine control over @command{diff} output. 161118fd37a7SXin LI 161218fd37a7SXin LI@example 161318fd37a7SXin LIdiff \ 161418fd37a7SXin LI --old-line-format='< %l 161518fd37a7SXin LI' \ 161618fd37a7SXin LI --new-line-format='> %l 161718fd37a7SXin LI' \ 161818fd37a7SXin LI --old-group-format='%df%(f=l?:,%dl)d%dE 161918fd37a7SXin LI%<' \ 162018fd37a7SXin LI --new-group-format='%dea%dF%(F=L?:,%dL) 162118fd37a7SXin LI%>' \ 162218fd37a7SXin LI --changed-group-format='%df%(f=l?:,%dl)c%dF%(F=L?:,%dL) 162318fd37a7SXin LI%<--- 162418fd37a7SXin LI%>' \ 162518fd37a7SXin LI --unchanged-group-format='' \ 162618fd37a7SXin LI old new 162718fd37a7SXin LI@end example 162818fd37a7SXin LI 162918fd37a7SXin LI@node Example If-then-else 163018fd37a7SXin LI@subsection An Example of If-then-else Format 163118fd37a7SXin LI 163218fd37a7SXin LIHere is the output of @samp{diff -DTWO lao tzu} (@pxref{Sample 163318fd37a7SXin LIdiff Input}, for the complete contents of the two files): 163418fd37a7SXin LI 163518fd37a7SXin LI@example 163618fd37a7SXin LI#ifndef TWO 163718fd37a7SXin LIThe Way that can be told of is not the eternal Way; 163818fd37a7SXin LIThe name that can be named is not the eternal name. 163918fd37a7SXin LI#endif /* ! TWO */ 164018fd37a7SXin LIThe Nameless is the origin of Heaven and Earth; 164118fd37a7SXin LI#ifndef TWO 164218fd37a7SXin LIThe Named is the mother of all things. 164318fd37a7SXin LI#else /* TWO */ 164418fd37a7SXin LIThe named is the mother of all things. 164518fd37a7SXin LI 164618fd37a7SXin LI#endif /* TWO */ 164718fd37a7SXin LITherefore let there always be non-being, 164818fd37a7SXin LI so we may see their subtlety, 164918fd37a7SXin LIAnd let there always be being, 165018fd37a7SXin LI so we may see their outcome. 165118fd37a7SXin LIThe two are the same, 165218fd37a7SXin LIBut after they are produced, 165318fd37a7SXin LI they have different names. 165418fd37a7SXin LI#ifdef TWO 165518fd37a7SXin LIThey both may be called deep and profound. 165618fd37a7SXin LIDeeper and more profound, 165718fd37a7SXin LIThe door of all subtleties! 165818fd37a7SXin LI#endif /* TWO */ 165918fd37a7SXin LI@end example 166018fd37a7SXin LI 166118fd37a7SXin LI@node Detailed If-then-else 166218fd37a7SXin LI@subsection Detailed Description of If-then-else Format 166318fd37a7SXin LI 166418fd37a7SXin LIFor lines common to both files, @command{diff} uses the unchanged line 166518fd37a7SXin LIgroup format. For each hunk of differences in the merged output 166618fd37a7SXin LIformat, if the hunk contains only lines from the first file, 166718fd37a7SXin LI@command{diff} uses the old line group format; if the hunk contains only 166818fd37a7SXin LIlines from the second file, @command{diff} uses the new group format; 166918fd37a7SXin LIotherwise, @command{diff} uses the changed group format. 167018fd37a7SXin LI 167118fd37a7SXin LIThe old, new, and unchanged line formats specify the output format of 167218fd37a7SXin LIlines from the first file, lines from the second file, and lines common 167318fd37a7SXin LIto both files, respectively. 167418fd37a7SXin LI 167518fd37a7SXin LIThe option @option{--ifdef=@var{name}} is equivalent to 167618fd37a7SXin LIthe following sequence of options using shell syntax: 167718fd37a7SXin LI 167818fd37a7SXin LI@example 167918fd37a7SXin LI--old-group-format='#ifndef @var{name} 168018fd37a7SXin LI%<#endif /* ! @var{name} */ 168118fd37a7SXin LI' \ 168218fd37a7SXin LI--new-group-format='#ifdef @var{name} 168318fd37a7SXin LI%>#endif /* @var{name} */ 168418fd37a7SXin LI' \ 168518fd37a7SXin LI--unchanged-group-format='%=' \ 168618fd37a7SXin LI--changed-group-format='#ifndef @var{name} 168718fd37a7SXin LI%<#else /* @var{name} */ 168818fd37a7SXin LI%>#endif /* @var{name} */ 168918fd37a7SXin LI' 169018fd37a7SXin LI@end example 169118fd37a7SXin LI 169218fd37a7SXin LIYou should carefully check the @command{diff} output for proper nesting. 169318fd37a7SXin LIFor example, when using the @option{-D @var{name}} or 169418fd37a7SXin LI@option{--ifdef=@var{name}} option, you should check that if the 169518fd37a7SXin LIdiffering lines contain any of the C preprocessor directives 169618fd37a7SXin LI@samp{#ifdef}, @samp{#ifndef}, @samp{#else}, @samp{#elif}, or 169718fd37a7SXin LI@samp{#endif}, they are nested properly and match. If they don't, you 169818fd37a7SXin LImust make corrections manually. It is a good idea to carefully check 169918fd37a7SXin LIthe resulting code anyway to make sure that it really does what you 170018fd37a7SXin LIwant it to; depending on how the input files were produced, the output 170118fd37a7SXin LImight contain duplicate or otherwise incorrect code. 170218fd37a7SXin LI 170318fd37a7SXin LIThe @command{patch} @option{-D @var{name}} option behaves like 170418fd37a7SXin LIthe @command{diff} @option{-D @var{name}} option, except it operates on 170518fd37a7SXin LIa file and a diff to produce a merged file; @xref{patch Options}. 170618fd37a7SXin LI 170718fd37a7SXin LI@node Incomplete Lines 170818fd37a7SXin LI@chapter Incomplete Lines 170918fd37a7SXin LI@cindex incomplete lines 171018fd37a7SXin LI@cindex full lines 171118fd37a7SXin LI@cindex newline treatment by @command{diff} 171218fd37a7SXin LI 171318fd37a7SXin LIWhen an input file ends in a non-newline character, its last line is 171418fd37a7SXin LIcalled an @dfn{incomplete line} because its last character is not a 171518fd37a7SXin LInewline. All other lines are called @dfn{full lines} and end in a 171618fd37a7SXin LInewline character. Incomplete lines do not match full lines unless 171718fd37a7SXin LIdifferences in white space are ignored (@pxref{White Space}). 171818fd37a7SXin LI 171918fd37a7SXin LIAn incomplete line is normally distinguished on output from a full 172018fd37a7SXin LIline by a following line that starts with @samp{\}. However, the 172118fd37a7SXin LI@acronym{RCS} format (@pxref{RCS}) outputs the incomplete line as-is, 172218fd37a7SXin LIwithout any trailing newline or following line. The side by side 172318fd37a7SXin LIformat normally represents incomplete lines as-is, but in some cases 172418fd37a7SXin LIuses a @samp{\} or @samp{/} gutter marker; @xref{Side by Side}. The 172518fd37a7SXin LIif-then-else line format preserves a line's incompleteness with 172618fd37a7SXin LI@samp{%L}, and discards the newline with @samp{%l}; @xref{Line 172718fd37a7SXin LIFormats}. Finally, with the @command{ed} and forward @command{ed} 172818fd37a7SXin LIoutput formats (@pxref{Output Formats}) @command{diff} cannot 172918fd37a7SXin LIrepresent an incomplete line, so it pretends there was a newline and 173018fd37a7SXin LIreports an error. 173118fd37a7SXin LI 173218fd37a7SXin LIFor example, suppose @file{F} and @file{G} are one-byte files that 173318fd37a7SXin LIcontain just @samp{f} and @samp{g}, respectively. Then @samp{diff F G} 173418fd37a7SXin LIoutputs 173518fd37a7SXin LI 173618fd37a7SXin LI@example 173718fd37a7SXin LI1c1 173818fd37a7SXin LI< f 173918fd37a7SXin LI\ No newline at end of file 174018fd37a7SXin LI--- 174118fd37a7SXin LI> g 174218fd37a7SXin LI\ No newline at end of file 174318fd37a7SXin LI@end example 174418fd37a7SXin LI 174518fd37a7SXin LI@noindent 174618fd37a7SXin LI(The exact message may differ in non-English locales.) 174718fd37a7SXin LI@samp{diff -n F G} outputs the following without a trailing newline: 174818fd37a7SXin LI 174918fd37a7SXin LI@example 175018fd37a7SXin LId1 1 175118fd37a7SXin LIa1 1 175218fd37a7SXin LIg 175318fd37a7SXin LI@end example 175418fd37a7SXin LI 175518fd37a7SXin LI@noindent 175618fd37a7SXin LI@samp{diff -e F G} reports two errors and outputs the following: 175718fd37a7SXin LI 175818fd37a7SXin LI@example 175918fd37a7SXin LI1c 176018fd37a7SXin LIg 176118fd37a7SXin LI. 176218fd37a7SXin LI@end example 176318fd37a7SXin LI 176418fd37a7SXin LI@node Comparing Directories 176518fd37a7SXin LI@chapter Comparing Directories 176618fd37a7SXin LI 176718fd37a7SXin LI@vindex LC_COLLATE 176818fd37a7SXin LIYou can use @command{diff} to compare some or all of the files in two 176918fd37a7SXin LIdirectory trees. When both file name arguments to @command{diff} are 177018fd37a7SXin LIdirectories, it compares each file that is contained in both 177118fd37a7SXin LIdirectories, examining file names in alphabetical order as specified by 177218fd37a7SXin LIthe @env{LC_COLLATE} locale category. Normally 177318fd37a7SXin LI@command{diff} is silent about pairs of files that contain no differences, 177418fd37a7SXin LIbut if you use the @option{-s} or @option{--report-identical-files} option, 177518fd37a7SXin LIit reports pairs of identical files. Normally @command{diff} reports 177618fd37a7SXin LIsubdirectories common to both directories without comparing 177718fd37a7SXin LIsubdirectories' files, but if you use the @option{-r} or 177818fd37a7SXin LI@option{--recursive} option, it compares every corresponding pair of files 177918fd37a7SXin LIin the directory trees, as many levels deep as they go. 178018fd37a7SXin LI 178118fd37a7SXin LIFor file names that are in only one of the directories, @command{diff} 178218fd37a7SXin LInormally does not show the contents of the file that exists; it reports 178318fd37a7SXin LIonly that the file exists in that directory and not in the other. You 178418fd37a7SXin LIcan make @command{diff} act as though the file existed but was empty in the 178518fd37a7SXin LIother directory, so that it outputs the entire contents of the file that 178618fd37a7SXin LIactually exists. (It is output as either an insertion or a 178718fd37a7SXin LIdeletion, depending on whether it is in the first or the second 178818fd37a7SXin LIdirectory given.) To do this, use the @option{-N} or @option{--new-file} 178918fd37a7SXin LIoption. 179018fd37a7SXin LI 179118fd37a7SXin LIIf the older directory contains one or more large files that are not in 179218fd37a7SXin LIthe newer directory, you can make the patch smaller by using the 179318fd37a7SXin LI@option{--unidirectional-new-file} option instead of @option{-N}. 179418fd37a7SXin LIThis option is like @option{-N} except that it only inserts the contents 179518fd37a7SXin LIof files that appear in the second directory but not the first (that is, 179618fd37a7SXin LIfiles that were added). At the top of the patch, write instructions for 179718fd37a7SXin LIthe user applying the patch to remove the files that were deleted before 179818fd37a7SXin LIapplying the patch. @xref{Making Patches}, for more discussion of 179918fd37a7SXin LImaking patches for distribution. 180018fd37a7SXin LI 180118fd37a7SXin LITo ignore some files while comparing directories, use the @option{-x 180218fd37a7SXin LI@var{pattern}} or @option{--exclude=@var{pattern}} option. This option 180318fd37a7SXin LIignores any files or subdirectories whose base names match the shell 180418fd37a7SXin LIpattern @var{pattern}. Unlike in the shell, a period at the start of 180518fd37a7SXin LIthe base of a file name matches a wildcard at the start of a pattern. 180618fd37a7SXin LIYou should enclose @var{pattern} in quotes so that the shell does not 180718fd37a7SXin LIexpand it. For example, the option @option{-x '*.[ao]'} ignores any file 180818fd37a7SXin LIwhose name ends with @samp{.a} or @samp{.o}. 180918fd37a7SXin LI 181018fd37a7SXin LIThis option accumulates if you specify it more than once. For example, 181118fd37a7SXin LIusing the options @option{-x 'RCS' -x '*,v'} ignores any file or 181218fd37a7SXin LIsubdirectory whose base name is @samp{RCS} or ends with @samp{,v}. 181318fd37a7SXin LI 181418fd37a7SXin LIIf you need to give this option many times, you can instead put the 181518fd37a7SXin LIpatterns in a file, one pattern per line, and use the @option{-X 181618fd37a7SXin LI@var{file}} or @option{--exclude-from=@var{file}} option. Trailing 181718fd37a7SXin LIwhite space and empty lines are ignored in the pattern file. 181818fd37a7SXin LI 181918fd37a7SXin LIIf you have been comparing two directories and stopped partway through, 182018fd37a7SXin LIlater you might want to continue where you left off. You can do this by 182118fd37a7SXin LIusing the @option{-S @var{file}} or @option{--starting-file=@var{file}} 182218fd37a7SXin LIoption. This compares only the file @var{file} and all alphabetically 182318fd37a7SXin LIlater files in the topmost directory level. 182418fd37a7SXin LI 182518fd37a7SXin LIIf two directories differ only in that file names are lower case in 182618fd37a7SXin LIone directory and upper case in the upper, @command{diff} normally 182718fd37a7SXin LIreports many differences because it compares file names in a 182818fd37a7SXin LIcase sensitive way. With the @option{--ignore-file-name-case} option, 182918fd37a7SXin LI@command{diff} ignores case differences in file names, so that for example 183018fd37a7SXin LIthe contents of the file @file{Tao} in one directory are compared to 183118fd37a7SXin LIthe contents of the file @file{TAO} in the other. The 183218fd37a7SXin LI@option{--no-ignore-file-name-case} option cancels the effect of the 183318fd37a7SXin LI@option{--ignore-file-name-case} option, reverting to the default 183418fd37a7SXin LIbehavior. 183518fd37a7SXin LI 183618fd37a7SXin LIIf an @option{-x @var{pattern}} or @option{--exclude=@var{pattern}} 183718fd37a7SXin LIoption, or an @option{-X @var{file}} or 183818fd37a7SXin LI@option{--exclude-from=@var{file}} option, 183918fd37a7SXin LIis specified while the @option{--ignore-file-name-case} option is in 184018fd37a7SXin LIeffect, case is ignored when excluding file names matching the 184118fd37a7SXin LIspecified patterns. 184218fd37a7SXin LI 184318fd37a7SXin LI@node Adjusting Output 184418fd37a7SXin LI@chapter Making @command{diff} Output Prettier 184518fd37a7SXin LI 184618fd37a7SXin LI@command{diff} provides several ways to adjust the appearance of its output. 184718fd37a7SXin LIThese adjustments can be applied to any output format. 184818fd37a7SXin LI 184918fd37a7SXin LI@menu 185018fd37a7SXin LI* Tabs:: Preserving the alignment of tab stops. 185118fd37a7SXin LI* Pagination:: Page numbering and time-stamping @command{diff} output. 185218fd37a7SXin LI@end menu 185318fd37a7SXin LI 185418fd37a7SXin LI@node Tabs 185518fd37a7SXin LI@section Preserving Tab Stop Alignment 185618fd37a7SXin LI@cindex tab stop alignment 185718fd37a7SXin LI@cindex aligning tab stops 185818fd37a7SXin LI 185918fd37a7SXin LIThe lines of text in some of the @command{diff} output formats are 186018fd37a7SXin LIpreceded by one or two characters that indicate whether the text is 186118fd37a7SXin LIinserted, deleted, or changed. The addition of those characters can 186218fd37a7SXin LIcause tabs to move to the next tab stop, throwing off the alignment of 186318fd37a7SXin LIcolumns in the line. @acronym{GNU} @command{diff} provides two ways 186418fd37a7SXin LIto make tab-aligned columns line up correctly. 186518fd37a7SXin LI 186618fd37a7SXin LIThe first way is to have @command{diff} convert all tabs into the correct 186718fd37a7SXin LInumber of spaces before outputting them; select this method with the 186818fd37a7SXin LI@option{-t} or @option{--expand-tabs} option. To use this form of output with 186918fd37a7SXin LI@command{patch}, you must give @command{patch} the @option{-l} or 187018fd37a7SXin LI@option{--ignore-white-space} option (@pxref{Changed White Space}, for more 187118fd37a7SXin LIinformation). @command{diff} normally assumes that tab stops are set 187218fd37a7SXin LIevery 8 print columns, but this can be altered by the 187318fd37a7SXin LI@option{--tabsize=@var{columns}} option. 187418fd37a7SXin LI 187518fd37a7SXin LIThe other method for making tabs line up correctly is to add a tab 187618fd37a7SXin LIcharacter instead of a space after the indicator character at the 187718fd37a7SXin LIbeginning of the line. This ensures that all following tab characters 187818fd37a7SXin LIare in the same position relative to tab stops that they were in the 187918fd37a7SXin LIoriginal files, so that the output is aligned correctly. Its 188018fd37a7SXin LIdisadvantage is that it can make long lines too long to fit on one line 188118fd37a7SXin LIof the screen or the paper. It also does not work with the unified 188218fd37a7SXin LIoutput format, which does not have a space character after the change 188318fd37a7SXin LItype indicator character. Select this method with the @option{-T} or 188418fd37a7SXin LI@option{--initial-tab} option. 188518fd37a7SXin LI 188618fd37a7SXin LI@node Pagination 188718fd37a7SXin LI@section Paginating @command{diff} Output 188818fd37a7SXin LI@cindex paginating @command{diff} output 188918fd37a7SXin LI 189018fd37a7SXin LIIt can be convenient to have long output page-numbered and time-stamped. 189118fd37a7SXin LIThe @option{-l} or @option{--paginate} option does this by sending the 189218fd37a7SXin LI@command{diff} output through the @command{pr} program. Here is what the page 189318fd37a7SXin LIheader might look like for @samp{diff -lc lao tzu}: 189418fd37a7SXin LI 189518fd37a7SXin LI@example 189618fd37a7SXin LI2002-02-22 14:20 diff -lc lao tzu Page 1 189718fd37a7SXin LI@end example 189818fd37a7SXin LI 189918fd37a7SXin LI@node diff Performance 190018fd37a7SXin LI@chapter @command{diff} Performance Tradeoffs 190118fd37a7SXin LI@cindex performance of @command{diff} 190218fd37a7SXin LI 190318fd37a7SXin LI@acronym{GNU} @command{diff} runs quite efficiently; however, in some 190418fd37a7SXin LIcircumstances you can cause it to run faster or produce a more compact 190518fd37a7SXin LIset of changes. 190618fd37a7SXin LI 190718fd37a7SXin LIOne way to improve @command{diff} performance is to use hard or 190818fd37a7SXin LIsymbolic links to files instead of copies. This improves performance 190918fd37a7SXin LIbecause @command{diff} normally does not need to read two hard or 191018fd37a7SXin LIsymbolic links to the same file, since their contents must be 191118fd37a7SXin LIidentical. For example, suppose you copy a large directory hierarchy, 191218fd37a7SXin LImake a few changes to the copy, and then often use @samp{diff -r} to 191318fd37a7SXin LIcompare the original to the copy. If the original files are 191418fd37a7SXin LIread-only, you can greatly improve performance by creating the copy 191518fd37a7SXin LIusing hard or symbolic links (e.g., with @acronym{GNU} @samp{cp -lR} or 191618fd37a7SXin LI@samp{cp -sR}). Before editing a file in the copy for the first time, 191718fd37a7SXin LIyou should break the link and replace it with a regular copy. 191818fd37a7SXin LI 191918fd37a7SXin LIYou can also affect the performance of @acronym{GNU} @command{diff} by 192018fd37a7SXin LIgiving it options that change the way it compares files. 192118fd37a7SXin LIPerformance has more than one dimension. These options improve one 192218fd37a7SXin LIaspect of performance at the cost of another, or they improve 192318fd37a7SXin LIperformance in some cases while hurting it in others. 192418fd37a7SXin LI 192518fd37a7SXin LIThe way that @acronym{GNU} @command{diff} determines which lines have 192618fd37a7SXin LIchanged always comes up with a near-minimal set of differences. 192718fd37a7SXin LIUsually it is good enough for practical purposes. If the 192818fd37a7SXin LI@command{diff} output is large, you might want @command{diff} to use a 192918fd37a7SXin LImodified algorithm that sometimes produces a smaller set of 193018fd37a7SXin LIdifferences. The @option{-d} or @option{--minimal} option does this; 193118fd37a7SXin LIhowever, it can also cause @command{diff} to run more slowly than 193218fd37a7SXin LIusual, so it is not the default behavior. 193318fd37a7SXin LI 193418fd37a7SXin LIWhen the files you are comparing are large and have small groups of 193518fd37a7SXin LIchanges scattered throughout them, you can use the 193618fd37a7SXin LI@option{--speed-large-files} option to make a different modification to 193718fd37a7SXin LIthe algorithm that @command{diff} uses. If the input files have a constant 193818fd37a7SXin LIsmall density of changes, this option speeds up the comparisons without 193918fd37a7SXin LIchanging the output. If not, @command{diff} might produce a larger set of 194018fd37a7SXin LIdifferences; however, the output will still be correct. 194118fd37a7SXin LI 194218fd37a7SXin LINormally @command{diff} discards the prefix and suffix that is common to 194318fd37a7SXin LIboth files before it attempts to find a minimal set of differences. 194418fd37a7SXin LIThis makes @command{diff} run faster, but occasionally it may produce 194518fd37a7SXin LInon-minimal output. The @option{--horizon-lines=@var{lines}} option 194618fd37a7SXin LIprevents @command{diff} from discarding the last @var{lines} lines of the 194718fd37a7SXin LIprefix and the first @var{lines} lines of the suffix. This gives 194818fd37a7SXin LI@command{diff} further opportunities to find a minimal output. 194918fd37a7SXin LI 195018fd37a7SXin LISuppose a run of changed lines includes a sequence of lines at one end 195118fd37a7SXin LIand there is an identical sequence of lines just outside the other end. 195218fd37a7SXin LIThe @command{diff} command is free to choose which identical sequence is 195318fd37a7SXin LIincluded in the hunk. In this case, @command{diff} normally shifts the 195418fd37a7SXin LIhunk's boundaries when this merges adjacent hunks, or shifts a hunk's 195518fd37a7SXin LIlines towards the end of the file. Merging hunks can make the output 195618fd37a7SXin LIlook nicer in some cases. 195718fd37a7SXin LI 195818fd37a7SXin LI@node Comparing Three Files 195918fd37a7SXin LI@chapter Comparing Three Files 196018fd37a7SXin LI@cindex comparing three files 196118fd37a7SXin LI@cindex format of @command{diff3} output 196218fd37a7SXin LI 196318fd37a7SXin LIUse the program @command{diff3} to compare three files and show any 196418fd37a7SXin LIdifferences among them. (@command{diff3} can also merge files; see 196518fd37a7SXin LI@ref{diff3 Merging}). 196618fd37a7SXin LI 196718fd37a7SXin LIThe ``normal'' @command{diff3} output format shows each hunk of 196818fd37a7SXin LIdifferences without surrounding context. Hunks are labeled depending 196918fd37a7SXin LIon whether they are two-way or three-way, and lines are annotated by 197018fd37a7SXin LItheir location in the input files. 197118fd37a7SXin LI 197218fd37a7SXin LI@xref{Invoking diff3}, for more information on how to run @command{diff3}. 197318fd37a7SXin LI 197418fd37a7SXin LI@menu 197518fd37a7SXin LI* Sample diff3 Input:: Sample @command{diff3} input for examples. 197618fd37a7SXin LI* Example diff3 Normal:: Sample output in the normal format. 197718fd37a7SXin LI* diff3 Hunks:: The format of normal output format. 197818fd37a7SXin LI* Detailed diff3 Normal:: A detailed description of normal output format. 197918fd37a7SXin LI@end menu 198018fd37a7SXin LI 198118fd37a7SXin LI@node Sample diff3 Input 198218fd37a7SXin LI@section A Third Sample Input File 198318fd37a7SXin LI@cindex @command{diff3} sample input 198418fd37a7SXin LI@cindex sample input for @command{diff3} 198518fd37a7SXin LI 198618fd37a7SXin LIHere is a third sample file that will be used in examples to illustrate 198718fd37a7SXin LIthe output of @command{diff3} and how various options can change it. The 198818fd37a7SXin LIfirst two files are the same that we used for @command{diff} (@pxref{Sample 198918fd37a7SXin LIdiff Input}). This is the third sample file, called @file{tao}: 199018fd37a7SXin LI 199118fd37a7SXin LI@example 199218fd37a7SXin LIThe Way that can be told of is not the eternal Way; 199318fd37a7SXin LIThe name that can be named is not the eternal name. 199418fd37a7SXin LIThe Nameless is the origin of Heaven and Earth; 199518fd37a7SXin LIThe named is the mother of all things. 199618fd37a7SXin LI 199718fd37a7SXin LITherefore let there always be non-being, 199818fd37a7SXin LI so we may see their subtlety, 199918fd37a7SXin LIAnd let there always be being, 200018fd37a7SXin LI so we may see their result. 200118fd37a7SXin LIThe two are the same, 200218fd37a7SXin LIBut after they are produced, 200318fd37a7SXin LI they have different names. 200418fd37a7SXin LI 200518fd37a7SXin LI -- The Way of Lao-Tzu, tr. Wing-tsit Chan 200618fd37a7SXin LI@end example 200718fd37a7SXin LI 200818fd37a7SXin LI@node Example diff3 Normal 200918fd37a7SXin LI@section An Example of @command{diff3} Normal Format 201018fd37a7SXin LI 201118fd37a7SXin LIHere is the output of the command @samp{diff3 lao tzu tao} 201218fd37a7SXin LI(@pxref{Sample diff3 Input}, for the complete contents of the files). 201318fd37a7SXin LINotice that it shows only the lines that are different among the three 201418fd37a7SXin LIfiles. 201518fd37a7SXin LI 201618fd37a7SXin LI@example 201718fd37a7SXin LI====2 201818fd37a7SXin LI1:1,2c 201918fd37a7SXin LI3:1,2c 202018fd37a7SXin LI The Way that can be told of is not the eternal Way; 202118fd37a7SXin LI The name that can be named is not the eternal name. 202218fd37a7SXin LI2:0a 202318fd37a7SXin LI====1 202418fd37a7SXin LI1:4c 202518fd37a7SXin LI The Named is the mother of all things. 202618fd37a7SXin LI2:2,3c 202718fd37a7SXin LI3:4,5c 202818fd37a7SXin LI The named is the mother of all things. 202918fd37a7SXin LI 203018fd37a7SXin LI====3 203118fd37a7SXin LI1:8c 203218fd37a7SXin LI2:7c 203318fd37a7SXin LI so we may see their outcome. 203418fd37a7SXin LI3:9c 203518fd37a7SXin LI so we may see their result. 203618fd37a7SXin LI==== 203718fd37a7SXin LI1:11a 203818fd37a7SXin LI2:11,13c 203918fd37a7SXin LI They both may be called deep and profound. 204018fd37a7SXin LI Deeper and more profound, 204118fd37a7SXin LI The door of all subtleties! 204218fd37a7SXin LI3:13,14c 204318fd37a7SXin LI 204418fd37a7SXin LI -- The Way of Lao-Tzu, tr. Wing-tsit Chan 204518fd37a7SXin LI@end example 204618fd37a7SXin LI 204718fd37a7SXin LI@node Detailed diff3 Normal 204818fd37a7SXin LI@section Detailed Description of @command{diff3} Normal Format 204918fd37a7SXin LI 205018fd37a7SXin LIEach hunk begins with a line marked @samp{====}. Three-way hunks have 205118fd37a7SXin LIplain @samp{====} lines, and two-way hunks have @samp{1}, @samp{2}, or 205218fd37a7SXin LI@samp{3} appended to specify which of the three input files differ in 205318fd37a7SXin LIthat hunk. The hunks contain copies of two or three sets of input 205418fd37a7SXin LIlines each preceded by one or two commands identifying where the lines 205518fd37a7SXin LIcame from. 205618fd37a7SXin LI 205718fd37a7SXin LINormally, two spaces precede each copy of an input line to distinguish 205818fd37a7SXin LIit from the commands. But with the @option{-T} or @option{--initial-tab} 205918fd37a7SXin LIoption, @command{diff3} uses a tab instead of two spaces; this lines up 206018fd37a7SXin LItabs correctly. @xref{Tabs}, for more information. 206118fd37a7SXin LI 206218fd37a7SXin LICommands take the following forms: 206318fd37a7SXin LI 206418fd37a7SXin LI@table @samp 206518fd37a7SXin LI@item @var{file}:@var{l}a 206618fd37a7SXin LIThis hunk appears after line @var{l} of file @var{file}, and 206718fd37a7SXin LIcontains no lines in that file. To edit this file to yield the other 206818fd37a7SXin LIfiles, one must append hunk lines taken from the other files. For 206918fd37a7SXin LIexample, @samp{1:11a} means that the hunk follows line 11 in the first 207018fd37a7SXin LIfile and contains no lines from that file. 207118fd37a7SXin LI 207218fd37a7SXin LI@item @var{file}:@var{r}c 207318fd37a7SXin LIThis hunk contains the lines in the range @var{r} of file @var{file}. 207418fd37a7SXin LIThe range @var{r} is a comma-separated pair of line numbers, or just one 207518fd37a7SXin LInumber if the range is a singleton. To edit this file to yield the 207618fd37a7SXin LIother files, one must change the specified lines to be the lines taken 207718fd37a7SXin LIfrom the other files. For example, @samp{2:11,13c} means that the hunk 207818fd37a7SXin LIcontains lines 11 through 13 from the second file. 207918fd37a7SXin LI@end table 208018fd37a7SXin LI 208118fd37a7SXin LIIf the last line in a set of input lines is incomplete 208218fd37a7SXin LI(@pxref{Incomplete Lines}), it is distinguished on output from a full 208318fd37a7SXin LIline by a following line that starts with @samp{\}. 208418fd37a7SXin LI 208518fd37a7SXin LI@node diff3 Hunks 208618fd37a7SXin LI@section @command{diff3} Hunks 208718fd37a7SXin LI@cindex hunks for @command{diff3} 208818fd37a7SXin LI@cindex @command{diff3} hunks 208918fd37a7SXin LI 209018fd37a7SXin LIGroups of lines that differ in two or three of the input files are 209118fd37a7SXin LIcalled @dfn{diff3 hunks}, by analogy with @command{diff} hunks 209218fd37a7SXin LI(@pxref{Hunks}). If all three input files differ in a @command{diff3} 209318fd37a7SXin LIhunk, the hunk is called a @dfn{three-way hunk}; if just two input files 209418fd37a7SXin LIdiffer, it is a @dfn{two-way hunk}. 209518fd37a7SXin LI 209618fd37a7SXin LIAs with @command{diff}, several solutions are possible. When comparing the 209718fd37a7SXin LIfiles @samp{A}, @samp{B}, and @samp{C}, @command{diff3} normally finds 209818fd37a7SXin LI@command{diff3} hunks by merging the two-way hunks output by the two 209918fd37a7SXin LIcommands @samp{diff A B} and @samp{diff A C}. This does not necessarily 210018fd37a7SXin LIminimize the size of the output, but exceptions should be rare. 210118fd37a7SXin LI 210218fd37a7SXin LIFor example, suppose @file{F} contains the three lines @samp{a}, 210318fd37a7SXin LI@samp{b}, @samp{f}, @file{G} contains the lines @samp{g}, @samp{b}, 210418fd37a7SXin LI@samp{g}, and @file{H} contains the lines @samp{a}, @samp{b}, 210518fd37a7SXin LI@samp{h}. @samp{diff3 F G H} might output the following: 210618fd37a7SXin LI 210718fd37a7SXin LI@example 210818fd37a7SXin LI====2 210918fd37a7SXin LI1:1c 211018fd37a7SXin LI3:1c 211118fd37a7SXin LI a 211218fd37a7SXin LI2:1c 211318fd37a7SXin LI g 211418fd37a7SXin LI==== 211518fd37a7SXin LI1:3c 211618fd37a7SXin LI f 211718fd37a7SXin LI2:3c 211818fd37a7SXin LI g 211918fd37a7SXin LI3:3c 212018fd37a7SXin LI h 212118fd37a7SXin LI@end example 212218fd37a7SXin LI 212318fd37a7SXin LI@noindent 212418fd37a7SXin LIbecause it found a two-way hunk containing @samp{a} in the first and 212518fd37a7SXin LIthird files and @samp{g} in the second file, then the single line 212618fd37a7SXin LI@samp{b} common to all three files, then a three-way hunk containing 212718fd37a7SXin LIthe last line of each file. 212818fd37a7SXin LI 212918fd37a7SXin LI@node diff3 Merging 213018fd37a7SXin LI@chapter Merging From a Common Ancestor 213118fd37a7SXin LI@cindex merging from a common ancestor 213218fd37a7SXin LI 213318fd37a7SXin LIWhen two people have made changes to copies of the same file, 213418fd37a7SXin LI@command{diff3} can produce a merged output that contains both sets of 213518fd37a7SXin LIchanges together with warnings about conflicts. 213618fd37a7SXin LI 213718fd37a7SXin LIOne might imagine programs with names like @command{diff4} and @command{diff5} 213818fd37a7SXin LIto compare more than three files simultaneously, but in practice the 213918fd37a7SXin LIneed rarely arises. You can use @command{diff3} to merge three or more 214018fd37a7SXin LIsets of changes to a file by merging two change sets at a time. 214118fd37a7SXin LI 214218fd37a7SXin LI@command{diff3} can incorporate changes from two modified versions into a 214318fd37a7SXin LIcommon preceding version. This lets you merge the sets of changes 214418fd37a7SXin LIrepresented by the two newer files. Specify the common ancestor version 214518fd37a7SXin LIas the second argument and the two newer versions as the first and third 214618fd37a7SXin LIarguments, like this: 214718fd37a7SXin LI 214818fd37a7SXin LI@example 214918fd37a7SXin LIdiff3 @var{mine} @var{older} @var{yours} 215018fd37a7SXin LI@end example 215118fd37a7SXin LI 215218fd37a7SXin LI@noindent 215318fd37a7SXin LIYou can remember the order of the arguments by noting that they are in 215418fd37a7SXin LIalphabetical order. 215518fd37a7SXin LI 215618fd37a7SXin LI@cindex conflict 215718fd37a7SXin LI@cindex overlap 215818fd37a7SXin LIYou can think of this as subtracting @var{older} from @var{yours} and 215918fd37a7SXin LIadding the result to @var{mine}, or as merging into @var{mine} the 216018fd37a7SXin LIchanges that would turn @var{older} into @var{yours}. This merging is 216118fd37a7SXin LIwell-defined as long as @var{mine} and @var{older} match in the 216218fd37a7SXin LIneighborhood of each such change. This fails to be true when all three 216318fd37a7SXin LIinput files differ or when only @var{older} differs; we call this 216418fd37a7SXin LIa @dfn{conflict}. When all three input files differ, we call the 216518fd37a7SXin LIconflict an @dfn{overlap}. 216618fd37a7SXin LI 216718fd37a7SXin LI@command{diff3} gives you several ways to handle overlaps and conflicts. 216818fd37a7SXin LIYou can omit overlaps or conflicts, or select only overlaps, 216918fd37a7SXin LIor mark conflicts with special @samp{<<<<<<<} and @samp{>>>>>>>} lines. 217018fd37a7SXin LI 217118fd37a7SXin LI@command{diff3} can output the merge results as an @command{ed} script that 217218fd37a7SXin LIthat can be applied to the first file to yield the merged output. 217318fd37a7SXin LIHowever, it is usually better to have @command{diff3} generate the merged 217418fd37a7SXin LIoutput directly; this bypasses some problems with @command{ed}. 217518fd37a7SXin LI 217618fd37a7SXin LI@menu 217718fd37a7SXin LI* Which Changes:: Selecting changes to incorporate. 217818fd37a7SXin LI* Marking Conflicts:: Marking conflicts. 217918fd37a7SXin LI* Bypassing ed:: Generating merged output directly. 218018fd37a7SXin LI* Merging Incomplete Lines:: How @command{diff3} merges incomplete lines. 218118fd37a7SXin LI* Saving the Changed File:: Emulating System V behavior. 218218fd37a7SXin LI@end menu 218318fd37a7SXin LI 218418fd37a7SXin LI@node Which Changes 218518fd37a7SXin LI@section Selecting Which Changes to Incorporate 218618fd37a7SXin LI@cindex overlapping change, selection of 218718fd37a7SXin LI@cindex unmerged change 218818fd37a7SXin LI 218918fd37a7SXin LIYou can select all unmerged changes from @var{older} to @var{yours} for merging 219018fd37a7SXin LIinto @var{mine} with the @option{-e} or @option{--ed} option. You can 219118fd37a7SXin LIselect only the nonoverlapping unmerged changes with @option{-3} or 219218fd37a7SXin LI@option{--easy-only}, and you can select only the overlapping changes with 219318fd37a7SXin LI@option{-x} or @option{--overlap-only}. 219418fd37a7SXin LI 219518fd37a7SXin LIThe @option{-e}, @option{-3} and @option{-x} options select only 219618fd37a7SXin LI@dfn{unmerged changes}, i.e.@: changes where @var{mine} and @var{yours} 219718fd37a7SXin LIdiffer; they ignore changes from @var{older} to @var{yours} where 219818fd37a7SXin LI@var{mine} and @var{yours} are identical, because they assume that such 219918fd37a7SXin LIchanges have already been merged. If this assumption is not a safe 220018fd37a7SXin LIone, you can use the @option{-A} or @option{--show-all} option 220118fd37a7SXin LI(@pxref{Marking Conflicts}). 220218fd37a7SXin LI 220318fd37a7SXin LIHere is the output of the command @command{diff3} with each of these three 220418fd37a7SXin LIoptions (@pxref{Sample diff3 Input}, for the complete contents of the files). 220518fd37a7SXin LINotice that @option{-e} outputs the union of the disjoint sets of changes 220618fd37a7SXin LIoutput by @option{-3} and @option{-x}. 220718fd37a7SXin LI 220818fd37a7SXin LIOutput of @samp{diff3 -e lao tzu tao}: 220918fd37a7SXin LI@example 221018fd37a7SXin LI11a 221118fd37a7SXin LI 221218fd37a7SXin LI -- The Way of Lao-Tzu, tr. Wing-tsit Chan 221318fd37a7SXin LI. 221418fd37a7SXin LI8c 221518fd37a7SXin LI so we may see their result. 221618fd37a7SXin LI. 221718fd37a7SXin LI@end example 221818fd37a7SXin LI 221918fd37a7SXin LIOutput of @samp{diff3 -3 lao tzu tao}: 222018fd37a7SXin LI@example 222118fd37a7SXin LI8c 222218fd37a7SXin LI so we may see their result. 222318fd37a7SXin LI. 222418fd37a7SXin LI@end example 222518fd37a7SXin LI 222618fd37a7SXin LIOutput of @samp{diff3 -x lao tzu tao}: 222718fd37a7SXin LI@example 222818fd37a7SXin LI11a 222918fd37a7SXin LI 223018fd37a7SXin LI -- The Way of Lao-Tzu, tr. Wing-tsit Chan 223118fd37a7SXin LI. 223218fd37a7SXin LI@end example 223318fd37a7SXin LI 223418fd37a7SXin LI@node Marking Conflicts 223518fd37a7SXin LI@section Marking Conflicts 223618fd37a7SXin LI@cindex conflict marking 223718fd37a7SXin LI@cindex @samp{<<<<<<<} for marking conflicts 223818fd37a7SXin LI 223918fd37a7SXin LI@command{diff3} can mark conflicts in the merged output by 224018fd37a7SXin LIbracketing them with special marker lines. A conflict 224118fd37a7SXin LIthat comes from two files @var{A} and @var{B} is marked as follows: 224218fd37a7SXin LI 224318fd37a7SXin LI@example 224418fd37a7SXin LI<<<<<<< @var{A} 224518fd37a7SXin LI@r{lines from @var{A}} 224618fd37a7SXin LI======= 224718fd37a7SXin LI@r{lines from @var{B}} 224818fd37a7SXin LI>>>>>>> @var{B} 224918fd37a7SXin LI@end example 225018fd37a7SXin LI 225118fd37a7SXin LIA conflict that comes from three files @var{A}, @var{B} and @var{C} is 225218fd37a7SXin LImarked as follows: 225318fd37a7SXin LI 225418fd37a7SXin LI@example 225518fd37a7SXin LI<<<<<<< @var{A} 225618fd37a7SXin LI@r{lines from @var{A}} 225718fd37a7SXin LI||||||| @var{B} 225818fd37a7SXin LI@r{lines from @var{B}} 225918fd37a7SXin LI======= 226018fd37a7SXin LI@r{lines from @var{C}} 226118fd37a7SXin LI>>>>>>> @var{C} 226218fd37a7SXin LI@end example 226318fd37a7SXin LI 226418fd37a7SXin LIThe @option{-A} or @option{--show-all} option acts like the @option{-e} 226518fd37a7SXin LIoption, except that it brackets conflicts, and it outputs all changes 226618fd37a7SXin LIfrom @var{older} to @var{yours}, not just the unmerged changes. Thus, 226718fd37a7SXin LIgiven the sample input files (@pxref{Sample diff3 Input}), @samp{diff3 226818fd37a7SXin LI-A lao tzu tao} puts brackets around the conflict where only @file{tzu} 226918fd37a7SXin LIdiffers: 227018fd37a7SXin LI 227118fd37a7SXin LI@example 227218fd37a7SXin LI<<<<<<< tzu 227318fd37a7SXin LI======= 227418fd37a7SXin LIThe Way that can be told of is not the eternal Way; 227518fd37a7SXin LIThe name that can be named is not the eternal name. 227618fd37a7SXin LI>>>>>>> tao 227718fd37a7SXin LI@end example 227818fd37a7SXin LI 227918fd37a7SXin LIAnd it outputs the three-way conflict as follows: 228018fd37a7SXin LI 228118fd37a7SXin LI@example 228218fd37a7SXin LI<<<<<<< lao 228318fd37a7SXin LI||||||| tzu 228418fd37a7SXin LIThey both may be called deep and profound. 228518fd37a7SXin LIDeeper and more profound, 228618fd37a7SXin LIThe door of all subtleties! 228718fd37a7SXin LI======= 228818fd37a7SXin LI 228918fd37a7SXin LI -- The Way of Lao-Tzu, tr. Wing-tsit Chan 229018fd37a7SXin LI>>>>>>> tao 229118fd37a7SXin LI@end example 229218fd37a7SXin LI 229318fd37a7SXin LIThe @option{-E} or @option{--show-overlap} option outputs less information 229418fd37a7SXin LIthan the @option{-A} or @option{--show-all} option, because it outputs only 229518fd37a7SXin LIunmerged changes, and it never outputs the contents of the second 229618fd37a7SXin LIfile. Thus the @option{-E} option acts like the @option{-e} option, 229718fd37a7SXin LIexcept that it brackets the first and third files from three-way 229818fd37a7SXin LIoverlapping changes. Similarly, @option{-X} acts like @option{-x}, except 229918fd37a7SXin LIit brackets all its (necessarily overlapping) changes. For example, 230018fd37a7SXin LIfor the three-way overlapping change above, the @option{-E} and @option{-X} 230118fd37a7SXin LIoptions output the following: 230218fd37a7SXin LI 230318fd37a7SXin LI@example 230418fd37a7SXin LI<<<<<<< lao 230518fd37a7SXin LI======= 230618fd37a7SXin LI 230718fd37a7SXin LI -- The Way of Lao-Tzu, tr. Wing-tsit Chan 230818fd37a7SXin LI>>>>>>> tao 230918fd37a7SXin LI@end example 231018fd37a7SXin LI 231118fd37a7SXin LIIf you are comparing files that have meaningless or uninformative names, 231218fd37a7SXin LIyou can use the @option{--label=@var{label}} 231318fd37a7SXin LIoption to show alternate names in the @samp{<<<<<<<}, @samp{|||||||} 231418fd37a7SXin LIand @samp{>>>>>>>} brackets. This option can be given up to three 231518fd37a7SXin LItimes, once for each input file. Thus @samp{diff3 -A --label X 231618fd37a7SXin LI--label Y --label Z A 231718fd37a7SXin LIB C} acts like @samp{diff3 -A A B C}, except that the output looks like 231818fd37a7SXin LIit came from files named @samp{X}, @samp{Y} and @samp{Z} rather than 231918fd37a7SXin LIfrom files named @samp{A}, @samp{B} and @samp{C}. 232018fd37a7SXin LI 232118fd37a7SXin LI@node Bypassing ed 232218fd37a7SXin LI@section Generating the Merged Output Directly 232318fd37a7SXin LI@cindex merged @command{diff3} format 232418fd37a7SXin LI 232518fd37a7SXin LIWith the @option{-m} or @option{--merge} option, @command{diff3} outputs the 232618fd37a7SXin LImerged file directly. This is more efficient than using @command{ed} to 232718fd37a7SXin LIgenerate it, and works even with non-text files that @command{ed} would 232818fd37a7SXin LIreject. If you specify @option{-m} without an @command{ed} script option, 232918fd37a7SXin LI@option{-A} is assumed. 233018fd37a7SXin LI 233118fd37a7SXin LIFor example, the command @samp{diff3 -m lao tzu tao} 233218fd37a7SXin LI(@pxref{Sample diff3 Input} for a copy of the input files) would output 233318fd37a7SXin LIthe following: 233418fd37a7SXin LI 233518fd37a7SXin LI@example 233618fd37a7SXin LI<<<<<<< tzu 233718fd37a7SXin LI======= 233818fd37a7SXin LIThe Way that can be told of is not the eternal Way; 233918fd37a7SXin LIThe name that can be named is not the eternal name. 234018fd37a7SXin LI>>>>>>> tao 234118fd37a7SXin LIThe Nameless is the origin of Heaven and Earth; 234218fd37a7SXin LIThe Named is the mother of all things. 234318fd37a7SXin LITherefore let there always be non-being, 234418fd37a7SXin LI so we may see their subtlety, 234518fd37a7SXin LIAnd let there always be being, 234618fd37a7SXin LI so we may see their result. 234718fd37a7SXin LIThe two are the same, 234818fd37a7SXin LIBut after they are produced, 234918fd37a7SXin LI they have different names. 235018fd37a7SXin LI<<<<<<< lao 235118fd37a7SXin LI||||||| tzu 235218fd37a7SXin LIThey both may be called deep and profound. 235318fd37a7SXin LIDeeper and more profound, 235418fd37a7SXin LIThe door of all subtleties! 235518fd37a7SXin LI======= 235618fd37a7SXin LI 235718fd37a7SXin LI -- The Way of Lao-Tzu, tr. Wing-tsit Chan 235818fd37a7SXin LI>>>>>>> tao 235918fd37a7SXin LI@end example 236018fd37a7SXin LI 236118fd37a7SXin LI@node Merging Incomplete Lines 236218fd37a7SXin LI@section How @command{diff3} Merges Incomplete Lines 236318fd37a7SXin LI@cindex incomplete line merging 236418fd37a7SXin LI 236518fd37a7SXin LIWith @option{-m}, incomplete lines (@pxref{Incomplete Lines}) are simply 236618fd37a7SXin LIcopied to the output as they are found; if the merged output ends in an 236718fd37a7SXin LIconflict and one of the input files ends in an incomplete 236818fd37a7SXin LIline, succeeding @samp{|||||||}, @samp{=======} or @samp{>>>>>>>} 236918fd37a7SXin LIbrackets appear somewhere other than the start of a line because 237018fd37a7SXin LIthey are appended to the incomplete line. 237118fd37a7SXin LI 237218fd37a7SXin LIWithout @option{-m}, if an @command{ed} script option is specified and an 237318fd37a7SXin LIincomplete line is found, @command{diff3} generates a warning and acts as 237418fd37a7SXin LIif a newline had been present. 237518fd37a7SXin LI 237618fd37a7SXin LI@node Saving the Changed File 237718fd37a7SXin LI@section Saving the Changed File 237818fd37a7SXin LI@cindex System V @command{diff3} compatibility 237918fd37a7SXin LI 238018fd37a7SXin LITraditional Unix @command{diff3} generates an @command{ed} script without the 238118fd37a7SXin LItrailing @samp{w} and @samp{q} commands that save the changes. 238218fd37a7SXin LISystem V @command{diff3} generates these extra commands. @acronym{GNU} 238318fd37a7SXin LI@command{diff3} normally behaves like traditional Unix 238418fd37a7SXin LI@command{diff3}, but with the @option{-i} option it behaves like 238518fd37a7SXin LISystem V @command{diff3} and appends the @samp{w} and @samp{q} 238618fd37a7SXin LIcommands. 238718fd37a7SXin LI 238818fd37a7SXin LIThe @option{-i} option requires one of the @command{ed} script options 238918fd37a7SXin LI@option{-AeExX3}, and is incompatible with the merged output option 239018fd37a7SXin LI@option{-m}. 239118fd37a7SXin LI 239218fd37a7SXin LI@node Interactive Merging 239318fd37a7SXin LI@chapter Interactive Merging with @command{sdiff} 239418fd37a7SXin LI@cindex diff merging 239518fd37a7SXin LI@cindex interactive merging 239618fd37a7SXin LI 239718fd37a7SXin LIWith @command{sdiff}, you can merge two files interactively based on a 239818fd37a7SXin LIside-by-side @option{-y} format comparison (@pxref{Side by Side}). Use 239918fd37a7SXin LI@option{-o @var{file}} or @option{--output=@var{file}} to specify where to 240018fd37a7SXin LIput the merged text. @xref{Invoking sdiff}, for more details on the 240118fd37a7SXin LIoptions to @command{sdiff}. 240218fd37a7SXin LI 240318fd37a7SXin LIAnother way to merge files interactively is to use the Emacs Lisp 240418fd37a7SXin LIpackage @command{emerge}. @xref{emerge, , emerge, emacs, The 240518fd37a7SXin LI@acronym{GNU} Emacs Manual}, for more information. 240618fd37a7SXin LI 240718fd37a7SXin LI@menu 240818fd37a7SXin LI* sdiff Option Summary:: Summary of @command{sdiff} options. 240918fd37a7SXin LI* Merge Commands:: Merging two files interactively. 241018fd37a7SXin LI@end menu 241118fd37a7SXin LI 241218fd37a7SXin LI@node sdiff Option Summary 241318fd37a7SXin LI@section Specifying @command{diff} Options to @command{sdiff} 241418fd37a7SXin LI@cindex @command{sdiff} output format 241518fd37a7SXin LI 241618fd37a7SXin LIThe following @command{sdiff} options have the same meaning as for 241718fd37a7SXin LI@command{diff}. @xref{diff Options}, for the use of these options. 241818fd37a7SXin LI 241918fd37a7SXin LI@example 242018fd37a7SXin LI-a -b -d -i -t -v 242118fd37a7SXin LI-B -E -I @var{regexp} 242218fd37a7SXin LI 242318fd37a7SXin LI--expand-tabs 242418fd37a7SXin LI--ignore-blank-lines --ignore-case 242518fd37a7SXin LI--ignore-matching-lines=@var{regexp} --ignore-space-change 242618fd37a7SXin LI--ignore-tab-expansion 242718fd37a7SXin LI--left-column --minimal --speed-large-files 242818fd37a7SXin LI--strip-trailing-cr --suppress-common-lines 242918fd37a7SXin LI--tabsize=@var{columns} --text --version --width=@var{columns} 243018fd37a7SXin LI@end example 243118fd37a7SXin LI 243218fd37a7SXin LIFor historical reasons, @command{sdiff} has alternate names for some 243318fd37a7SXin LIoptions. The @option{-l} option is equivalent to the 243418fd37a7SXin LI@option{--left-column} option, and similarly @option{-s} is equivalent 243518fd37a7SXin LIto @option{--suppress-common-lines}. The meaning of the @command{sdiff} 243618fd37a7SXin LI@option{-w} and @option{-W} options is interchanged from that of 243718fd37a7SXin LI@command{diff}: with @command{sdiff}, @option{-w @var{columns}} is 243818fd37a7SXin LIequivalent to @option{--width=@var{columns}}, and @option{-W} is 243918fd37a7SXin LIequivalent to @option{--ignore-all-space}. @command{sdiff} without the 244018fd37a7SXin LI@option{-o} option is equivalent to @command{diff} with the @option{-y} 244118fd37a7SXin LIor @option{--side-by-side} option (@pxref{Side by Side}). 244218fd37a7SXin LI 244318fd37a7SXin LI@node Merge Commands 244418fd37a7SXin LI@section Merge Commands 244518fd37a7SXin LI@cindex merge commands 244618fd37a7SXin LI@cindex merging interactively 244718fd37a7SXin LI 244818fd37a7SXin LIGroups of common lines, with a blank gutter, are copied from the first 244918fd37a7SXin LIfile to the output. After each group of differing lines, @command{sdiff} 245018fd37a7SXin LIprompts with @samp{%} and pauses, waiting for one of the following 245118fd37a7SXin LIcommands. Follow each command with @key{RET}. 245218fd37a7SXin LI 245318fd37a7SXin LI@table @samp 245418fd37a7SXin LI@item e 245518fd37a7SXin LIDiscard both versions. 245618fd37a7SXin LIInvoke a text editor on an empty temporary file, 245718fd37a7SXin LIthen copy the resulting file to the output. 245818fd37a7SXin LI 245918fd37a7SXin LI@item eb 246018fd37a7SXin LIConcatenate the two versions, edit the result in a temporary file, 246118fd37a7SXin LIthen copy the edited result to the output. 246218fd37a7SXin LI 246318fd37a7SXin LI@item ed 246418fd37a7SXin LILike @samp{eb}, except precede each version with a header that 246518fd37a7SXin LIshows what file and lines the version came from. 246618fd37a7SXin LI 246718fd37a7SXin LI@item el 2468*b61bbb70SXin LI@itemx e1 246918fd37a7SXin LIEdit a copy of the left version, then copy the result to the output. 247018fd37a7SXin LI 247118fd37a7SXin LI@item er 2472*b61bbb70SXin LI@itemx e2 247318fd37a7SXin LIEdit a copy of the right version, then copy the result to the output. 247418fd37a7SXin LI 247518fd37a7SXin LI@item l 2476*b61bbb70SXin LI@itemx 1 247718fd37a7SXin LICopy the left version to the output. 247818fd37a7SXin LI 247918fd37a7SXin LI@item q 248018fd37a7SXin LIQuit. 248118fd37a7SXin LI 248218fd37a7SXin LI@item r 2483*b61bbb70SXin LI@itemx 2 248418fd37a7SXin LICopy the right version to the output. 248518fd37a7SXin LI 248618fd37a7SXin LI@item s 248718fd37a7SXin LISilently copy common lines. 248818fd37a7SXin LI 248918fd37a7SXin LI@item v 249018fd37a7SXin LIVerbosely copy common lines. This is the default. 249118fd37a7SXin LI@end table 249218fd37a7SXin LI 249318fd37a7SXin LI@vindex EDITOR 249418fd37a7SXin LIThe text editor invoked is specified by the @env{EDITOR} environment 249518fd37a7SXin LIvariable if it is set. The default is system-dependent. 249618fd37a7SXin LI 249718fd37a7SXin LI@node Merging with patch 249818fd37a7SXin LI@chapter Merging with @command{patch} 249918fd37a7SXin LI 250018fd37a7SXin LI@command{patch} takes comparison output produced by @command{diff} and applies 250118fd37a7SXin LIthe differences to a copy of the original file, producing a patched 250218fd37a7SXin LIversion. With @command{patch}, you can distribute just the changes to a 250318fd37a7SXin LIset of files instead of distributing the entire file set; your 250418fd37a7SXin LIcorrespondents can apply @command{patch} to update their copy of the files 250518fd37a7SXin LIwith your changes. @command{patch} automatically determines the diff 250618fd37a7SXin LIformat, skips any leading or trailing headers, and uses the headers to 250718fd37a7SXin LIdetermine which file to patch. This lets your correspondents feed a 250818fd37a7SXin LImail message containing a difference listing directly to 250918fd37a7SXin LI@command{patch}. 251018fd37a7SXin LI 251118fd37a7SXin LI@command{patch} detects and warns about common problems like forward 251218fd37a7SXin LIpatches. It saves any patches that it could not apply. It can also maintain a 251318fd37a7SXin LI@code{patchlevel.h} file to ensure that your correspondents apply 251418fd37a7SXin LIdiffs in the proper order. 251518fd37a7SXin LI 251618fd37a7SXin LI@command{patch} accepts a series of diffs in its standard input, usually 251718fd37a7SXin LIseparated by headers that specify which file to patch. It applies 251818fd37a7SXin LI@command{diff} hunks (@pxref{Hunks}) one by one. If a hunk does not 251918fd37a7SXin LIexactly match the original file, @command{patch} uses heuristics to try to 252018fd37a7SXin LIpatch the file as well as it can. If no approximate match can be found, 252118fd37a7SXin LI@command{patch} rejects the hunk and skips to the next hunk. @command{patch} 252218fd37a7SXin LInormally replaces each file @var{f} with its new version, putting reject 252318fd37a7SXin LIhunks (if any) into @samp{@var{f}.rej}. 252418fd37a7SXin LI 252518fd37a7SXin LI@xref{Invoking patch}, for detailed information on the options to 252618fd37a7SXin LI@command{patch}. 252718fd37a7SXin LI 252818fd37a7SXin LI@menu 252918fd37a7SXin LI* patch Input:: Selecting the type of @command{patch} input. 253018fd37a7SXin LI* Revision Control:: Getting files from @acronym{RCS}, @acronym{SCCS}, etc. 253118fd37a7SXin LI* Imperfect:: Dealing with imperfect patches. 253218fd37a7SXin LI* Creating and Removing:: Creating and removing files with a patch. 253318fd37a7SXin LI* Patching Time Stamps:: Updating time stamps on patched files. 253418fd37a7SXin LI* Multiple Patches:: Handling multiple patches in a file. 253518fd37a7SXin LI* patch Directories:: Changing directory and stripping directories. 253618fd37a7SXin LI* Backups:: Whether backup files are made. 253718fd37a7SXin LI* Backup Names:: Backup file names. 253818fd37a7SXin LI* Reject Names:: Reject file names. 253918fd37a7SXin LI* patch Messages:: Messages and questions @command{patch} can produce. 254018fd37a7SXin LI* patch and POSIX:: Conformance to the @acronym{POSIX} standard. 254118fd37a7SXin LI* patch and Tradition:: @acronym{GNU} versus traditional @command{patch}. 254218fd37a7SXin LI@end menu 254318fd37a7SXin LI 254418fd37a7SXin LI@node patch Input 254518fd37a7SXin LI@section Selecting the @command{patch} Input Format 254618fd37a7SXin LI@cindex @command{patch} input format 254718fd37a7SXin LI 254818fd37a7SXin LI@command{patch} normally determines which @command{diff} format the patch 254918fd37a7SXin LIfile uses by examining its contents. For patch files that contain 255018fd37a7SXin LIparticularly confusing leading text, you might need to use one of the 255118fd37a7SXin LIfollowing options to force @command{patch} to interpret the patch file as a 255218fd37a7SXin LIcertain format of diff. The output formats listed here are the only 255318fd37a7SXin LIones that @command{patch} can understand. 255418fd37a7SXin LI 255518fd37a7SXin LI@table @option 255618fd37a7SXin LI@item -c 255718fd37a7SXin LI@itemx --context 255818fd37a7SXin LIcontext diff. 255918fd37a7SXin LI 256018fd37a7SXin LI@item -e 256118fd37a7SXin LI@itemx --ed 256218fd37a7SXin LI@command{ed} script. 256318fd37a7SXin LI 256418fd37a7SXin LI@item -n 256518fd37a7SXin LI@itemx --normal 256618fd37a7SXin LInormal diff. 256718fd37a7SXin LI 256818fd37a7SXin LI@item -u 256918fd37a7SXin LI@itemx --unified 257018fd37a7SXin LIunified diff. 257118fd37a7SXin LI@end table 257218fd37a7SXin LI 257318fd37a7SXin LI@node Revision Control 257418fd37a7SXin LI@section Revision Control 257518fd37a7SXin LI@cindex revision control 257618fd37a7SXin LI@cindex version control 257718fd37a7SXin LI@cindex @acronym{RCS} 257818fd37a7SXin LI@cindex ClearCase 257918fd37a7SXin LI@cindex @acronym{SCCS} 258018fd37a7SXin LI 258118fd37a7SXin LIIf a nonexistent input file is under a revision control system 258218fd37a7SXin LIsupported by @command{patch}, @command{patch} normally asks the user 258318fd37a7SXin LIwhether to get (or check out) the file from the revision control 258418fd37a7SXin LIsystem. Patch currently supports @acronym{RCS}, ClearCase and 258518fd37a7SXin LI@acronym{SCCS}. Under @acronym{RCS} and @acronym{SCCS}, 258618fd37a7SXin LI@command{patch} also asks when the input file is read-only and matches 258718fd37a7SXin LIthe default version in the revision control system. 258818fd37a7SXin LI 258918fd37a7SXin LI@vindex PATCH_GET 259018fd37a7SXin LIThe @option{-g @var{num}} or @option{--get=@var{num}} option affects access 259118fd37a7SXin LIto files under supported revision control systems. If @var{num} is 259218fd37a7SXin LIpositive, @command{patch} gets the file without asking the user; if 259318fd37a7SXin LIzero, @command{patch} neither asks the user nor gets the file; and if 259418fd37a7SXin LInegative, @command{patch} asks the user before getting the file. The 259518fd37a7SXin LIdefault value of @var{num} is given by the value of the 259618fd37a7SXin LI@env{PATCH_GET} environment variable if it is set; if not, the default 259718fd37a7SXin LIvalue is zero if @command{patch} is conforming to @acronym{POSIX}, negative 259818fd37a7SXin LIotherwise. @xref{patch and POSIX}. 259918fd37a7SXin LI 260018fd37a7SXin LI@vindex VERSION_CONTROL 260118fd37a7SXin LIThe choice of revision control system is unaffected by the 260218fd37a7SXin LI@env{VERSION_CONTROL} environment variable (@pxref{Backup Names}). 260318fd37a7SXin LI 260418fd37a7SXin LI@node Imperfect 260518fd37a7SXin LI@section Applying Imperfect Patches 260618fd37a7SXin LI@cindex imperfect patch application 260718fd37a7SXin LI 260818fd37a7SXin LI@command{patch} tries to skip any leading text in the patch file, 260918fd37a7SXin LIapply the diff, and then skip any trailing text. Thus you can feed a 261018fd37a7SXin LImail message directly to @command{patch}, and it should work. If the 261118fd37a7SXin LIentire diff is indented by a constant amount of white space, 261218fd37a7SXin LI@command{patch} automatically ignores the indentation. If a context 261318fd37a7SXin LIdiff contains trailing carriage return on each line, @command{patch} 261418fd37a7SXin LIautomatically ignores the carriage return. If a context diff has been 261518fd37a7SXin LIencapsulated by prepending @w{@samp{- }} to lines beginning with @samp{-} 261618fd37a7SXin LIas per @uref{ftp://ftp.isi.edu/in-notes/rfc934.txt, Internet RFC 934}, 261718fd37a7SXin LI@command{patch} automatically unencapsulates the input. 261818fd37a7SXin LI 261918fd37a7SXin LIHowever, certain other types of imperfect input require user 262018fd37a7SXin LIintervention or testing. 262118fd37a7SXin LI 262218fd37a7SXin LI@menu 262318fd37a7SXin LI* Changed White Space:: When tabs and spaces don't match exactly. 262418fd37a7SXin LI* Reversed Patches:: Applying reversed patches correctly. 262518fd37a7SXin LI* Inexact:: Helping @command{patch} find close matches. 262618fd37a7SXin LI* Dry Runs:: Predicting what @command{patch} will do. 262718fd37a7SXin LI@end menu 262818fd37a7SXin LI 262918fd37a7SXin LI@node Changed White Space 263018fd37a7SXin LI@subsection Applying Patches with Changed White Space 263118fd37a7SXin LI@cindex white space in patches 263218fd37a7SXin LI 263318fd37a7SXin LISometimes mailers, editors, or other programs change spaces into tabs, 263418fd37a7SXin LIor vice versa. If this happens to a patch file or an input file, the 263518fd37a7SXin LIfiles might look the same, but @command{patch} will not be able to match 263618fd37a7SXin LIthem properly. If this problem occurs, use the @option{-l} or 263718fd37a7SXin LI@option{--ignore-white-space} option, which makes @command{patch} compare 263818fd37a7SXin LIblank characters (i.e.@: spaces and tabs) loosely so that any nonempty 263918fd37a7SXin LIsequence of blanks in the patch file matches any nonempty sequence of 264018fd37a7SXin LIblanks in the input files. Non-blank 264118fd37a7SXin LIcharacters must still match exactly. Each line of the context must 264218fd37a7SXin LIstill match a line in the input file. 264318fd37a7SXin LI 264418fd37a7SXin LI@node Reversed Patches 264518fd37a7SXin LI@subsection Applying Reversed Patches 264618fd37a7SXin LI@cindex reversed patches 264718fd37a7SXin LI 264818fd37a7SXin LISometimes people run @command{diff} with the new file first instead of 264918fd37a7SXin LIsecond. This creates a diff that is ``reversed''. To apply such 265018fd37a7SXin LIpatches, give @command{patch} the @option{-R} or @option{--reverse} option. 265118fd37a7SXin LI@command{patch} then attempts to swap each hunk around before applying it. 265218fd37a7SXin LIRejects come out in the swapped format. 265318fd37a7SXin LI 265418fd37a7SXin LIOften @command{patch} can guess that the patch is reversed. If the first 265518fd37a7SXin LIhunk of a patch fails, @command{patch} reverses the hunk to see if it can 265618fd37a7SXin LIapply it that way. If it can, @command{patch} asks you if you want to have 265718fd37a7SXin LIthe @option{-R} option set; if it can't, @command{patch} continues to apply 265818fd37a7SXin LIthe patch normally. This method cannot detect a reversed patch if it is 265918fd37a7SXin LIa normal diff and the first command is an append (which should have been 266018fd37a7SXin LIa delete) since appends always succeed, because a null context matches 266118fd37a7SXin LIanywhere. But most patches add or change lines rather than delete them, 266218fd37a7SXin LIso most reversed normal diffs begin with a delete, which fails, and 266318fd37a7SXin LI@command{patch} notices. 266418fd37a7SXin LI 266518fd37a7SXin LIIf you apply a patch that you have already applied, @command{patch} thinks 266618fd37a7SXin LIit is a reversed patch and offers to un-apply the patch. This could be 266718fd37a7SXin LIconstrued as a feature. If you did this inadvertently and you don't 266818fd37a7SXin LIwant to un-apply the patch, just answer @samp{n} to this offer and to 266918fd37a7SXin LIthe subsequent ``apply anyway'' question---or type @kbd{C-c} to kill the 267018fd37a7SXin LI@command{patch} process. 267118fd37a7SXin LI 267218fd37a7SXin LI@node Inexact 267318fd37a7SXin LI@subsection Helping @command{patch} Find Inexact Matches 267418fd37a7SXin LI@cindex inexact patches 267518fd37a7SXin LI@cindex fuzz factor when patching 267618fd37a7SXin LI 267718fd37a7SXin LIFor context diffs, and to a lesser extent normal diffs, @command{patch} can 267818fd37a7SXin LIdetect when the line numbers mentioned in the patch are incorrect, and 267918fd37a7SXin LIit attempts to find the correct place to apply each hunk of the patch. 268018fd37a7SXin LIAs a first guess, it takes the line number mentioned in the hunk, plus 268118fd37a7SXin LIor minus any offset used in applying the previous hunk. If that is not 268218fd37a7SXin LIthe correct place, @command{patch} scans both forward and backward for a 268318fd37a7SXin LIset of lines matching the context given in the hunk. 268418fd37a7SXin LI 268518fd37a7SXin LIFirst @command{patch} looks for a place where all lines of the context 268618fd37a7SXin LImatch. If it cannot find such a place, and it is reading a context or 268718fd37a7SXin LIunified diff, and the maximum fuzz factor is set to 1 or more, then 268818fd37a7SXin LI@command{patch} makes another scan, ignoring the first and last line of 268918fd37a7SXin LIcontext. If that fails, and the maximum fuzz factor is set to 2 or 269018fd37a7SXin LImore, it makes another scan, ignoring the first two and last two lines 269118fd37a7SXin LIof context are ignored. It continues similarly if the maximum fuzz 269218fd37a7SXin LIfactor is larger. 269318fd37a7SXin LI 269418fd37a7SXin LIThe @option{-F @var{lines}} or @option{--fuzz=@var{lines}} option sets the 269518fd37a7SXin LImaximum fuzz factor to @var{lines}. This option only applies to context 269618fd37a7SXin LIand unified diffs; it ignores up to @var{lines} lines while looking for 269718fd37a7SXin LIthe place to install a hunk. Note that a larger fuzz factor increases 269818fd37a7SXin LIthe odds of making a faulty patch. The default fuzz factor is 2; there 269918fd37a7SXin LIis no point to setting it to more than the number of lines of context 270018fd37a7SXin LIin the diff, ordinarily 3. 270118fd37a7SXin LI 270218fd37a7SXin LIIf @command{patch} cannot find a place to install a hunk of the patch, it 270318fd37a7SXin LIwrites the hunk out to a reject file (@pxref{Reject Names}, for information 270418fd37a7SXin LIon how reject files are named). It writes out rejected hunks in context 270518fd37a7SXin LIformat no matter what form the input patch is in. If the input is a 270618fd37a7SXin LInormal or @command{ed} diff, many of the contexts are simply null. The 270718fd37a7SXin LIline numbers on the hunks in the reject file may be different from those 270818fd37a7SXin LIin the patch file: they show the approximate location where @command{patch} 270918fd37a7SXin LIthinks the failed hunks belong in the new file rather than in the old 271018fd37a7SXin LIone. 271118fd37a7SXin LI 271218fd37a7SXin LIIf the @option{--verbose} option is given, then 271318fd37a7SXin LIas it completes each hunk @command{patch} tells you whether the hunk 271418fd37a7SXin LIsucceeded or failed, and if it failed, on which line (in the new file) 271518fd37a7SXin LI@command{patch} thinks the hunk should go. If this is different from the 271618fd37a7SXin LIline number specified in the diff, it tells you the offset. A single 271718fd37a7SXin LIlarge offset @emph{may} indicate that @command{patch} installed a hunk in 271818fd37a7SXin LIthe wrong place. @command{patch} also tells you if it used a fuzz factor 271918fd37a7SXin LIto make the match, in which case you should also be slightly suspicious. 272018fd37a7SXin LI 272118fd37a7SXin LI@command{patch} cannot tell if the line numbers are off in an @command{ed} 272218fd37a7SXin LIscript, and can only detect wrong line numbers in a normal diff when it 272318fd37a7SXin LIfinds a change or delete command. It may have the same problem with a 272418fd37a7SXin LIcontext diff using a fuzz factor equal to or greater than the number of 272518fd37a7SXin LIlines of context shown in the diff (typically 3). In these cases, you 272618fd37a7SXin LIshould probably look at a context diff between your original and patched 272718fd37a7SXin LIinput files to see if the changes make sense. Compiling without errors 272818fd37a7SXin LIis a pretty good indication that the patch worked, but not a guarantee. 272918fd37a7SXin LI 273018fd37a7SXin LIA patch against an empty file applies to a nonexistent file, and vice 273118fd37a7SXin LIversa. @xref{Creating and Removing}. 273218fd37a7SXin LI 273318fd37a7SXin LI@command{patch} usually produces the correct results, even when it must 273418fd37a7SXin LImake many guesses. However, the results are guaranteed only when 273518fd37a7SXin LIthe patch is applied to an exact copy of the file that the patch was 273618fd37a7SXin LIgenerated from. 273718fd37a7SXin LI 273818fd37a7SXin LI@node Dry Runs 273918fd37a7SXin LI@subsection Predicting what @command{patch} will do 274018fd37a7SXin LI@cindex testing @command{patch} 274118fd37a7SXin LI@cindex dry runs for @command{patch} 274218fd37a7SXin LI 274318fd37a7SXin LIIt may not be obvious in advance what @command{patch} will do with a 274418fd37a7SXin LIcomplicated or poorly formatted patch. If you are concerned that the 274518fd37a7SXin LIinput might cause @command{patch} to modify the wrong files, you can 274618fd37a7SXin LIuse the @option{--dry-run} option, which causes @command{patch} to 274718fd37a7SXin LIprint the results of applying patches without actually changing any 274818fd37a7SXin LIfiles. You can then inspect the diagnostics generated by the dry run 274918fd37a7SXin LIto see whether @command{patch} will modify the files that you expect. 275018fd37a7SXin LIIf the patch does not do what you want, you can modify the patch (or 275118fd37a7SXin LIthe other options to @command{patch}) and try another dry run. Once 275218fd37a7SXin LIyou are satisfied with the proposed patch you can apply it by invoking 275318fd37a7SXin LI@command{patch} as before, but this time without the 275418fd37a7SXin LI@option{--dry-run} option. 275518fd37a7SXin LI 275618fd37a7SXin LI@node Creating and Removing 275718fd37a7SXin LI@section Creating and Removing Files 275818fd37a7SXin LI@cindex creating files 275918fd37a7SXin LI@cindex empty files, removing 276018fd37a7SXin LI@cindex removing empty files 276118fd37a7SXin LI 276218fd37a7SXin LISometimes when comparing two directories, a file may exist in one 276318fd37a7SXin LIdirectory but not the other. If you give @command{diff} the 276418fd37a7SXin LI@option{-N} or @option{--new-file} option, or if you supply an old or 276518fd37a7SXin LInew file that is named @file{/dev/null} or is empty and is dated the 276618fd37a7SXin LIEpoch (1970-01-01 00:00:00 UTC), @command{diff} outputs a patch that 276718fd37a7SXin LIadds or deletes the contents of this file. When given such a patch, 276818fd37a7SXin LI@command{patch} normally creates a new file or removes the old file. 276918fd37a7SXin LIHowever, when conforming to @acronym{POSIX} (@pxref{patch and POSIX}), 277018fd37a7SXin LI@command{patch} does not remove the old file, but leaves it empty. 277118fd37a7SXin LIThe @option{-E} or @option{--remove-empty-files} option causes 277218fd37a7SXin LI@command{patch} to remove output files that are empty after applying a 277318fd37a7SXin LIpatch, even if the patch does not appear to be one that removed the 277418fd37a7SXin LIfile. 277518fd37a7SXin LI 277618fd37a7SXin LIIf the patch appears to create a file that already exists, 277718fd37a7SXin LI@command{patch} asks for confirmation before applying the patch. 277818fd37a7SXin LI 277918fd37a7SXin LI@node Patching Time Stamps 278018fd37a7SXin LI@section Updating Time Stamps on Patched Files 278118fd37a7SXin LI@cindex time stamps on patched files 278218fd37a7SXin LI 278318fd37a7SXin LIWhen @command{patch} updates a file, it normally sets the file's 278418fd37a7SXin LIlast-modified time stamp to the current time of day. If you are using 278518fd37a7SXin LI@command{patch} to track a software distribution, this can cause 278618fd37a7SXin LI@command{make} to incorrectly conclude that a patched file is out of 278718fd37a7SXin LIdate. For example, if @file{syntax.c} depends on @file{syntax.y}, and 278818fd37a7SXin LI@command{patch} updates @file{syntax.c} and then @file{syntax.y}, then 278918fd37a7SXin LI@file{syntax.c} will normally appear to be out of date with respect to 279018fd37a7SXin LI@file{syntax.y} even though its contents are actually up to date. 279118fd37a7SXin LI 279218fd37a7SXin LIThe @option{-Z} or @option{--set-utc} option causes @command{patch} to 279318fd37a7SXin LIset a patched file's modification and access times to the time stamps 279418fd37a7SXin LIgiven in context diff headers. If the context diff headers do not 279518fd37a7SXin LIspecify a time zone, they are assumed to use Coordinated Universal 279618fd37a7SXin LITime (@acronym{UTC}, often known as @acronym{GMT}). 279718fd37a7SXin LI 279818fd37a7SXin LIThe @option{-T} or @option{--set-time} option acts like @option{-Z} or 279918fd37a7SXin LI@option{--set-utc}, except that it assumes that the context diff 280018fd37a7SXin LIheaders' time stamps use local time instead of @acronym{UTC}. This option 280118fd37a7SXin LIis not recommended, because patches using local time cannot easily be 280218fd37a7SXin LIused by people in other time zones, and because local time stamps are 280318fd37a7SXin LIambiguous when local clocks move backwards during daylight-saving time 280418fd37a7SXin LIadjustments. If the context diff headers specify a time zone, this 280518fd37a7SXin LIoption is equivalent to @option{-Z} or @option{--set-utc}. 280618fd37a7SXin LI 280718fd37a7SXin LI@command{patch} normally refrains from setting a file's time stamps if 280818fd37a7SXin LIthe file's original last-modified time stamp does not match the time 280918fd37a7SXin LIgiven in the diff header, of if the file's contents do not exactly 281018fd37a7SXin LImatch the patch. However, if the @option{-f} or @option{--force} 281118fd37a7SXin LIoption is given, the file's time stamps are set regardless. 281218fd37a7SXin LI 281318fd37a7SXin LIDue to the limitations of the current @command{diff} format, 281418fd37a7SXin LI@command{patch} cannot update the times of files whose contents have 281518fd37a7SXin LInot changed. Also, if you set file time stamps to values other than 281618fd37a7SXin LIthe current time of day, you should also remove (e.g., with @samp{make 281718fd37a7SXin LIclean}) all files that depend on the patched files, so that later 281818fd37a7SXin LIinvocations of @command{make} do not get confused by the patched 281918fd37a7SXin LIfiles' times. 282018fd37a7SXin LI 282118fd37a7SXin LI@node Multiple Patches 282218fd37a7SXin LI@section Multiple Patches in a File 282318fd37a7SXin LI@cindex multiple patches 282418fd37a7SXin LI@cindex intuiting file names from patches 282518fd37a7SXin LI 282618fd37a7SXin LIIf the patch file contains more than one patch, and if you do not 282718fd37a7SXin LIspecify an input file on the command line, @command{patch} tries to 282818fd37a7SXin LIapply each patch as if they came from separate patch files. This 282918fd37a7SXin LImeans that it determines the name of the file to patch for each patch, 283018fd37a7SXin LIand that it examines the leading text before each patch for file names 283118fd37a7SXin LIand prerequisite revision level (@pxref{Making Patches}, for more on 283218fd37a7SXin LIthat topic). 283318fd37a7SXin LI 283418fd37a7SXin LI@command{patch} uses the following rules to intuit a file name from 283518fd37a7SXin LIthe leading text before a patch. First, @command{patch} takes an 283618fd37a7SXin LIordered list of candidate file names as follows: 283718fd37a7SXin LI 283818fd37a7SXin LI@itemize @bullet 283918fd37a7SXin LI@item 284018fd37a7SXin LIIf the header is that of a context diff, @command{patch} takes the old 284118fd37a7SXin LIand new file names in the header. A name is ignored if it does not 284218fd37a7SXin LIhave enough slashes to satisfy the @option{-p@var{num}} or 284318fd37a7SXin LI@option{--strip=@var{num}} option. The name @file{/dev/null} is also 284418fd37a7SXin LIignored. 284518fd37a7SXin LI 284618fd37a7SXin LI@item 284718fd37a7SXin LIIf there is an @samp{Index:} line in the leading garbage and if either 284818fd37a7SXin LIthe old and new names are both absent or if @command{patch} is 284918fd37a7SXin LIconforming to @acronym{POSIX}, @command{patch} takes the name in the 285018fd37a7SXin LI@samp{Index:} line. 285118fd37a7SXin LI 285218fd37a7SXin LI@item 285318fd37a7SXin LIFor the purpose of the following rules, the candidate file names are 285418fd37a7SXin LIconsidered to be in the order (old, new, index), regardless of the 285518fd37a7SXin LIorder that they appear in the header. 285618fd37a7SXin LI@end itemize 285718fd37a7SXin LI 285818fd37a7SXin LI@noindent 285918fd37a7SXin LIThen @command{patch} selects a file name from the candidate list as 286018fd37a7SXin LIfollows: 286118fd37a7SXin LI 286218fd37a7SXin LI@itemize @bullet 286318fd37a7SXin LI@item 286418fd37a7SXin LIIf some of the named files exist, @command{patch} selects the first 286518fd37a7SXin LIname if conforming to @acronym{POSIX}, and the best name otherwise. 286618fd37a7SXin LI 286718fd37a7SXin LI@item 286818fd37a7SXin LIIf @command{patch} is not ignoring @acronym{RCS}, ClearCase, and @acronym{SCCS} 286918fd37a7SXin LI(@pxref{Revision Control}), and no named files exist but an @acronym{RCS}, 287018fd37a7SXin LIClearCase, or @acronym{SCCS} master is found, @command{patch} selects the 287118fd37a7SXin LIfirst named file with an @acronym{RCS}, ClearCase, or @acronym{SCCS} master. 287218fd37a7SXin LI 287318fd37a7SXin LI@item 287418fd37a7SXin LIIf no named files exist, no @acronym{RCS}, ClearCase, or @acronym{SCCS} master 287518fd37a7SXin LIwas found, some names are given, @command{patch} is not conforming to 287618fd37a7SXin LI@acronym{POSIX}, and the patch appears to create a file, @command{patch} 287718fd37a7SXin LIselects the best name requiring the creation of the fewest 287818fd37a7SXin LIdirectories. 287918fd37a7SXin LI 288018fd37a7SXin LI@item 288118fd37a7SXin LIIf no file name results from the above heuristics, you are asked for 288218fd37a7SXin LIthe name of the file to patch, and @command{patch} selects that name. 288318fd37a7SXin LI@end itemize 288418fd37a7SXin LI 288518fd37a7SXin LITo determine the @dfn{best} of a nonempty list of file names, 288618fd37a7SXin LI@command{patch} first takes all the names with the fewest path name 288718fd37a7SXin LIcomponents; of those, it then takes all the names with the shortest 288818fd37a7SXin LIbasename; of those, it then takes all the shortest names; finally, it 288918fd37a7SXin LItakes the first remaining name. 289018fd37a7SXin LI 289118fd37a7SXin LI@xref{patch and POSIX}, to see whether @command{patch} is conforming 289218fd37a7SXin LIto @acronym{POSIX}. 289318fd37a7SXin LI 289418fd37a7SXin LI@node patch Directories 289518fd37a7SXin LI@section Applying Patches in Other Directories 289618fd37a7SXin LI@cindex directories and patch 289718fd37a7SXin LI@cindex patching directories 289818fd37a7SXin LI 289918fd37a7SXin LIThe @option{-d @var{directory}} or @option{--directory=@var{directory}} 290018fd37a7SXin LIoption to @command{patch} makes directory @var{directory} the current 290118fd37a7SXin LIdirectory for interpreting both file names in the patch file, and file 290218fd37a7SXin LInames given as arguments to other options (such as @option{-B} and 290318fd37a7SXin LI@option{-o}). For example, while in a mail reading program, you can patch 290418fd37a7SXin LIa file in the @file{/usr/src/emacs} directory directly from a message 290518fd37a7SXin LIcontaining the patch like this: 290618fd37a7SXin LI 290718fd37a7SXin LI@example 290818fd37a7SXin LI| patch -d /usr/src/emacs 290918fd37a7SXin LI@end example 291018fd37a7SXin LI 291118fd37a7SXin LISometimes the file names given in a patch contain leading directories, 291218fd37a7SXin LIbut you keep your files in a directory different from the one given in 291318fd37a7SXin LIthe patch. In those cases, you can use the 291418fd37a7SXin LI@option{-p@var{number}} or @option{--strip=@var{number}} 291518fd37a7SXin LIoption to set the file name strip count to @var{number}. The strip 291618fd37a7SXin LIcount tells @command{patch} how many slashes, along with the directory 291718fd37a7SXin LInames between them, to strip from the front of file names. A sequence 291818fd37a7SXin LIof one or more adjacent slashes is counted as a single slash. By 291918fd37a7SXin LIdefault, @command{patch} strips off all leading directories, leaving 292018fd37a7SXin LIjust the base file names. 292118fd37a7SXin LI 292218fd37a7SXin LIFor example, suppose the file name in the patch file is 292318fd37a7SXin LI@file{/gnu/src/emacs/etc/NEWS}. Using @option{-p0} gives the 292418fd37a7SXin LIentire file name unmodified, @option{-p1} gives 292518fd37a7SXin LI@file{gnu/src/emacs/etc/NEWS} (no leading slash), @option{-p4} gives 292618fd37a7SXin LI@file{etc/NEWS}, and not specifying @option{-p} at all gives @file{NEWS}. 292718fd37a7SXin LI 292818fd37a7SXin LI@command{patch} looks for each file (after any slashes have been stripped) 292918fd37a7SXin LIin the current directory, or if you used the @option{-d @var{directory}} 293018fd37a7SXin LIoption, in that directory. 293118fd37a7SXin LI 293218fd37a7SXin LI@node Backups 293318fd37a7SXin LI@section Backup Files 293418fd37a7SXin LI@cindex backup file strategy 293518fd37a7SXin LI 293618fd37a7SXin LINormally, @command{patch} creates a backup file if the patch does not 293718fd37a7SXin LIexactly match the original input file, because in that case the 293818fd37a7SXin LIoriginal data might not be recovered if you undo the patch with 293918fd37a7SXin LI@samp{patch -R} (@pxref{Reversed Patches}). However, when conforming 294018fd37a7SXin LIto @acronym{POSIX}, @command{patch} does not create backup files by 294118fd37a7SXin LIdefault. @xref{patch and POSIX}. 294218fd37a7SXin LI 294318fd37a7SXin LIThe @option{-b} or @option{--backup} option causes @command{patch} to 294418fd37a7SXin LImake a backup file regardless of whether the patch matches the 294518fd37a7SXin LIoriginal input. The @option{--backup-if-mismatch} option causes 294618fd37a7SXin LI@command{patch} to create backup files for mismatches files; this is 294718fd37a7SXin LIthe default when not conforming to @acronym{POSIX}. The 294818fd37a7SXin LI@option{--no-backup-if-mismatch} option causes @command{patch} to not 294918fd37a7SXin LIcreate backup files, even for mismatched patches; this is the default 295018fd37a7SXin LIwhen conforming to @acronym{POSIX}. 295118fd37a7SXin LI 295218fd37a7SXin LIWhen backing up a file that does not exist, an empty, unreadable 295318fd37a7SXin LIbackup file is created as a placeholder to represent the nonexistent 295418fd37a7SXin LIfile. 295518fd37a7SXin LI 295618fd37a7SXin LI@node Backup Names 295718fd37a7SXin LI@section Backup File Names 295818fd37a7SXin LI@cindex backup file names 295918fd37a7SXin LI 296018fd37a7SXin LINormally, @command{patch} renames an original input file into a backup 296118fd37a7SXin LIfile by appending to its name the extension @samp{.orig}, or @samp{~} 296218fd37a7SXin LIif using @samp{.orig} would make the backup file name too 296318fd37a7SXin LIlong.@footnote{A coding error in @acronym{GNU} @command{patch} version 296418fd37a7SXin LI2.5.4 causes it to always use @samp{~}, but this should be fixed in 296518fd37a7SXin LIthe next release.} The @option{-z @var{backup-suffix}} or 296618fd37a7SXin LI@option{--suffix=@var{backup-suffix}} option causes @command{patch} to 296718fd37a7SXin LIuse @var{backup-suffix} as the backup extension instead. 296818fd37a7SXin LI 296918fd37a7SXin LI@vindex SIMPLE_BACKUP_SUFFIX 297018fd37a7SXin LIAlternately, you can specify the extension for backup files with the 297118fd37a7SXin LI@env{SIMPLE_BACKUP_SUFFIX} environment variable, which the options 297218fd37a7SXin LIoverride. 297318fd37a7SXin LI 297418fd37a7SXin LI@command{patch} can also create numbered backup files the way 297518fd37a7SXin LI@acronym{GNU} Emacs does. With this method, instead of having a 297618fd37a7SXin LIsingle backup of each file, @command{patch} makes a new backup file 297718fd37a7SXin LIname each time it patches a file. For example, the backups of a file 297818fd37a7SXin LInamed @file{sink} would be called, successively, @file{sink.~1~}, 297918fd37a7SXin LI@file{sink.~2~}, @file{sink.~3~}, etc. 298018fd37a7SXin LI 298118fd37a7SXin LI@vindex PATCH_VERSION_CONTROL 298218fd37a7SXin LI@vindex VERSION_CONTROL 298318fd37a7SXin LIThe @option{-V @var{backup-style}} or 298418fd37a7SXin LI@option{--version-control=@var{backup-style}} option takes as an 298518fd37a7SXin LIargument a method for creating backup file names. You can alternately 298618fd37a7SXin LIcontrol the type of backups that @command{patch} makes with the 298718fd37a7SXin LI@env{PATCH_VERSION_CONTROL} environment variable, which the 298818fd37a7SXin LI@option{-V} option overrides. If @env{PATCH_VERSION_CONTROL} is not 298918fd37a7SXin LIset, the @env{VERSION_CONTROL} environment variable is used instead. 299018fd37a7SXin LIPlease note that these options and variables control backup file 299118fd37a7SXin LInames; they do not affect the choice of revision control system 299218fd37a7SXin LI(@pxref{Revision Control}). 299318fd37a7SXin LI 299418fd37a7SXin LIThe values of these environment variables and the argument to the 299518fd37a7SXin LI@option{-V} option are like the @acronym{GNU} Emacs @code{version-control} 299618fd37a7SXin LIvariable (@pxref{Backup Names, , , emacs, The @acronym{GNU} Emacs Manual}, 299718fd37a7SXin LIfor more information on backup versions in Emacs). They also 299818fd37a7SXin LIrecognize synonyms that are more descriptive. The valid values are 299918fd37a7SXin LIlisted below; unique abbreviations are acceptable. 300018fd37a7SXin LI 300118fd37a7SXin LI@table @option 300218fd37a7SXin LI@item t 300318fd37a7SXin LI@itemx numbered 300418fd37a7SXin LIAlways make numbered backups. 300518fd37a7SXin LI 300618fd37a7SXin LI@item nil 300718fd37a7SXin LI@itemx existing 300818fd37a7SXin LIMake numbered backups of files that already have them, simple backups of 300918fd37a7SXin LIthe others. This is the default. 301018fd37a7SXin LI 301118fd37a7SXin LI@item never 301218fd37a7SXin LI@itemx simple 301318fd37a7SXin LIAlways make simple backups. 301418fd37a7SXin LI@end table 301518fd37a7SXin LI 301618fd37a7SXin LIYou can also tell @command{patch} to prepend a prefix, such as a 301718fd37a7SXin LIdirectory name, to produce backup file names. The @option{-B 301818fd37a7SXin LI@var{prefix}} or @option{--prefix=@var{prefix}} option makes backup 301918fd37a7SXin LIfiles by prepending @var{prefix} to them. The @option{-Y 302018fd37a7SXin LI@var{prefix}} or @option{--basename-prefix=@var{prefix}} prepends 302118fd37a7SXin LI@var{prefix} to the last file name component of backup file names 302218fd37a7SXin LIinstead; for example, @option{-Y ~} causes the backup name for 302318fd37a7SXin LI@file{dir/file.c} to be @file{dir/~file.c}. If you use either of 302418fd37a7SXin LIthese prefix options, the suffix-based options are ignored. 302518fd37a7SXin LI 302618fd37a7SXin LIIf you specify the output file with the @option{-o} option, that file is 302718fd37a7SXin LIthe one that is backed up, not the input file. 302818fd37a7SXin LI 302918fd37a7SXin LIOptions that affect the names of backup files do not affect whether 303018fd37a7SXin LIbackups are made. For example, if you specify the 303118fd37a7SXin LI@option{--no-backup-if-mismatch} option, none of the options described 303218fd37a7SXin LIin this section have any affect, because no backups are made. 303318fd37a7SXin LI 303418fd37a7SXin LI@node Reject Names 303518fd37a7SXin LI@section Reject File Names 303618fd37a7SXin LI@cindex reject file names 303718fd37a7SXin LI 303818fd37a7SXin LIThe names for reject files (files containing patches that 303918fd37a7SXin LI@command{patch} could not find a place to apply) are normally the name 304018fd37a7SXin LIof the output file with @samp{.rej} appended (or @samp{#} if using 304118fd37a7SXin LI@samp{.rej} would make the backup file name too long). 304218fd37a7SXin LI 304318fd37a7SXin LIAlternatively, you can tell @command{patch} to place all of the rejected 304418fd37a7SXin LIpatches in a single file. The @option{-r @var{reject-file}} or 304518fd37a7SXin LI@option{--reject-file=@var{reject-file}} option uses @var{reject-file} as 304618fd37a7SXin LIthe reject file name. 304718fd37a7SXin LI 304818fd37a7SXin LI@node patch Messages 304918fd37a7SXin LI@section Messages and Questions from @command{patch} 305018fd37a7SXin LI@cindex @command{patch} messages and questions 305118fd37a7SXin LI@cindex diagnostics from @command{patch} 305218fd37a7SXin LI@cindex messages from @command{patch} 305318fd37a7SXin LI 305418fd37a7SXin LI@command{patch} can produce a variety of messages, especially if it 305518fd37a7SXin LIhas trouble decoding its input. In a few situations where it's not 305618fd37a7SXin LIsure how to proceed, @command{patch} normally prompts you for more 305718fd37a7SXin LIinformation from the keyboard. There are options to produce more or 305818fd37a7SXin LIfewer messages, to have it not ask for keyboard input, and to 305918fd37a7SXin LIaffect the way that file names are quoted in messages. 306018fd37a7SXin LI 306118fd37a7SXin LI@menu 306218fd37a7SXin LI* More or Fewer Messages:: Controlling the verbosity of @command{patch}. 306318fd37a7SXin LI* patch and Keyboard Input:: Inhibiting keyboard input. 306418fd37a7SXin LI* patch Quoting Style:: Quoting file names in diagnostics. 306518fd37a7SXin LI@end menu 306618fd37a7SXin LI 306718fd37a7SXin LI@command{patch} exits with status 0 if all hunks are applied successfully, 306818fd37a7SXin LI1 if some hunks cannot be applied, and 2 if there is more serious trouble. 306918fd37a7SXin LIWhen applying a set of patches in a loop, you should check the 307018fd37a7SXin LIexit status, so you don't apply a later patch to a partially patched 307118fd37a7SXin LIfile. 307218fd37a7SXin LI 307318fd37a7SXin LI@node More or Fewer Messages 307418fd37a7SXin LI@subsection Controlling the Verbosity of @command{patch} 307518fd37a7SXin LI@cindex verbose messages from @command{patch} 307618fd37a7SXin LI@cindex inhibit messages from @command{patch} 307718fd37a7SXin LI 307818fd37a7SXin LIYou can cause @command{patch} to produce more messages by using the 307918fd37a7SXin LI@option{--verbose} option. For example, when you give this option, 308018fd37a7SXin LIthe message @samp{Hmm...} indicates that @command{patch} is reading text in 308118fd37a7SXin LIthe patch file, attempting to determine whether there is a patch in that 308218fd37a7SXin LItext, and if so, what kind of patch it is. 308318fd37a7SXin LI 308418fd37a7SXin LIYou can inhibit all terminal output from @command{patch}, unless an error 308518fd37a7SXin LIoccurs, by using the @option{-s}, @option{--quiet}, or @option{--silent} 308618fd37a7SXin LIoption. 308718fd37a7SXin LI 308818fd37a7SXin LI@node patch and Keyboard Input 308918fd37a7SXin LI@subsection Inhibiting Keyboard Input 309018fd37a7SXin LI@cindex keyboard input to @command{patch} 309118fd37a7SXin LI 309218fd37a7SXin LIThere are two ways you can prevent @command{patch} from asking you any 309318fd37a7SXin LIquestions. The @option{-f} or @option{--force} option assumes that you know 309418fd37a7SXin LIwhat you are doing. It causes @command{patch} to do the following: 309518fd37a7SXin LI 309618fd37a7SXin LI@itemize @bullet 309718fd37a7SXin LI@item 309818fd37a7SXin LISkip patches that do not contain file names in their headers. 309918fd37a7SXin LI 310018fd37a7SXin LI@item 310118fd37a7SXin LIPatch files even though they have the wrong version for the 310218fd37a7SXin LI@samp{Prereq:} line in the patch; 310318fd37a7SXin LI 310418fd37a7SXin LI@item 310518fd37a7SXin LIAssume that patches are not reversed even if they look like they are. 310618fd37a7SXin LI@end itemize 310718fd37a7SXin LI 310818fd37a7SXin LI@noindent 310918fd37a7SXin LIThe @option{-t} or @option{--batch} option is similar to @option{-f}, in that 311018fd37a7SXin LIit suppresses questions, but it makes somewhat different assumptions: 311118fd37a7SXin LI 311218fd37a7SXin LI@itemize @bullet 311318fd37a7SXin LI@item 311418fd37a7SXin LISkip patches that do not contain file names in their headers 311518fd37a7SXin LI(the same as @option{-f}). 311618fd37a7SXin LI 311718fd37a7SXin LI@item 311818fd37a7SXin LISkip patches for which the file has the wrong version for the 311918fd37a7SXin LI@samp{Prereq:} line in the patch; 312018fd37a7SXin LI 312118fd37a7SXin LI@item 312218fd37a7SXin LIAssume that patches are reversed if they look like they are. 312318fd37a7SXin LI@end itemize 312418fd37a7SXin LI 312518fd37a7SXin LI@node patch Quoting Style 312618fd37a7SXin LI@subsection @command{patch} Quoting Style 312718fd37a7SXin LI@cindex quoting style 312818fd37a7SXin LI 312918fd37a7SXin LIWhen @command{patch} outputs a file name in a diagnostic message, it 313018fd37a7SXin LIcan format the name in any of several ways. This can be useful to 313118fd37a7SXin LIoutput file names unambiguously, even if they contain punctuation or 313218fd37a7SXin LIspecial characters like newlines. The 313318fd37a7SXin LI@option{--quoting-style=@var{word}} option controls how names are 313418fd37a7SXin LIoutput. The @var{word} should be one of the following: 313518fd37a7SXin LI 313618fd37a7SXin LI@table @samp 313718fd37a7SXin LI@item literal 313818fd37a7SXin LIOutput names as-is. 313918fd37a7SXin LI@item shell 314018fd37a7SXin LIQuote names for the shell if they contain shell metacharacters or would 314118fd37a7SXin LIcause ambiguous output. 314218fd37a7SXin LI@item shell-always 314318fd37a7SXin LIQuote names for the shell, even if they would normally not require quoting. 314418fd37a7SXin LI@item c 314518fd37a7SXin LIQuote names as for a C language string. 314618fd37a7SXin LI@item escape 314718fd37a7SXin LIQuote as with @samp{c} except omit the surrounding double-quote 314818fd37a7SXin LIcharacters. 314918fd37a7SXin LI@c The following are not yet implemented in patch 2.5.4. 315018fd37a7SXin LI@c @item clocale 315118fd37a7SXin LI@c Quote as with @samp{c} except use quotation marks appropriate for the 315218fd37a7SXin LI@c locale. 315318fd37a7SXin LI@c @item locale 315418fd37a7SXin LI@c @c Use @t instead of @samp to avoid duplicate quoting in some output styles. 315518fd37a7SXin LI@c Like @samp{clocale}, but quote @t{`like this'} instead of @t{"like 315618fd37a7SXin LI@c this"} in the default C locale. This looks nicer on many displays. 315718fd37a7SXin LI@end table 315818fd37a7SXin LI 315918fd37a7SXin LI@vindex QUOTING_STYLE 316018fd37a7SXin LIYou can specify the default value of the @option{--quoting-style} 316118fd37a7SXin LIoption with the environment variable @env{QUOTING_STYLE}. If that 316218fd37a7SXin LIenvironment variable is not set, the default value is @samp{shell}, 316318fd37a7SXin LIbut this default may change in a future version of @command{patch}. 316418fd37a7SXin LI 316518fd37a7SXin LI@node patch and POSIX 316618fd37a7SXin LI@section @command{patch} and the @acronym{POSIX} Standard 316718fd37a7SXin LI@cindex @acronym{POSIX} 316818fd37a7SXin LI 316918fd37a7SXin LI@vindex POSIXLY_CORRECT 317018fd37a7SXin LIIf you specify the @option{--posix} option, or set the 317118fd37a7SXin LI@env{POSIXLY_CORRECT} environment variable, @command{patch} conforms 317218fd37a7SXin LImore strictly to the @acronym{POSIX} standard, as follows: 317318fd37a7SXin LI 317418fd37a7SXin LI@itemize @bullet 317518fd37a7SXin LI@item 317618fd37a7SXin LITake the first existing file from the list (old, new, index) 317718fd37a7SXin LIwhen intuiting file names from diff headers. @xref{Multiple Patches}. 317818fd37a7SXin LI 317918fd37a7SXin LI@item 318018fd37a7SXin LIDo not remove files that are removed by a diff. 318118fd37a7SXin LI@xref{Creating and Removing}. 318218fd37a7SXin LI 318318fd37a7SXin LI@item 318418fd37a7SXin LIDo not ask whether to get files from @acronym{RCS}, ClearCase, or 318518fd37a7SXin LI@acronym{SCCS}. @xref{Revision Control}. 318618fd37a7SXin LI 318718fd37a7SXin LI@item 318818fd37a7SXin LIRequire that all options precede the files in the command line. 318918fd37a7SXin LI 319018fd37a7SXin LI@item 319118fd37a7SXin LIDo not backup files, even when there is a mismatch. @xref{Backups}. 319218fd37a7SXin LI 319318fd37a7SXin LI@end itemize 319418fd37a7SXin LI 319518fd37a7SXin LI@node patch and Tradition 319618fd37a7SXin LI@section @acronym{GNU} @command{patch} and Traditional @command{patch} 319718fd37a7SXin LI@cindex traditional @command{patch} 319818fd37a7SXin LI 319918fd37a7SXin LIThe current version of @acronym{GNU} @command{patch} normally follows the 320018fd37a7SXin LI@acronym{POSIX} standard. @xref{patch and POSIX}, for the few exceptions 320118fd37a7SXin LIto this general rule. 320218fd37a7SXin LI 320318fd37a7SXin LIUnfortunately, @acronym{POSIX} redefined the behavior of @command{patch} in 320418fd37a7SXin LIseveral important ways. You should be aware of the following 320518fd37a7SXin LIdifferences if you must interoperate with traditional @command{patch}, 320618fd37a7SXin LIor with @acronym{GNU} @command{patch} version 2.1 and earlier. 320718fd37a7SXin LI 320818fd37a7SXin LI@itemize @bullet 320918fd37a7SXin LI@item 321018fd37a7SXin LIIn traditional @command{patch}, the @option{-p} option's operand was 321118fd37a7SXin LIoptional, and a bare @option{-p} was equivalent to @option{-p0}. The 321218fd37a7SXin LI@option{-p} option now requires an operand, and @option{-p@ 0} is now 321318fd37a7SXin LIequivalent to @option{-p0}. For maximum compatibility, use options 321418fd37a7SXin LIlike @option{-p0} and @option{-p1}. 321518fd37a7SXin LI 321618fd37a7SXin LIAlso, traditional @command{patch} simply counted slashes when 321718fd37a7SXin LIstripping path prefixes; @command{patch} now counts pathname 321818fd37a7SXin LIcomponents. That is, a sequence of one or more adjacent slashes now 321918fd37a7SXin LIcounts as a single slash. For maximum portability, avoid sending 322018fd37a7SXin LIpatches containing @file{//} in file names. 322118fd37a7SXin LI 322218fd37a7SXin LI@item 322318fd37a7SXin LIIn traditional @command{patch}, backups were enabled by default. This 322418fd37a7SXin LIbehavior is now enabled with the @option{-b} or @option{--backup} 322518fd37a7SXin LIoption. 322618fd37a7SXin LI 322718fd37a7SXin LIConversely, in @acronym{POSIX} @command{patch}, backups are never made, 322818fd37a7SXin LIeven when there is a mismatch. In @acronym{GNU} @command{patch}, this 322918fd37a7SXin LIbehavior is enabled with the @option{--no-backup-if-mismatch} option, 323018fd37a7SXin LIor by conforming to @acronym{POSIX}. 323118fd37a7SXin LI 323218fd37a7SXin LIThe @option{-b@ @var{suffix}} option of traditional @command{patch} is 323318fd37a7SXin LIequivalent to the @samp{-b -z@ @var{suffix}} options of @acronym{GNU} 323418fd37a7SXin LI@command{patch}. 323518fd37a7SXin LI 323618fd37a7SXin LI@item 323718fd37a7SXin LITraditional @command{patch} used a complicated (and incompletely 323818fd37a7SXin LIdocumented) method to intuit the name of the file to be patched from 323918fd37a7SXin LIthe patch header. This method did not conform to @acronym{POSIX}, and had 324018fd37a7SXin LIa few gotchas. Now @command{patch} uses a different, equally 324118fd37a7SXin LIcomplicated (but better documented) method that is optionally 324218fd37a7SXin LI@acronym{POSIX}-conforming; we hope it has fewer gotchas. The two methods 324318fd37a7SXin LIare compatible if the file names in the context diff header and the 324418fd37a7SXin LI@samp{Index:} line are all identical after prefix-stripping. Your 324518fd37a7SXin LIpatch is normally compatible if each header's file names all contain 324618fd37a7SXin LIthe same number of slashes. 324718fd37a7SXin LI 324818fd37a7SXin LI@item 324918fd37a7SXin LIWhen traditional @command{patch} asked the user a question, it sent 325018fd37a7SXin LIthe question to standard error and looked for an answer from the first 325118fd37a7SXin LIfile in the following list that was a terminal: standard error, 325218fd37a7SXin LIstandard output, @file{/dev/tty}, and standard input. Now 325318fd37a7SXin LI@command{patch} sends questions to standard output and gets answers 325418fd37a7SXin LIfrom @file{/dev/tty}. Defaults for some answers have been changed so 325518fd37a7SXin LIthat @command{patch} never goes into an infinite loop when using 325618fd37a7SXin LIdefault answers. 325718fd37a7SXin LI 325818fd37a7SXin LI@item 325918fd37a7SXin LITraditional @command{patch} exited with a status value that counted 326018fd37a7SXin LIthe number of bad hunks, or with status 1 if there was real trouble. 326118fd37a7SXin LINow @command{patch} exits with status 1 if some hunks failed, or with 326218fd37a7SXin LI2 if there was real trouble. 326318fd37a7SXin LI 326418fd37a7SXin LI@item 326518fd37a7SXin LILimit yourself to the following options when sending instructions 326618fd37a7SXin LImeant to be executed by anyone running @acronym{GNU} @command{patch}, 326718fd37a7SXin LItraditional @command{patch}, or a @command{patch} that conforms to 326818fd37a7SXin LI@acronym{POSIX}. Spaces are significant in the following list, and 326918fd37a7SXin LIoperands are required. 327018fd37a7SXin LI 327118fd37a7SXin LI@example 327218fd37a7SXin LI@option{-c} 327318fd37a7SXin LI@option{-d @var{dir}} 327418fd37a7SXin LI@option{-D @var{define}} 327518fd37a7SXin LI@option{-e} 327618fd37a7SXin LI@option{-l} 327718fd37a7SXin LI@option{-n} 327818fd37a7SXin LI@option{-N} 327918fd37a7SXin LI@option{-o @var{outfile}} 328018fd37a7SXin LI@option{-p@var{num}} 328118fd37a7SXin LI@option{-R} 328218fd37a7SXin LI@option{-r @var{rejectfile}} 328318fd37a7SXin LI@end example 328418fd37a7SXin LI 328518fd37a7SXin LI@end itemize 328618fd37a7SXin LI 328718fd37a7SXin LI@node Making Patches 328818fd37a7SXin LI@chapter Tips for Making and Using Patches 328918fd37a7SXin LI 329018fd37a7SXin LIUse some common sense when making and using patches. For example, 329118fd37a7SXin LIwhen sending bug fixes to a program's maintainer, send several small 329218fd37a7SXin LIpatches, one per independent subject, instead of one large, 329318fd37a7SXin LIharder-to-digest patch that covers all the subjects. 329418fd37a7SXin LI 329518fd37a7SXin LIHere are some other things you should keep in mind if you are going to 329618fd37a7SXin LIdistribute patches for updating a software package. 329718fd37a7SXin LI 329818fd37a7SXin LI@menu 329918fd37a7SXin LI* Tips for Patch Producers:: Advice for making patches. 330018fd37a7SXin LI* Tips for Patch Consumers:: Advice for using patches. 330118fd37a7SXin LI* Avoiding Common Mistakes:: Avoiding common mistakes when using @command{patch}. 330218fd37a7SXin LI* Generating Smaller Patches:: How to generate smaller patches. 330318fd37a7SXin LI@end menu 330418fd37a7SXin LI 330518fd37a7SXin LI@node Tips for Patch Producers 330618fd37a7SXin LI@section Tips for Patch Producers 330718fd37a7SXin LI@cindex patch producer tips 330818fd37a7SXin LI 330918fd37a7SXin LITo create a patch that changes an older version of a package into a 331018fd37a7SXin LInewer version, first make a copy of the older and newer versions in 331118fd37a7SXin LIadjacent subdirectories. It is common to do that by unpacking 331218fd37a7SXin LI@command{tar} archives of the two versions. 331318fd37a7SXin LI 331418fd37a7SXin LITo generate the patch, use the command @samp{diff -Naur @var{old} 331518fd37a7SXin LI@var{new}} where @var{old} and @var{new} identify the old and new 331618fd37a7SXin LIdirectories. The names @var{old} and @var{new} should not contain any 331718fd37a7SXin LIslashes. The @option{-N} option lets the patch create and remove 331818fd37a7SXin LIfiles; @option{-a} lets the patch update non-text files; @option{-u} 331918fd37a7SXin LIgenerates useful time stamps and enough context; and @option{-r} lets 332018fd37a7SXin LIthe patch update subdirectories. Here is an example command, using 332118fd37a7SXin LIBourne shell syntax: 332218fd37a7SXin LI 332318fd37a7SXin LI@example 332418fd37a7SXin LIdiff -Naur gcc-3.0.3 gcc-3.0.4 332518fd37a7SXin LI@end example 332618fd37a7SXin LI 332718fd37a7SXin LITell your recipients how to apply the patches. This should include 332818fd37a7SXin LIwhich working directory to use, and which @command{patch} options to 332918fd37a7SXin LIuse; the option @samp{-p1} is recommended. Test your procedure by 333018fd37a7SXin LIpretending to be a recipient and applying your patches to a copy of 333118fd37a7SXin LIthe original files. 333218fd37a7SXin LI 333318fd37a7SXin LI@xref{Avoiding Common Mistakes}, for how to avoid common mistakes when 333418fd37a7SXin LIgenerating a patch. 333518fd37a7SXin LI 333618fd37a7SXin LI@node Tips for Patch Consumers 333718fd37a7SXin LI@section Tips for Patch Consumers 333818fd37a7SXin LI@cindex patch consumer tips 333918fd37a7SXin LI 334018fd37a7SXin LIA patch producer should tell recipients how to apply the patches, so 334118fd37a7SXin LIthe first rule of thumb for a patch consumer is to follow the 334218fd37a7SXin LIinstructions supplied with the patch. 334318fd37a7SXin LI 334418fd37a7SXin LI@acronym{GNU} @command{diff} can analyze files with arbitrarily long lines 334518fd37a7SXin LIand files that end in incomplete lines. However, older versions of 334618fd37a7SXin LI@command{patch} cannot patch such files. If you are having trouble 334718fd37a7SXin LIapplying such patches, try upgrading to a recent version of @acronym{GNU} 334818fd37a7SXin LI@command{patch}. 334918fd37a7SXin LI 335018fd37a7SXin LI@node Avoiding Common Mistakes 335118fd37a7SXin LI@section Avoiding Common Mistakes 335218fd37a7SXin LI@cindex common mistakes with patches 335318fd37a7SXin LI@cindex patch, common mistakes 335418fd37a7SXin LI 335518fd37a7SXin LIWhen producing a patch for multiple files, apply @command{diff} to 335618fd37a7SXin LIdirectories whose names do not have slashes. This reduces confusion 335718fd37a7SXin LIwhen the patch consumer specifies the @option{-p@var{number}} option, 335818fd37a7SXin LIsince this option can have surprising results when the old and new 335918fd37a7SXin LIfile names have different numbers of slashes. For example, do not 336018fd37a7SXin LIsend a patch with a header that looks like this: 336118fd37a7SXin LI 336218fd37a7SXin LI@example 336318fd37a7SXin LIdiff -Naur v2.0.29/prog/README prog/README 336418fd37a7SXin LI--- v2.0.29/prog/README 2002-03-10 23:30:39.942229878 -0800 336518fd37a7SXin LI+++ prog/README 2002-03-17 20:49:32.442260588 -0800 336618fd37a7SXin LI@end example 336718fd37a7SXin LI 336818fd37a7SXin LI@noindent 336918fd37a7SXin LIbecause the two file names have different numbers of slashes, and 337018fd37a7SXin LIdifferent versions of @command{patch} interpret the file names 337118fd37a7SXin LIdifferently. To avoid confusion, send output that looks like this 337218fd37a7SXin LIinstead: 337318fd37a7SXin LI 337418fd37a7SXin LI@example 337518fd37a7SXin LIdiff -Naur v2.0.29/prog/README v2.0.30/prog/README 337618fd37a7SXin LI--- v2.0.29/prog/README 2002-03-10 23:30:39.942229878 -0800 337718fd37a7SXin LI+++ v2.0.30/prog/README 2002-03-17 20:49:32.442260588 -0800 337818fd37a7SXin LI@end example 337918fd37a7SXin LI 338018fd37a7SXin LIMake sure you have specified the file names correctly, either in a 338118fd37a7SXin LIcontext diff header or with an @samp{Index:} line. Take care to not send out 338218fd37a7SXin LIreversed patches, since these make people wonder whether they have 338318fd37a7SXin LIalready applied the patch. 338418fd37a7SXin LI 338518fd37a7SXin LIAvoid sending patches that compare backup file names like 338618fd37a7SXin LI@file{README.orig} or @file{README~}, since this might confuse 338718fd37a7SXin LI@command{patch} into patching a backup file instead of the real file. 338818fd37a7SXin LIInstead, send patches that compare the same base file names in 338918fd37a7SXin LIdifferent directories, e.g.@: @file{old/README} and @file{new/README}. 339018fd37a7SXin LI 339118fd37a7SXin LITo save people from partially applying a patch before other patches that 339218fd37a7SXin LIshould have gone before it, you can make the first patch in the patch 339318fd37a7SXin LIfile update a file with a name like @file{patchlevel.h} or 339418fd37a7SXin LI@file{version.c}, which contains a patch level or version number. If 339518fd37a7SXin LIthe input file contains the wrong version number, @command{patch} will 339618fd37a7SXin LIcomplain immediately. 339718fd37a7SXin LI 339818fd37a7SXin LIAn even clearer way to prevent this problem is to put a @samp{Prereq:} 339918fd37a7SXin LIline before the patch. If the leading text in the patch file contains a 340018fd37a7SXin LIline that starts with @samp{Prereq:}, @command{patch} takes the next word 340118fd37a7SXin LIfrom that line (normally a version number) and checks whether the next 340218fd37a7SXin LIinput file contains that word, preceded and followed by either 340318fd37a7SXin LIwhite space or a newline. If not, @command{patch} prompts you for 340418fd37a7SXin LIconfirmation before proceeding. This makes it difficult to accidentally 340518fd37a7SXin LIapply patches in the wrong order. 340618fd37a7SXin LI 340718fd37a7SXin LI@node Generating Smaller Patches 340818fd37a7SXin LI@section Generating Smaller Patches 340918fd37a7SXin LI@cindex patches, shrinking 341018fd37a7SXin LI 341118fd37a7SXin LIThe simplest way to generate a patch is to use @samp{diff -Naur} 341218fd37a7SXin LI(@pxref{Tips for Patch Producers}), but you might be able to reduce 341318fd37a7SXin LIthe size of the patch by renaming or removing some files before making 341418fd37a7SXin LIthe patch. If the older version of the package contains any files 341518fd37a7SXin LIthat the newer version does not, or if any files have been renamed 341618fd37a7SXin LIbetween the two versions, make a list of @command{rm} and @command{mv} 341718fd37a7SXin LIcommands for the user to execute in the old version directory before 341818fd37a7SXin LIapplying the patch. Then run those commands yourself in the scratch 341918fd37a7SXin LIdirectory. 342018fd37a7SXin LI 342118fd37a7SXin LIIf there are any files that you don't need to include in the patch 342218fd37a7SXin LIbecause they can easily be rebuilt from other files (for example, 342318fd37a7SXin LI@file{TAGS} and output from @command{yacc} and @command{makeinfo}), 342418fd37a7SXin LIexclude them from the patch by giving @command{diff} the @option{-x 342518fd37a7SXin LI@var{pattern}} option (@pxref{Comparing Directories}). If you want 342618fd37a7SXin LIyour patch to modify a derived file because your recipients lack tools 342718fd37a7SXin LIto build it, make sure that the patch for the derived file follows any 342818fd37a7SXin LIpatches for files that it depends on, so that the recipients' time 342918fd37a7SXin LIstamps will not confuse @command{make}. 343018fd37a7SXin LI 343118fd37a7SXin LINow you can create the patch using @samp{diff -Naur}. Make sure to 343218fd37a7SXin LIspecify the scratch directory first and the newer directory second. 343318fd37a7SXin LI 343418fd37a7SXin LIAdd to the top of the patch a note telling the user any @command{rm} and 343518fd37a7SXin LI@command{mv} commands to run before applying the patch. Then you can 343618fd37a7SXin LIremove the scratch directory. 343718fd37a7SXin LI 343818fd37a7SXin LIYou can also shrink the patch size by using fewer lines of context, 343918fd37a7SXin LIbut bear in mind that @command{patch} typically needs at least two 344018fd37a7SXin LIlines for proper operation when patches do not exactly match the input 344118fd37a7SXin LIfiles. 344218fd37a7SXin LI 344318fd37a7SXin LI@node Invoking cmp 344418fd37a7SXin LI@chapter Invoking @command{cmp} 344518fd37a7SXin LI@cindex invoking @command{cmp} 344618fd37a7SXin LI@cindex @command{cmp} invocation 344718fd37a7SXin LI 344818fd37a7SXin LIThe @command{cmp} command compares two files, and if they differ, 344918fd37a7SXin LItells the first byte and line number where they differ or reports 345018fd37a7SXin LIthat one file is a prefix of the other. Bytes and 345118fd37a7SXin LIlines are numbered starting with 1. The arguments of @command{cmp} 345218fd37a7SXin LIare as follows: 345318fd37a7SXin LI 345418fd37a7SXin LI@example 345518fd37a7SXin LIcmp @var{options}@dots{} @var{from-file} @r{[}@var{to-file} @r{[}@var{from-skip} @r{[}@var{to-skip}@r{]}@r{]}@r{]} 345618fd37a7SXin LI@end example 345718fd37a7SXin LI 345818fd37a7SXin LIThe file name @file{-} is always the standard input. @command{cmp} 345918fd37a7SXin LIalso uses the standard input if one file name is omitted. The 346018fd37a7SXin LI@var{from-skip} and @var{to-skip} operands specify how many bytes to 346118fd37a7SXin LIignore at the start of each file; they are equivalent to the 346218fd37a7SXin LI@option{--ignore-initial=@var{from-skip}:@var{to-skip}} option. 346318fd37a7SXin LI 346418fd37a7SXin LIBy default, @command{cmp} outputs nothing if the two files have the 346518fd37a7SXin LIsame contents. If one file is a prefix of the other, @command{cmp} 346618fd37a7SXin LIprints to standard error a message of the following form: 346718fd37a7SXin LI 346818fd37a7SXin LI@example 346918fd37a7SXin LIcmp: EOF on @var{shorter-file} 347018fd37a7SXin LI@end example 347118fd37a7SXin LI 347218fd37a7SXin LIOtherwise, @command{cmp} prints to standard output a message of the 347318fd37a7SXin LIfollowing form: 347418fd37a7SXin LI 347518fd37a7SXin LI@example 347618fd37a7SXin LI@var{from-file} @var{to-file} differ: char @var{byte-number}, line @var{line-number} 347718fd37a7SXin LI@end example 347818fd37a7SXin LI 347918fd37a7SXin LIThe message formats can differ outside the @acronym{POSIX} locale. 348018fd37a7SXin LIAlso, @acronym{POSIX} allows the @acronym{EOF} message to be followed 348118fd37a7SXin LIby a blank and some additional information. 348218fd37a7SXin LI 348318fd37a7SXin LIAn exit status of 0 means no differences were found, 1 means some 348418fd37a7SXin LIdifferences were found, and 2 means trouble. 348518fd37a7SXin LI 348618fd37a7SXin LI@menu 348718fd37a7SXin LI* cmp Options:: Summary of options to @command{cmp}. 348818fd37a7SXin LI@end menu 348918fd37a7SXin LI 349018fd37a7SXin LI@node cmp Options 349118fd37a7SXin LI@section Options to @command{cmp} 349218fd37a7SXin LI@cindex @command{cmp} options 349318fd37a7SXin LI@cindex options for @command{cmp} 349418fd37a7SXin LI 349518fd37a7SXin LIBelow is a summary of all of the options that @acronym{GNU} 349618fd37a7SXin LI@command{cmp} accepts. Most options have two equivalent names, one of 349718fd37a7SXin LIwhich is a single letter preceded by @samp{-}, and the other of which 349818fd37a7SXin LIis a long name preceded by @samp{--}. Multiple single letter options 349918fd37a7SXin LI(unless they take an argument) can be combined into a single command 350018fd37a7SXin LIline word: @option{-bl} is equivalent to @option{-b -l}. 350118fd37a7SXin LI 350218fd37a7SXin LI@table @option 350318fd37a7SXin LI@item -b 350418fd37a7SXin LI@itemx --print-bytes 350518fd37a7SXin LIPrint the differing bytes. Display control bytes as a 350618fd37a7SXin LI@samp{^} followed by a letter of the alphabet and precede bytes 350718fd37a7SXin LIthat have the high bit set with @samp{M-} (which stands for ``meta''). 350818fd37a7SXin LI 350918fd37a7SXin LI@item --help 351018fd37a7SXin LIOutput a summary of usage and then exit. 351118fd37a7SXin LI 351218fd37a7SXin LI@item -i @var{skip} 351318fd37a7SXin LI@itemx --ignore-initial=@var{skip} 351418fd37a7SXin LIIgnore any differences in the first @var{skip} bytes of the input 351518fd37a7SXin LIfiles. Treat files with fewer than @var{skip} bytes as if they are 351618fd37a7SXin LIempty. If @var{skip} is of the form 351718fd37a7SXin LI@option{@var{from-skip}:@var{to-skip}}, skip the first @var{from-skip} 351818fd37a7SXin LIbytes of the first input file and the first @var{to-skip} bytes of the 351918fd37a7SXin LIsecond. 352018fd37a7SXin LI 352118fd37a7SXin LI@item -l 352218fd37a7SXin LI@itemx --verbose 352318fd37a7SXin LIOutput the (decimal) byte numbers and (octal) values of all differing bytes, 352418fd37a7SXin LIinstead of the default standard output. 352518fd37a7SXin LI 352618fd37a7SXin LI@item -n @var{count} 352718fd37a7SXin LI@itemx --bytes=@var{count} 352818fd37a7SXin LICompare at most @var{count} input bytes. 352918fd37a7SXin LI 353018fd37a7SXin LI@item -s 353118fd37a7SXin LI@itemx --quiet 353218fd37a7SXin LI@itemx --silent 353318fd37a7SXin LIDo not print anything; only return an exit status indicating whether 353418fd37a7SXin LIthe files differ. 353518fd37a7SXin LI 353618fd37a7SXin LI@item -v 353718fd37a7SXin LI@itemx --version 353818fd37a7SXin LIOutput version information and then exit. 353918fd37a7SXin LI@end table 354018fd37a7SXin LI 354118fd37a7SXin LIIn the above table, operands that are byte counts are normally 354218fd37a7SXin LIdecimal, but may be preceded by @samp{0} for octal and @samp{0x} for 354318fd37a7SXin LIhexadecimal. 354418fd37a7SXin LI 354518fd37a7SXin LIA byte count can be followed by a suffix to specify a multiple of that 354618fd37a7SXin LIcount; in this case an omitted integer is understood to be 1. A bare 354718fd37a7SXin LIsize letter, or one followed by @samp{iB}, specifies a multiple using 354818fd37a7SXin LIpowers of 1024. A size letter followed by @samp{B} specifies powers 354918fd37a7SXin LIof 1000 instead. For example, @option{-n 4M} and @option{-n 4MiB} are 355018fd37a7SXin LIequivalent to @option{-n 4194304}, whereas @option{-n 4MB} is 355118fd37a7SXin LIequivalent to @option{-n 4000000}. This notation is upward compatible 355218fd37a7SXin LIwith the @uref{http://www.bipm.fr/enus/3_SI/si-prefixes.html, SI 355318fd37a7SXin LIprefixes} for decimal multiples and with the 355418fd37a7SXin LI@uref{http://physics.nist.gov/cuu/Units/binary.html, IEC 60027-2 355518fd37a7SXin LIprefixes for binary multiples}. 355618fd37a7SXin LI 355718fd37a7SXin LIThe following suffixes are defined. Large sizes like @code{1Y} may be 355818fd37a7SXin LIrejected by your computer due to limitations of its arithmetic. 355918fd37a7SXin LI 356018fd37a7SXin LI@table @samp 356118fd37a7SXin LI@item kB 356218fd37a7SXin LI@cindex kilobyte, definition of 356318fd37a7SXin LIkilobyte: @math{10^3 = 1000}. 356418fd37a7SXin LI@item k 356518fd37a7SXin LI@itemx K 356618fd37a7SXin LI@itemx KiB 356718fd37a7SXin LI@cindex kibibyte, definition of 356818fd37a7SXin LIkibibyte: @math{2^10 = 1024}. @samp{K} is special: the SI prefix is 356918fd37a7SXin LI@samp{k} and the IEC 60027-2 prefix is @samp{Ki}, but tradition and 357018fd37a7SXin LI@acronym{POSIX} use @samp{k} to mean @samp{KiB}. 357118fd37a7SXin LI@item MB 357218fd37a7SXin LI@cindex megabyte, definition of 357318fd37a7SXin LImegabyte: @math{10^6 = 1,000,000}. 357418fd37a7SXin LI@item M 357518fd37a7SXin LI@itemx MiB 357618fd37a7SXin LI@cindex mebibyte, definition of 357718fd37a7SXin LImebibyte: @math{2^20 = 1,048,576}. 357818fd37a7SXin LI@item GB 357918fd37a7SXin LI@cindex gigabyte, definition of 358018fd37a7SXin LIgigabyte: @math{10^9 = 1,000,000,000}. 358118fd37a7SXin LI@item G 358218fd37a7SXin LI@itemx GiB 358318fd37a7SXin LI@cindex gibibyte, definition of 358418fd37a7SXin LIgibibyte: @math{2^30 = 1,073,741,824}. 358518fd37a7SXin LI@item TB 358618fd37a7SXin LI@cindex terabyte, definition of 358718fd37a7SXin LIterabyte: @math{10^12 = 1,000,000,000,000}. 358818fd37a7SXin LI@item T 358918fd37a7SXin LI@itemx TiB 359018fd37a7SXin LI@cindex tebibyte, definition of 359118fd37a7SXin LItebibyte: @math{2^40 = 1,099,511,627,776}. 359218fd37a7SXin LI@item PB 359318fd37a7SXin LI@cindex petabyte, definition of 359418fd37a7SXin LIpetabyte: @math{10^15 = 1,000,000,000,000,000}. 359518fd37a7SXin LI@item P 359618fd37a7SXin LI@itemx PiB 359718fd37a7SXin LI@cindex pebibyte, definition of 359818fd37a7SXin LIpebibyte: @math{2^50 = 1,125,899,906,842,624}. 359918fd37a7SXin LI@item EB 360018fd37a7SXin LI@cindex exabyte, definition of 360118fd37a7SXin LIexabyte: @math{10^18 = 1,000,000,000,000,000,000}. 360218fd37a7SXin LI@item E 360318fd37a7SXin LI@itemx EiB 360418fd37a7SXin LI@cindex exbibyte, definition of 360518fd37a7SXin LIexbibyte: @math{2^60 = 1,152,921,504,606,846,976}. 360618fd37a7SXin LI@item ZB 360718fd37a7SXin LI@cindex zettabyte, definition of 360818fd37a7SXin LIzettabyte: @math{10^21 = 1,000,000,000,000,000,000,000} 360918fd37a7SXin LI@item Z 361018fd37a7SXin LI@itemx ZiB 361118fd37a7SXin LI@math{2^70 = 1,180,591,620,717,411,303,424}. 361218fd37a7SXin LI(@samp{Zi} is a GNU extension to IEC 60027-2.) 361318fd37a7SXin LI@item YB 361418fd37a7SXin LI@cindex yottabyte, definition of 361518fd37a7SXin LIyottabyte: @math{10^24 = 1,000,000,000,000,000,000,000,000}. 361618fd37a7SXin LI@item Y 361718fd37a7SXin LI@itemx YiB 361818fd37a7SXin LI@math{2^80 = 1,208,925,819,614,629,174,706,176}. 361918fd37a7SXin LI(@samp{Yi} is a GNU extension to IEC 60027-2.) 362018fd37a7SXin LI@end table 362118fd37a7SXin LI 362218fd37a7SXin LI@node Invoking diff 362318fd37a7SXin LI@chapter Invoking @command{diff} 362418fd37a7SXin LI@cindex invoking @command{diff} 362518fd37a7SXin LI@cindex @command{diff} invocation 362618fd37a7SXin LI 362718fd37a7SXin LIThe format for running the @command{diff} command is: 362818fd37a7SXin LI 362918fd37a7SXin LI@example 363018fd37a7SXin LIdiff @var{options}@dots{} @var{files}@dots{} 363118fd37a7SXin LI@end example 363218fd37a7SXin LI 363318fd37a7SXin LIIn the simplest case, two file names @var{from-file} and 363418fd37a7SXin LI@var{to-file} are given, and @command{diff} compares the contents of 363518fd37a7SXin LI@var{from-file} and @var{to-file}. A file name of @file{-} stands for 363618fd37a7SXin LItext read from the standard input. As a special case, @samp{diff - -} 363718fd37a7SXin LIcompares a copy of standard input to itself. 363818fd37a7SXin LI 363918fd37a7SXin LIIf one file is a directory and the other is not, @command{diff} compares 364018fd37a7SXin LIthe file in the directory whose name is that of the non-directory. 364118fd37a7SXin LIThe non-directory file must not be @file{-}. 364218fd37a7SXin LI 364318fd37a7SXin LIIf two file names are given and both are directories, 364418fd37a7SXin LI@command{diff} compares corresponding files in both directories, in 364518fd37a7SXin LIalphabetical order; this comparison is not recursive unless the 364618fd37a7SXin LI@option{-r} or @option{--recursive} option is given. @command{diff} never 364718fd37a7SXin LIcompares the actual contents of a directory as if it were a file. The 364818fd37a7SXin LIfile that is fully specified may not be standard input, because standard 364918fd37a7SXin LIinput is nameless and the notion of ``file with the same name'' does not 365018fd37a7SXin LIapply. 365118fd37a7SXin LI 365218fd37a7SXin LIIf the @option{--from-file=@var{file}} option is given, the number of 365318fd37a7SXin LIfile names is arbitrary, and @var{file} is compared to each named file. 365418fd37a7SXin LISimilarly, if the @option{--to-file=@var{file}} option is given, each 365518fd37a7SXin LInamed file is compared to @var{file}. 365618fd37a7SXin LI 365718fd37a7SXin LI@command{diff} options begin with @samp{-}, so normally file names 365818fd37a7SXin LImay not begin with @samp{-}. However, @option{--} as an 365918fd37a7SXin LIargument by itself treats the remaining arguments as file names even if 366018fd37a7SXin LIthey begin with @samp{-}. 366118fd37a7SXin LI 366218fd37a7SXin LIAn exit status of 0 means no differences were found, 1 means some 366318fd37a7SXin LIdifferences were found, and 2 means trouble. Normally, differing 366418fd37a7SXin LIbinary files count as trouble, but this can be altered by using the 366518fd37a7SXin LI@option{-a} or @option{--text} option, or the @option{-q} or 366618fd37a7SXin LI@option{--brief} option. 366718fd37a7SXin LI 366818fd37a7SXin LI@menu 366918fd37a7SXin LI* diff Options:: Summary of options to @command{diff}. 367018fd37a7SXin LI@end menu 367118fd37a7SXin LI 367218fd37a7SXin LI@node diff Options 367318fd37a7SXin LI@section Options to @command{diff} 367418fd37a7SXin LI@cindex @command{diff} options 367518fd37a7SXin LI@cindex options for @command{diff} 367618fd37a7SXin LI 367718fd37a7SXin LIBelow is a summary of all of the options that @acronym{GNU} 367818fd37a7SXin LI@command{diff} accepts. Most options have two equivalent names, one 367918fd37a7SXin LIof which is a single letter preceded by @samp{-}, and the other of 368018fd37a7SXin LIwhich is a long name preceded by @samp{--}. Multiple single letter 368118fd37a7SXin LIoptions (unless they take an argument) can be combined into a single 368218fd37a7SXin LIcommand line word: @option{-ac} is equivalent to @option{-a -c}. Long 368318fd37a7SXin LInamed options can be abbreviated to any unique prefix of their name. 368418fd37a7SXin LIBrackets ([ and ]) indicate that an option takes an optional argument. 368518fd37a7SXin LI 368618fd37a7SXin LI@table @option 368718fd37a7SXin LI@item -a 368818fd37a7SXin LI@itemx --text 368918fd37a7SXin LITreat all files as text and compare them line-by-line, even if they 369018fd37a7SXin LIdo not seem to be text. @xref{Binary}. 369118fd37a7SXin LI 369218fd37a7SXin LI@item -b 369318fd37a7SXin LI@itemx --ignore-space-change 369418fd37a7SXin LIIgnore changes in amount of white space. @xref{White Space}. 369518fd37a7SXin LI 369618fd37a7SXin LI@item -B 369718fd37a7SXin LI@itemx --ignore-blank-lines 369818fd37a7SXin LIIgnore changes that just insert or delete blank lines. @xref{Blank 369918fd37a7SXin LILines}. 370018fd37a7SXin LI 370118fd37a7SXin LI@item --binary 370218fd37a7SXin LIRead and write data in binary mode. @xref{Binary}. 370318fd37a7SXin LI 370418fd37a7SXin LI@item -c 370518fd37a7SXin LIUse the context output format, showing three lines of context. 370618fd37a7SXin LI@xref{Context Format}. 370718fd37a7SXin LI 370818fd37a7SXin LI@item -C @var{lines} 370918fd37a7SXin LI@itemx --context@r{[}=@var{lines}@r{]} 371018fd37a7SXin LIUse the context output format, showing @var{lines} (an integer) lines of 371118fd37a7SXin LIcontext, or three if @var{lines} is not given. @xref{Context Format}. 371218fd37a7SXin LIFor proper operation, @command{patch} typically needs at least two lines of 371318fd37a7SXin LIcontext. 371418fd37a7SXin LI 371518fd37a7SXin LIOn older systems, @command{diff} supports an obsolete option 371618fd37a7SXin LI@option{-@var{lines}} that has effect when combined with @option{-c} 371718fd37a7SXin LIor @option{-p}. @acronym{POSIX} 1003.1-2001 (@pxref{Standards 371818fd37a7SXin LIconformance}) does not allow this; use @option{-C @var{lines}} 371918fd37a7SXin LIinstead. 372018fd37a7SXin LI 372118fd37a7SXin LI@item --changed-group-format=@var{format} 372218fd37a7SXin LIUse @var{format} to output a line group containing differing lines from 372318fd37a7SXin LIboth files in if-then-else format. @xref{Line Group Formats}. 372418fd37a7SXin LI 372518fd37a7SXin LI@item -d 372618fd37a7SXin LI@itemx --minimal 372718fd37a7SXin LIChange the algorithm perhaps find a smaller set of changes. This makes 372818fd37a7SXin LI@command{diff} slower (sometimes much slower). @xref{diff Performance}. 372918fd37a7SXin LI 373018fd37a7SXin LI@item -D @var{name} 373118fd37a7SXin LI@itemx --ifdef=@var{name} 373218fd37a7SXin LIMake merged @samp{#ifdef} format output, conditional on the preprocessor 373318fd37a7SXin LImacro @var{name}. @xref{If-then-else}. 373418fd37a7SXin LI 373518fd37a7SXin LI@item -e 373618fd37a7SXin LI@itemx --ed 373718fd37a7SXin LIMake output that is a valid @command{ed} script. @xref{ed Scripts}. 373818fd37a7SXin LI 373918fd37a7SXin LI@item -E 374018fd37a7SXin LI@itemx --ignore-tab-expansion 374118fd37a7SXin LIIgnore changes due to tab expansion. 374218fd37a7SXin LI@xref{White Space}. 374318fd37a7SXin LI 374418fd37a7SXin LI@item -f 374518fd37a7SXin LI@itemx --forward-ed 374618fd37a7SXin LIMake output that looks vaguely like an @command{ed} script but has changes 374718fd37a7SXin LIin the order they appear in the file. @xref{Forward ed}. 374818fd37a7SXin LI 374918fd37a7SXin LI@item -F @var{regexp} 375018fd37a7SXin LI@itemx --show-function-line=@var{regexp} 375118fd37a7SXin LIIn context and unified format, for each hunk of differences, show some 375218fd37a7SXin LIof the last preceding line that matches @var{regexp}. @xref{Specified 375318fd37a7SXin LIHeadings}. 375418fd37a7SXin LI 375518fd37a7SXin LI@item --from-file=@var{file} 375618fd37a7SXin LICompare @var{file} to each operand; @var{file} may be a directory. 375718fd37a7SXin LI 375818fd37a7SXin LI@item --help 375918fd37a7SXin LIOutput a summary of usage and then exit. 376018fd37a7SXin LI 376118fd37a7SXin LI@item --horizon-lines=@var{lines} 376218fd37a7SXin LIDo not discard the last @var{lines} lines of the common prefix 376318fd37a7SXin LIand the first @var{lines} lines of the common suffix. 376418fd37a7SXin LI@xref{diff Performance}. 376518fd37a7SXin LI 376618fd37a7SXin LI@item -i 376718fd37a7SXin LI@itemx --ignore-case 376818fd37a7SXin LIIgnore changes in case; consider upper- and lower-case letters 376918fd37a7SXin LIequivalent. @xref{Case Folding}. 377018fd37a7SXin LI 377118fd37a7SXin LI@item -I @var{regexp} 377218fd37a7SXin LI@itemx --ignore-matching-lines=@var{regexp} 377318fd37a7SXin LIIgnore changes that just insert or delete lines that match @var{regexp}. 377418fd37a7SXin LI@xref{Specified Lines}. 377518fd37a7SXin LI 377618fd37a7SXin LI@item --ignore-file-name-case 377718fd37a7SXin LIIgnore case when comparing file names during recursive comparison. 377818fd37a7SXin LI@xref{Comparing Directories}. 377918fd37a7SXin LI 378018fd37a7SXin LI@item -l 378118fd37a7SXin LI@itemx --paginate 378218fd37a7SXin LIPass the output through @command{pr} to paginate it. @xref{Pagination}. 378318fd37a7SXin LI 378418fd37a7SXin LI@item --label=@var{label} 378518fd37a7SXin LIUse @var{label} instead of the file name in the context format 378618fd37a7SXin LI(@pxref{Context Format}) and unified format (@pxref{Unified Format}) 378718fd37a7SXin LIheaders. @xref{RCS}. 378818fd37a7SXin LI 378918fd37a7SXin LI@item --left-column 379018fd37a7SXin LIPrint only the left column of two common lines in side by side format. 379118fd37a7SXin LI@xref{Side by Side Format}. 379218fd37a7SXin LI 379318fd37a7SXin LI@item --line-format=@var{format} 379418fd37a7SXin LIUse @var{format} to output all input lines in if-then-else format. 379518fd37a7SXin LI@xref{Line Formats}. 379618fd37a7SXin LI 379718fd37a7SXin LI@item -n 379818fd37a7SXin LI@itemx --rcs 379918fd37a7SXin LIOutput @acronym{RCS}-format diffs; like @option{-f} except that each command 380018fd37a7SXin LIspecifies the number of lines affected. @xref{RCS}. 380118fd37a7SXin LI 380218fd37a7SXin LI@item -N 380318fd37a7SXin LI@itemx --new-file 380418fd37a7SXin LIIn directory comparison, if a file is found in only one directory, 380518fd37a7SXin LItreat it as present but empty in the other directory. @xref{Comparing 380618fd37a7SXin LIDirectories}. 380718fd37a7SXin LI 380818fd37a7SXin LI@item --new-group-format=@var{format} 380918fd37a7SXin LIUse @var{format} to output a group of lines taken from just the second 381018fd37a7SXin LIfile in if-then-else format. @xref{Line Group Formats}. 381118fd37a7SXin LI 381218fd37a7SXin LI@item --new-line-format=@var{format} 381318fd37a7SXin LIUse @var{format} to output a line taken from just the second file in 381418fd37a7SXin LIif-then-else format. @xref{Line Formats}. 381518fd37a7SXin LI 381618fd37a7SXin LI@item --old-group-format=@var{format} 381718fd37a7SXin LIUse @var{format} to output a group of lines taken from just the first 381818fd37a7SXin LIfile in if-then-else format. @xref{Line Group Formats}. 381918fd37a7SXin LI 382018fd37a7SXin LI@item --old-line-format=@var{format} 382118fd37a7SXin LIUse @var{format} to output a line taken from just the first file in 382218fd37a7SXin LIif-then-else format. @xref{Line Formats}. 382318fd37a7SXin LI 382418fd37a7SXin LI@item -p 382518fd37a7SXin LI@itemx --show-c-function 382618fd37a7SXin LIShow which C function each change is in. @xref{C Function Headings}. 382718fd37a7SXin LI 382818fd37a7SXin LI@item -q 382918fd37a7SXin LI@itemx --brief 383018fd37a7SXin LIReport only whether the files differ, not the details of the 383118fd37a7SXin LIdifferences. @xref{Brief}. 383218fd37a7SXin LI 383318fd37a7SXin LI@item -r 383418fd37a7SXin LI@itemx --recursive 383518fd37a7SXin LIWhen comparing directories, recursively compare any subdirectories 383618fd37a7SXin LIfound. @xref{Comparing Directories}. 383718fd37a7SXin LI 383818fd37a7SXin LI@item -s 383918fd37a7SXin LI@itemx --report-identical-files 384018fd37a7SXin LIReport when two files are the same. @xref{Comparing Directories}. 384118fd37a7SXin LI 384218fd37a7SXin LI@item -S @var{file} 384318fd37a7SXin LI@itemx --starting-file=@var{file} 384418fd37a7SXin LIWhen comparing directories, start with the file @var{file}. This is 384518fd37a7SXin LIused for resuming an aborted comparison. @xref{Comparing Directories}. 384618fd37a7SXin LI 384718fd37a7SXin LI@item --speed-large-files 384818fd37a7SXin LIUse heuristics to speed handling of large files that have numerous 384918fd37a7SXin LIscattered small changes. @xref{diff Performance}. 385018fd37a7SXin LI 385118fd37a7SXin LI@item --strip-trailing-cr 385218fd37a7SXin LIStrip any trailing carriage return at the end of an input line. 385318fd37a7SXin LI@xref{Binary}. 385418fd37a7SXin LI 385518fd37a7SXin LI@item --suppress-common-lines 385618fd37a7SXin LIDo not print common lines in side by side format. 385718fd37a7SXin LI@xref{Side by Side Format}. 385818fd37a7SXin LI 385918fd37a7SXin LI@item -t 386018fd37a7SXin LI@itemx --expand-tabs 386118fd37a7SXin LIExpand tabs to spaces in the output, to preserve the alignment of tabs 386218fd37a7SXin LIin the input files. @xref{Tabs}. 386318fd37a7SXin LI 386418fd37a7SXin LI@item -T 386518fd37a7SXin LI@itemx --initial-tab 386618fd37a7SXin LIOutput a tab rather than a space before the text of a line in normal or 386718fd37a7SXin LIcontext format. This causes the alignment of tabs in the line to look 386818fd37a7SXin LInormal. @xref{Tabs}. 386918fd37a7SXin LI 387018fd37a7SXin LI@item --tabsize=@var{columns} 387118fd37a7SXin LIAssume that tab stops are set every @var{columns} (default 8) print 387218fd37a7SXin LIcolumns. @xref{Tabs}. 387318fd37a7SXin LI 387418fd37a7SXin LI@item --to-file=@var{file} 387518fd37a7SXin LICompare each operand to @var{file}; @var{file} may be a directory. 387618fd37a7SXin LI 387718fd37a7SXin LI@item -u 387818fd37a7SXin LIUse the unified output format, showing three lines of context. 387918fd37a7SXin LI@xref{Unified Format}. 388018fd37a7SXin LI 388118fd37a7SXin LI@item --unchanged-group-format=@var{format} 388218fd37a7SXin LIUse @var{format} to output a group of common lines taken from both files 388318fd37a7SXin LIin if-then-else format. @xref{Line Group Formats}. 388418fd37a7SXin LI 388518fd37a7SXin LI@item --unchanged-line-format=@var{format} 388618fd37a7SXin LIUse @var{format} to output a line common to both files in if-then-else 388718fd37a7SXin LIformat. @xref{Line Formats}. 388818fd37a7SXin LI 388918fd37a7SXin LI@item --unidirectional-new-file 389018fd37a7SXin LIWhen comparing directories, if a file appears only in the second 389118fd37a7SXin LIdirectory of the two, treat it as present but empty in the other. 389218fd37a7SXin LI@xref{Comparing Directories}. 389318fd37a7SXin LI 389418fd37a7SXin LI@item -U @var{lines} 389518fd37a7SXin LI@itemx --unified@r{[}=@var{lines}@r{]} 389618fd37a7SXin LIUse the unified output format, showing @var{lines} (an integer) lines of 389718fd37a7SXin LIcontext, or three if @var{lines} is not given. @xref{Unified Format}. 389818fd37a7SXin LIFor proper operation, @command{patch} typically needs at least two lines of 389918fd37a7SXin LIcontext. 390018fd37a7SXin LI 390118fd37a7SXin LIOn older systems, @command{diff} supports an obsolete option 390218fd37a7SXin LI@option{-@var{lines}} that has effect when combined with @option{-u}. 390318fd37a7SXin LI@acronym{POSIX} 1003.1-2001 (@pxref{Standards conformance}) does not allow 390418fd37a7SXin LIthis; use @option{-U @var{lines}} instead. 390518fd37a7SXin LI 390618fd37a7SXin LI@item -v 390718fd37a7SXin LI@itemx --version 390818fd37a7SXin LIOutput version information and then exit. 390918fd37a7SXin LI 391018fd37a7SXin LI@item -w 391118fd37a7SXin LI@itemx --ignore-all-space 391218fd37a7SXin LIIgnore white space when comparing lines. @xref{White Space}. 391318fd37a7SXin LI 391418fd37a7SXin LI@item -W @var{columns} 391518fd37a7SXin LI@itemx --width=@var{columns} 391618fd37a7SXin LIOutput at most @var{columns} (default 130) print columns per line in 391718fd37a7SXin LIside by side format. @xref{Side by Side Format}. 391818fd37a7SXin LI 391918fd37a7SXin LI@item -x @var{pattern} 392018fd37a7SXin LI@itemx --exclude=@var{pattern} 392118fd37a7SXin LIWhen comparing directories, ignore files and subdirectories whose basenames 392218fd37a7SXin LImatch @var{pattern}. @xref{Comparing Directories}. 392318fd37a7SXin LI 392418fd37a7SXin LI@item -X @var{file} 392518fd37a7SXin LI@itemx --exclude-from=@var{file} 392618fd37a7SXin LIWhen comparing directories, ignore files and subdirectories whose basenames 392718fd37a7SXin LImatch any pattern contained in @var{file}. @xref{Comparing Directories}. 392818fd37a7SXin LI 392918fd37a7SXin LI@item -y 393018fd37a7SXin LI@itemx --side-by-side 393118fd37a7SXin LIUse the side by side output format. @xref{Side by Side Format}. 393218fd37a7SXin LI@end table 393318fd37a7SXin LI 393418fd37a7SXin LI@node Invoking diff3 393518fd37a7SXin LI@chapter Invoking @command{diff3} 393618fd37a7SXin LI@cindex invoking @command{diff3} 393718fd37a7SXin LI@cindex @command{diff3} invocation 393818fd37a7SXin LI 393918fd37a7SXin LIThe @command{diff3} command compares three files and outputs descriptions 394018fd37a7SXin LIof their differences. Its arguments are as follows: 394118fd37a7SXin LI 394218fd37a7SXin LI@example 394318fd37a7SXin LIdiff3 @var{options}@dots{} @var{mine} @var{older} @var{yours} 394418fd37a7SXin LI@end example 394518fd37a7SXin LI 394618fd37a7SXin LIThe files to compare are @var{mine}, @var{older}, and @var{yours}. 394718fd37a7SXin LIAt most one of these three file names may be @file{-}, 394818fd37a7SXin LIwhich tells @command{diff3} to read the standard input for that file. 394918fd37a7SXin LI 395018fd37a7SXin LIAn exit status of 0 means @command{diff3} was successful, 1 means some 395118fd37a7SXin LIconflicts were found, and 2 means trouble. 395218fd37a7SXin LI 395318fd37a7SXin LI@menu 395418fd37a7SXin LI* diff3 Options:: Summary of options to @command{diff3}. 395518fd37a7SXin LI@end menu 395618fd37a7SXin LI 395718fd37a7SXin LI@node diff3 Options 395818fd37a7SXin LI@section Options to @command{diff3} 395918fd37a7SXin LI@cindex @command{diff3} options 396018fd37a7SXin LI@cindex options for @command{diff3} 396118fd37a7SXin LI 396218fd37a7SXin LIBelow is a summary of all of the options that @acronym{GNU} @command{diff3} 396318fd37a7SXin LIaccepts. Multiple single letter options (unless they take an argument) 396418fd37a7SXin LIcan be combined into a single command line argument. 396518fd37a7SXin LI 396618fd37a7SXin LI@table @option 396718fd37a7SXin LI@item -a 396818fd37a7SXin LI@itemx --text 396918fd37a7SXin LITreat all files as text and compare them line-by-line, even if they 397018fd37a7SXin LIdo not appear to be text. @xref{Binary}. 397118fd37a7SXin LI 397218fd37a7SXin LI@item -A 397318fd37a7SXin LI@itemx --show-all 397418fd37a7SXin LIIncorporate all unmerged changes from @var{older} to @var{yours} into 397518fd37a7SXin LI@var{mine}, surrounding conflicts with bracket lines. 397618fd37a7SXin LI@xref{Marking Conflicts}. 397718fd37a7SXin LI 397818fd37a7SXin LI@item --diff-program=@var{program} 397918fd37a7SXin LIUse the compatible comparison program @var{program} to compare files 398018fd37a7SXin LIinstead of @command{diff}. 398118fd37a7SXin LI 398218fd37a7SXin LI@item -e 398318fd37a7SXin LI@itemx --ed 398418fd37a7SXin LIGenerate an @command{ed} script that incorporates all the changes from 398518fd37a7SXin LI@var{older} to @var{yours} into @var{mine}. @xref{Which Changes}. 398618fd37a7SXin LI 398718fd37a7SXin LI@item -E 398818fd37a7SXin LI@itemx --show-overlap 398918fd37a7SXin LILike @option{-e}, except bracket lines from overlapping changes' first 399018fd37a7SXin LIand third files. 399118fd37a7SXin LI@xref{Marking Conflicts}. 399218fd37a7SXin LIWith @option{-E}, an overlapping change looks like this: 399318fd37a7SXin LI 399418fd37a7SXin LI@example 399518fd37a7SXin LI<<<<<<< @var{mine} 399618fd37a7SXin LI@r{lines from @var{mine}} 399718fd37a7SXin LI======= 399818fd37a7SXin LI@r{lines from @var{yours}} 399918fd37a7SXin LI>>>>>>> @var{yours} 400018fd37a7SXin LI@end example 400118fd37a7SXin LI 400218fd37a7SXin LI@item --help 400318fd37a7SXin LIOutput a summary of usage and then exit. 400418fd37a7SXin LI 400518fd37a7SXin LI@item -i 400618fd37a7SXin LIGenerate @samp{w} and @samp{q} commands at the end of the @command{ed} 400718fd37a7SXin LIscript for System V compatibility. This option must be combined with 400818fd37a7SXin LIone of the @option{-AeExX3} options, and may not be combined with @option{-m}. 400918fd37a7SXin LI@xref{Saving the Changed File}. 401018fd37a7SXin LI 401118fd37a7SXin LI@item --label=@var{label} 401218fd37a7SXin LIUse the label @var{label} for the brackets output by the @option{-A}, 401318fd37a7SXin LI@option{-E} and @option{-X} options. This option may be given up to three 401418fd37a7SXin LItimes, one for each input file. The default labels are the names of 401518fd37a7SXin LIthe input files. Thus @samp{diff3 --label X --label Y --label Z -m A 401618fd37a7SXin LIB C} acts like 401718fd37a7SXin LI@samp{diff3 -m A B C}, except that the output looks like it came from 401818fd37a7SXin LIfiles named @samp{X}, @samp{Y} and @samp{Z} rather than from files 401918fd37a7SXin LInamed @samp{A}, @samp{B} and @samp{C}. @xref{Marking Conflicts}. 402018fd37a7SXin LI 402118fd37a7SXin LI@item -m 402218fd37a7SXin LI@itemx --merge 402318fd37a7SXin LIApply the edit script to the first file and send the result to standard 402418fd37a7SXin LIoutput. Unlike piping the output from @command{diff3} to @command{ed}, this 402518fd37a7SXin LIworks even for binary files and incomplete lines. @option{-A} is assumed 402618fd37a7SXin LIif no edit script option is specified. @xref{Bypassing ed}. 402718fd37a7SXin LI 402818fd37a7SXin LI@item --strip-trailing-cr 402918fd37a7SXin LIStrip any trailing carriage return at the end of an input line. 403018fd37a7SXin LI@xref{Binary}. 403118fd37a7SXin LI 403218fd37a7SXin LI@item -T 403318fd37a7SXin LI@itemx --initial-tab 403418fd37a7SXin LIOutput a tab rather than two spaces before the text of a line in normal format. 403518fd37a7SXin LIThis causes the alignment of tabs in the line to look normal. @xref{Tabs}. 403618fd37a7SXin LI 403718fd37a7SXin LI@item -v 403818fd37a7SXin LI@itemx --version 403918fd37a7SXin LIOutput version information and then exit. 404018fd37a7SXin LI 404118fd37a7SXin LI@item -x 404218fd37a7SXin LI@itemx --overlap-only 404318fd37a7SXin LILike @option{-e}, except output only the overlapping changes. 404418fd37a7SXin LI@xref{Which Changes}. 404518fd37a7SXin LI 404618fd37a7SXin LI@item -X 404718fd37a7SXin LILike @option{-E}, except output only the overlapping changes. 404818fd37a7SXin LIIn other words, like @option{-x}, except bracket changes as in @option{-E}. 404918fd37a7SXin LI@xref{Marking Conflicts}. 405018fd37a7SXin LI 405118fd37a7SXin LI@item -3 405218fd37a7SXin LI@itemx --easy-only 405318fd37a7SXin LILike @option{-e}, except output only the nonoverlapping changes. 405418fd37a7SXin LI@xref{Which Changes}. 405518fd37a7SXin LI@end table 405618fd37a7SXin LI 405718fd37a7SXin LI@node Invoking patch 405818fd37a7SXin LI@chapter Invoking @command{patch} 405918fd37a7SXin LI@cindex invoking @command{patch} 406018fd37a7SXin LI@cindex @command{patch} invocation 406118fd37a7SXin LI 406218fd37a7SXin LINormally @command{patch} is invoked like this: 406318fd37a7SXin LI 406418fd37a7SXin LI@example 406518fd37a7SXin LIpatch <@var{patchfile} 406618fd37a7SXin LI@end example 406718fd37a7SXin LI 406818fd37a7SXin LIThe full format for invoking @command{patch} is: 406918fd37a7SXin LI 407018fd37a7SXin LI@example 407118fd37a7SXin LIpatch @var{options}@dots{} @r{[}@var{origfile} @r{[}@var{patchfile}@r{]}@r{]} 407218fd37a7SXin LI@end example 407318fd37a7SXin LI 407418fd37a7SXin LIYou can also specify where to read the patch from with the @option{-i 407518fd37a7SXin LI@var{patchfile}} or @option{--input=@var{patchfile}} option. 407618fd37a7SXin LIIf you do not specify @var{patchfile}, or if @var{patchfile} is 407718fd37a7SXin LI@file{-}, @command{patch} reads the patch (that is, the @command{diff} output) 407818fd37a7SXin LIfrom the standard input. 407918fd37a7SXin LI 408018fd37a7SXin LIIf you do not specify an input file on the command line, @command{patch} 408118fd37a7SXin LItries to intuit from the @dfn{leading text} (any text in the patch 408218fd37a7SXin LIthat comes before the @command{diff} output) which file to edit. 408318fd37a7SXin LI@xref{Multiple Patches}. 408418fd37a7SXin LI 408518fd37a7SXin LIBy default, @command{patch} replaces the original input file with the 408618fd37a7SXin LIpatched version, possibly after renaming the original file into a 408718fd37a7SXin LIbackup file (@pxref{Backup Names}, for a description of how 408818fd37a7SXin LI@command{patch} names backup files). You can also specify where to 408918fd37a7SXin LIput the output with the @option{-o @var{file}} or 409018fd37a7SXin LI@option{--output=@var{file}} option; however, do not use this option 409118fd37a7SXin LIif @var{file} is one of the input files. 409218fd37a7SXin LI 409318fd37a7SXin LI@menu 409418fd37a7SXin LI* patch Options:: Summary table of options to @command{patch}. 409518fd37a7SXin LI@end menu 409618fd37a7SXin LI 409718fd37a7SXin LI@node patch Options 409818fd37a7SXin LI@section Options to @command{patch} 409918fd37a7SXin LI@cindex @command{patch} options 410018fd37a7SXin LI@cindex options for @command{patch} 410118fd37a7SXin LI 410218fd37a7SXin LIHere is a summary of all of the options that @acronym{GNU} @command{patch} 410318fd37a7SXin LIaccepts. @xref{patch and Tradition}, for which of these options are 410418fd37a7SXin LIsafe to use in older versions of @command{patch}. 410518fd37a7SXin LI 410618fd37a7SXin LIMultiple single-letter options that do not take an argument can be 410718fd37a7SXin LIcombined into a single command line argument with only one dash. 410818fd37a7SXin LI 410918fd37a7SXin LI@table @option 411018fd37a7SXin LI@item -b 411118fd37a7SXin LI@itemx --backup 411218fd37a7SXin LIBack up the original contents of each file, even if backups would 411318fd37a7SXin LInormally not be made. @xref{Backups}. 411418fd37a7SXin LI 411518fd37a7SXin LI@item -B @var{prefix} 411618fd37a7SXin LI@itemx --prefix=@var{prefix} 411718fd37a7SXin LIPrepend @var{prefix} to backup file names. @xref{Backup Names}. 411818fd37a7SXin LI 411918fd37a7SXin LI@item --backup-if-mismatch 412018fd37a7SXin LIBack up the original contents of each file if the patch does not 412118fd37a7SXin LIexactly match the file. This is the default behavior when not 412218fd37a7SXin LIconforming to @acronym{POSIX}. @xref{Backups}. 412318fd37a7SXin LI 412418fd37a7SXin LI@item --binary 412518fd37a7SXin LIRead and write all files in binary mode, except for standard output 412618fd37a7SXin LIand @file{/dev/tty}. This option has no effect on 412718fd37a7SXin LI@acronym{POSIX}-conforming systems like @acronym{GNU}/Linux. On systems where 412818fd37a7SXin LIthis option makes a difference, the patch should be generated by 412918fd37a7SXin LI@samp{diff -a --binary}. @xref{Binary}. 413018fd37a7SXin LI 413118fd37a7SXin LI@item -c 413218fd37a7SXin LI@itemx --context 413318fd37a7SXin LIInterpret the patch file as a context diff. @xref{patch Input}. 413418fd37a7SXin LI 413518fd37a7SXin LI@item -d @var{directory} 413618fd37a7SXin LI@itemx --directory=@var{directory} 413718fd37a7SXin LIMake directory @var{directory} the current directory for interpreting 413818fd37a7SXin LIboth file names in the patch file, and file names given as arguments to 413918fd37a7SXin LIother options. @xref{patch Directories}. 414018fd37a7SXin LI 414118fd37a7SXin LI@item -D @var{name} 414218fd37a7SXin LI@itemx --ifdef=@var{name} 414318fd37a7SXin LIMake merged if-then-else output using @var{name}. @xref{If-then-else}. 414418fd37a7SXin LI 414518fd37a7SXin LI@item --dry-run 414618fd37a7SXin LIPrint the results of applying the patches without actually changing 414718fd37a7SXin LIany files. @xref{Dry Runs}. 414818fd37a7SXin LI 414918fd37a7SXin LI@item -e 415018fd37a7SXin LI@itemx --ed 415118fd37a7SXin LIInterpret the patch file as an @command{ed} script. @xref{patch Input}. 415218fd37a7SXin LI 415318fd37a7SXin LI@item -E 415418fd37a7SXin LI@itemx --remove-empty-files 415518fd37a7SXin LIRemove output files that are empty after the patches have been applied. 415618fd37a7SXin LI@xref{Creating and Removing}. 415718fd37a7SXin LI 415818fd37a7SXin LI@item -f 415918fd37a7SXin LI@itemx --force 416018fd37a7SXin LIAssume that the user knows exactly what he or she is doing, and do not 416118fd37a7SXin LIask any questions. @xref{patch Messages}. 416218fd37a7SXin LI 416318fd37a7SXin LI@item -F @var{lines} 416418fd37a7SXin LI@itemx --fuzz=@var{lines} 416518fd37a7SXin LISet the maximum fuzz factor to @var{lines}. @xref{Inexact}. 416618fd37a7SXin LI 416718fd37a7SXin LI@item -g @var{num} 416818fd37a7SXin LI@itemx --get=@var{num} 416918fd37a7SXin LIIf @var{num} is positive, get input files from a revision control 417018fd37a7SXin LIsystem as necessary; if zero, do not get the files; if negative, ask 417118fd37a7SXin LIthe user whether to get the files. @xref{Revision Control}. 417218fd37a7SXin LI 417318fd37a7SXin LI@item --help 417418fd37a7SXin LIOutput a summary of usage and then exit. 417518fd37a7SXin LI 417618fd37a7SXin LI@item -i @var{patchfile} 417718fd37a7SXin LI@itemx --input=@var{patchfile} 417818fd37a7SXin LIRead the patch from @var{patchfile} rather than from standard input. 417918fd37a7SXin LI@xref{patch Options}. 418018fd37a7SXin LI 418118fd37a7SXin LI@item -l 418218fd37a7SXin LI@itemx --ignore-white-space 418318fd37a7SXin LILet any sequence of blanks (spaces or tabs) in the patch file match 418418fd37a7SXin LIany sequence of blanks in the input file. @xref{Changed White Space}. 418518fd37a7SXin LI 418618fd37a7SXin LI@item -n 418718fd37a7SXin LI@itemx --normal 418818fd37a7SXin LIInterpret the patch file as a normal diff. @xref{patch Input}. 418918fd37a7SXin LI 419018fd37a7SXin LI@item -N 419118fd37a7SXin LI@itemx --forward 419218fd37a7SXin LIIgnore patches that @command{patch} thinks are reversed or already applied. 419318fd37a7SXin LISee also @option{-R}. @xref{Reversed Patches}. 419418fd37a7SXin LI 419518fd37a7SXin LI@item --no-backup-if-mismatch 419618fd37a7SXin LIDo not back up the original contents of files. This is the default 419718fd37a7SXin LIbehavior when conforming to @acronym{POSIX}. @xref{Backups}. 419818fd37a7SXin LI 419918fd37a7SXin LI@item -o @var{file} 420018fd37a7SXin LI@itemx --output=@var{file} 420118fd37a7SXin LIUse @var{file} as the output file name. @xref{patch Options}. 420218fd37a7SXin LI 420318fd37a7SXin LI@item -p@var{number} 420418fd37a7SXin LI@itemx --strip=@var{number} 420518fd37a7SXin LISet the file name strip count to @var{number}. @xref{patch Directories}. 420618fd37a7SXin LI 420718fd37a7SXin LI@item --posix 420818fd37a7SXin LIConform to @acronym{POSIX}, as if the @env{POSIXLY_CORRECT} environment 420918fd37a7SXin LIvariable had been set. @xref{patch and POSIX}. 421018fd37a7SXin LI 421118fd37a7SXin LI@item --quoting-style=@var{word} 421218fd37a7SXin LIUse style @var{word} to quote names in diagnostics, as if the 421318fd37a7SXin LI@env{QUOTING_STYLE} environment variable had been set to @var{word}. 421418fd37a7SXin LI@xref{patch Quoting Style}. 421518fd37a7SXin LI 421618fd37a7SXin LI@item -r @var{reject-file} 421718fd37a7SXin LI@itemx --reject-file=@var{reject-file} 421818fd37a7SXin LIUse @var{reject-file} as the reject file name. @xref{Reject Names}. 421918fd37a7SXin LI 422018fd37a7SXin LI@item -R 422118fd37a7SXin LI@itemx --reverse 422218fd37a7SXin LIAssume that this patch was created with the old and new files swapped. 422318fd37a7SXin LI@xref{Reversed Patches}. 422418fd37a7SXin LI 422518fd37a7SXin LI@item -s 422618fd37a7SXin LI@itemx --quiet 422718fd37a7SXin LI@itemx --silent 422818fd37a7SXin LIWork silently unless an error occurs. @xref{patch Messages}. 422918fd37a7SXin LI 423018fd37a7SXin LI@item -t 423118fd37a7SXin LI@itemx --batch 423218fd37a7SXin LIDo not ask any questions. @xref{patch Messages}. 423318fd37a7SXin LI 423418fd37a7SXin LI@item -T 423518fd37a7SXin LI@itemx --set-time 423618fd37a7SXin LISet the modification and access times of patched files from time 423718fd37a7SXin LIstamps given in context diff headers, assuming that the context diff 423818fd37a7SXin LIheaders use local time. @xref{Patching Time Stamps}. 423918fd37a7SXin LI 424018fd37a7SXin LI@item -u 424118fd37a7SXin LI@itemx --unified 424218fd37a7SXin LIInterpret the patch file as a unified diff. @xref{patch Input}. 424318fd37a7SXin LI 424418fd37a7SXin LI@item -v 424518fd37a7SXin LI@itemx --version 424618fd37a7SXin LIOutput version information and then exit. 424718fd37a7SXin LI 424818fd37a7SXin LI@item -V @var{backup-style} 424918fd37a7SXin LI@itemx --version=control=@var{backup-style} 425018fd37a7SXin LISelect the naming convention for backup file names. @xref{Backup Names}. 425118fd37a7SXin LI 425218fd37a7SXin LI@item --verbose 425318fd37a7SXin LIPrint more diagnostics than usual. @xref{patch Messages}. 425418fd37a7SXin LI 425518fd37a7SXin LI@item -x @var{number} 425618fd37a7SXin LI@itemx --debug=@var{number} 425718fd37a7SXin LISet internal debugging flags. Of interest only to @command{patch} 425818fd37a7SXin LIpatchers. 425918fd37a7SXin LI 426018fd37a7SXin LI@item -Y @var{prefix} 426118fd37a7SXin LI@itemx --basename-prefix=@var{prefix} 426218fd37a7SXin LIPrepend @var{prefix} to base names of backup files. @xref{Backup Names}. 426318fd37a7SXin LI 426418fd37a7SXin LI@item -z @var{suffix} 426518fd37a7SXin LI@itemx --suffix=@var{suffix} 426618fd37a7SXin LIUse @var{suffix} as the backup extension instead of @samp{.orig} or 426718fd37a7SXin LI@samp{~}. @xref{Backup Names}. 426818fd37a7SXin LI 426918fd37a7SXin LI@item -Z 427018fd37a7SXin LI@itemx --set-utc 427118fd37a7SXin LISet the modification and access times of patched files from time 427218fd37a7SXin LIstamps given in context diff headers, assuming that the context diff 427318fd37a7SXin LIheaders use @acronym{UTC}. @xref{Patching Time Stamps}. 427418fd37a7SXin LI 427518fd37a7SXin LI@end table 427618fd37a7SXin LI 427718fd37a7SXin LI@node Invoking sdiff 427818fd37a7SXin LI@chapter Invoking @command{sdiff} 427918fd37a7SXin LI@cindex invoking @command{sdiff} 428018fd37a7SXin LI@cindex @command{sdiff} invocation 428118fd37a7SXin LI 428218fd37a7SXin LIThe @command{sdiff} command merges two files and interactively outputs the 428318fd37a7SXin LIresults. Its arguments are as follows: 428418fd37a7SXin LI 428518fd37a7SXin LI@example 428618fd37a7SXin LIsdiff -o @var{outfile} @var{options}@dots{} @var{from-file} @var{to-file} 428718fd37a7SXin LI@end example 428818fd37a7SXin LI 428918fd37a7SXin LIThis merges @var{from-file} with @var{to-file}, with output to @var{outfile}. 429018fd37a7SXin LIIf @var{from-file} is a directory and @var{to-file} is not, @command{sdiff} 429118fd37a7SXin LIcompares the file in @var{from-file} whose file name is that of @var{to-file}, 429218fd37a7SXin LIand vice versa. @var{from-file} and @var{to-file} may not both be 429318fd37a7SXin LIdirectories. 429418fd37a7SXin LI 429518fd37a7SXin LI@command{sdiff} options begin with @samp{-}, so normally @var{from-file} 429618fd37a7SXin LIand @var{to-file} may not begin with @samp{-}. However, @option{--} as an 429718fd37a7SXin LIargument by itself treats the remaining arguments as file names even if 429818fd37a7SXin LIthey begin with @samp{-}. You may not use @file{-} as an input file. 429918fd37a7SXin LI 430018fd37a7SXin LI@command{sdiff} without @option{-o} (or @option{--output}) produces a 430118fd37a7SXin LIside-by-side difference. This usage is obsolete; use the @option{-y} 430218fd37a7SXin LIor @option{--side-by-side} option of @command{diff} instead. 430318fd37a7SXin LI 430418fd37a7SXin LIAn exit status of 0 means no differences were found, 1 means some 430518fd37a7SXin LIdifferences were found, and 2 means trouble. 430618fd37a7SXin LI 430718fd37a7SXin LI@menu 430818fd37a7SXin LI* sdiff Options:: Summary of options to @command{diff}. 430918fd37a7SXin LI@end menu 431018fd37a7SXin LI 431118fd37a7SXin LI@node sdiff Options 431218fd37a7SXin LI@section Options to @command{sdiff} 431318fd37a7SXin LI@cindex @command{sdiff} options 431418fd37a7SXin LI@cindex options for @command{sdiff} 431518fd37a7SXin LI 431618fd37a7SXin LIBelow is a summary of all of the options that @acronym{GNU} 431718fd37a7SXin LI@command{sdiff} accepts. Each option has two equivalent names, one of 431818fd37a7SXin LIwhich is a single letter preceded by @samp{-}, and the other of which 431918fd37a7SXin LIis a long name preceded by @samp{--}. Multiple single letter options 432018fd37a7SXin LI(unless they take an argument) can be combined into a single command 432118fd37a7SXin LIline argument. Long named options can be abbreviated to any unique 432218fd37a7SXin LIprefix of their name. 432318fd37a7SXin LI 432418fd37a7SXin LI@table @option 432518fd37a7SXin LI@item -a 432618fd37a7SXin LI@itemx --text 432718fd37a7SXin LITreat all files as text and compare them line-by-line, even if they 432818fd37a7SXin LIdo not appear to be text. @xref{Binary}. 432918fd37a7SXin LI 433018fd37a7SXin LI@item -b 433118fd37a7SXin LI@itemx --ignore-space-change 433218fd37a7SXin LIIgnore changes in amount of white space. @xref{White Space}. 433318fd37a7SXin LI 433418fd37a7SXin LI@item -B 433518fd37a7SXin LI@itemx --ignore-blank-lines 433618fd37a7SXin LIIgnore changes that just insert or delete blank lines. @xref{Blank 433718fd37a7SXin LILines}. 433818fd37a7SXin LI 433918fd37a7SXin LI@item -d 434018fd37a7SXin LI@itemx --minimal 434118fd37a7SXin LIChange the algorithm to perhaps find a smaller set of changes. This 434218fd37a7SXin LImakes @command{sdiff} slower (sometimes much slower). @xref{diff 434318fd37a7SXin LIPerformance}. 434418fd37a7SXin LI 434518fd37a7SXin LI@item --diff-program=@var{program} 434618fd37a7SXin LIUse the compatible comparison program @var{program} to compare files 434718fd37a7SXin LIinstead of @command{diff}. 434818fd37a7SXin LI 434918fd37a7SXin LI@item -E 435018fd37a7SXin LI@itemx --ignore-tab-expansion 435118fd37a7SXin LIIgnore changes due to tab expansion. 435218fd37a7SXin LI@xref{White Space}. 435318fd37a7SXin LI 435418fd37a7SXin LI@item --help 435518fd37a7SXin LIOutput a summary of usage and then exit. 435618fd37a7SXin LI 435718fd37a7SXin LI@item -i 435818fd37a7SXin LI@itemx --ignore-case 435918fd37a7SXin LIIgnore changes in case; consider upper- and lower-case to be the same. 436018fd37a7SXin LI@xref{Case Folding}. 436118fd37a7SXin LI 436218fd37a7SXin LI@item -I @var{regexp} 436318fd37a7SXin LI@itemx --ignore-matching-lines=@var{regexp} 436418fd37a7SXin LIIgnore changes that just insert or delete lines that match @var{regexp}. 436518fd37a7SXin LI@xref{Specified Lines}. 436618fd37a7SXin LI 436718fd37a7SXin LI@item -l 436818fd37a7SXin LI@itemx --left-column 436918fd37a7SXin LIPrint only the left column of two common lines. 437018fd37a7SXin LI@xref{Side by Side Format}. 437118fd37a7SXin LI 437218fd37a7SXin LI@item -o @var{file} 437318fd37a7SXin LI@itemx --output=@var{file} 437418fd37a7SXin LIPut merged output into @var{file}. This option is required for merging. 437518fd37a7SXin LI 437618fd37a7SXin LI@item -s 437718fd37a7SXin LI@itemx --suppress-common-lines 437818fd37a7SXin LIDo not print common lines. @xref{Side by Side Format}. 437918fd37a7SXin LI 438018fd37a7SXin LI@item --speed-large-files 438118fd37a7SXin LIUse heuristics to speed handling of large files that have numerous 438218fd37a7SXin LIscattered small changes. @xref{diff Performance}. 438318fd37a7SXin LI 438418fd37a7SXin LI@item --strip-trailing-cr 438518fd37a7SXin LIStrip any trailing carriage return at the end of an input line. 438618fd37a7SXin LI@xref{Binary}. 438718fd37a7SXin LI 438818fd37a7SXin LI@item -t 438918fd37a7SXin LI@itemx --expand-tabs 439018fd37a7SXin LIExpand tabs to spaces in the output, to preserve the alignment of tabs 439118fd37a7SXin LIin the input files. @xref{Tabs}. 439218fd37a7SXin LI 439318fd37a7SXin LI@item --tabsize=@var{columns} 439418fd37a7SXin LIAssume that tab stops are set every @var{columns} (default 8) print 439518fd37a7SXin LIcolumns. @xref{Tabs}. 439618fd37a7SXin LI 439718fd37a7SXin LI@item -v 439818fd37a7SXin LI@itemx --version 439918fd37a7SXin LIOutput version information and then exit. 440018fd37a7SXin LI 440118fd37a7SXin LI@item -w @var{columns} 440218fd37a7SXin LI@itemx --width=@var{columns} 440318fd37a7SXin LIOutput at most @var{columns} (default 130) print columns per line. 440418fd37a7SXin LI@xref{Side by Side Format}. Note that for historical reasons, this 440518fd37a7SXin LIoption is @option{-W} in @command{diff}, @option{-w} in @command{sdiff}. 440618fd37a7SXin LI 440718fd37a7SXin LI@item -W 440818fd37a7SXin LI@itemx --ignore-all-space 440918fd37a7SXin LIIgnore white space when comparing lines. @xref{White Space}. 441018fd37a7SXin LINote that for historical reasons, this option is @option{-w} in @command{diff}, 441118fd37a7SXin LI@option{-W} in @command{sdiff}. 441218fd37a7SXin LI@end table 441318fd37a7SXin LI 441418fd37a7SXin LI@node Standards conformance 441518fd37a7SXin LI@chapter Standards conformance 441618fd37a7SXin LI@cindex @acronym{POSIX} 441718fd37a7SXin LI 441818fd37a7SXin LI@vindex POSIXLY_CORRECT 441918fd37a7SXin LIIn a few cases, the @acronym{GNU} utilities' default behavior is 442018fd37a7SXin LIincompatible with the @acronym{POSIX} standard. To suppress these 442118fd37a7SXin LIincompatibilities, define the @env{POSIXLY_CORRECT} environment 442218fd37a7SXin LIvariable. Unless you are checking for @acronym{POSIX} conformance, you 442318fd37a7SXin LIprobably do not need to define @env{POSIXLY_CORRECT}. 442418fd37a7SXin LI 442518fd37a7SXin LINormally options and operands can appear in any order, and programs act 442618fd37a7SXin LIas if all the options appear before any operands. For example, 442718fd37a7SXin LI@samp{diff lao tzu -C 2} acts like @samp{diff -C 2 lao tzu}, since 442818fd37a7SXin LI@samp{2} is an option-argument of @option{-C}. However, if the 442918fd37a7SXin LI@env{POSIXLY_CORRECT} environment variable is set, options must appear 443018fd37a7SXin LIbefore operands, unless otherwise specified for a particular command. 443118fd37a7SXin LI 443218fd37a7SXin LINewer versions of @acronym{POSIX} are occasionally incompatible with older 443318fd37a7SXin LIversions. For example, older versions of @acronym{POSIX} allowed the 443418fd37a7SXin LIcommand @samp{diff -c -10} to have the same meaning as @samp{diff -C 443518fd37a7SXin LI10}, but @acronym{POSIX} 1003.1-2001 @samp{diff} no longer allows 443618fd37a7SXin LIdigit-string options like @option{-10}. 443718fd37a7SXin LI 443818fd37a7SXin LI@vindex _POSIX2_VERSION 443918fd37a7SXin LIThe @acronym{GNU} utilities normally conform to the version of @acronym{POSIX} 444018fd37a7SXin LIthat is standard for your system. To cause them to conform to a 444118fd37a7SXin LIdifferent version of @acronym{POSIX}, define the @env{_POSIX2_VERSION} 444218fd37a7SXin LIenvironment variable to a value of the form @var{yyyymm} specifying 444318fd37a7SXin LIthe year and month the standard was adopted. Two values are currently 444418fd37a7SXin LIsupported for @env{_POSIX2_VERSION}: @samp{199209} stands for 444518fd37a7SXin LI@acronym{POSIX} 1003.2-1992, and @samp{200112} stands for @acronym{POSIX} 444618fd37a7SXin LI1003.1-2001. For example, if you are running older software that 444718fd37a7SXin LIassumes an older version of @acronym{POSIX} and uses @samp{diff -c -10}, 444818fd37a7SXin LIyou can work around the compatibility problems by setting 444918fd37a7SXin LI@samp{_POSIX2_VERSION=199209} in your environment. 445018fd37a7SXin LI 445118fd37a7SXin LI@node Projects 445218fd37a7SXin LI@chapter Future Projects 445318fd37a7SXin LI 445418fd37a7SXin LIHere are some ideas for improving @acronym{GNU} @command{diff} and 445518fd37a7SXin LI@command{patch}. The @acronym{GNU} project has identified some 445618fd37a7SXin LIimprovements as potential programming projects for volunteers. You 445718fd37a7SXin LIcan also help by reporting any bugs that you find. 445818fd37a7SXin LI 445918fd37a7SXin LIIf you are a programmer and would like to contribute something to the 446018fd37a7SXin LI@acronym{GNU} project, please consider volunteering for one of these 446118fd37a7SXin LIprojects. If you are seriously contemplating work, please write to 446218fd37a7SXin LI@email{gvc@@gnu.org} to coordinate with other volunteers. 446318fd37a7SXin LI 446418fd37a7SXin LI@menu 446518fd37a7SXin LI* Shortcomings:: Suggested projects for improvements. 446618fd37a7SXin LI* Bugs:: Reporting bugs. 446718fd37a7SXin LI@end menu 446818fd37a7SXin LI 446918fd37a7SXin LI@node Shortcomings 447018fd37a7SXin LI@section Suggested Projects for Improving @acronym{GNU} @command{diff} and @command{patch} 447118fd37a7SXin LI@cindex projects for directories 447218fd37a7SXin LI 447318fd37a7SXin LIOne should be able to use @acronym{GNU} @command{diff} to generate a 447418fd37a7SXin LIpatch from any pair of directory trees, and given the patch and a copy 447518fd37a7SXin LIof one such tree, use @command{patch} to generate a faithful copy of 447618fd37a7SXin LIthe other. Unfortunately, some changes to directory trees cannot be 447718fd37a7SXin LIexpressed using current patch formats; also, @command{patch} does not 447818fd37a7SXin LIhandle some of the existing formats. These shortcomings motivate the 447918fd37a7SXin LIfollowing suggested projects. 448018fd37a7SXin LI 448118fd37a7SXin LI@menu 448218fd37a7SXin LI* Internationalization:: Handling multibyte and varying-width characters. 448318fd37a7SXin LI* Changing Structure:: Handling changes to the directory structure. 448418fd37a7SXin LI* Special Files:: Handling symbolic links, device special files, etc. 448518fd37a7SXin LI* Unusual File Names:: Handling file names that contain unusual characters. 448618fd37a7SXin LI* Time Stamp Order:: Outputting diffs in time stamp order. 448718fd37a7SXin LI* Ignoring Changes:: Ignoring certain changes while showing others. 448818fd37a7SXin LI* Speedups:: Improving performance. 448918fd37a7SXin LI@end menu 449018fd37a7SXin LI 449118fd37a7SXin LI@node Internationalization 449218fd37a7SXin LI@subsection Handling Multibyte and Varying-Width Characters 449318fd37a7SXin LI@cindex multibyte characters 449418fd37a7SXin LI@cindex varying-width characters 449518fd37a7SXin LI 449618fd37a7SXin LI@command{diff}, @command{diff3} and @command{sdiff} treat each line of 449718fd37a7SXin LIinput as a string of unibyte characters. This can mishandle multibyte 449818fd37a7SXin LIcharacters in some cases. For example, when asked to ignore spaces, 449918fd37a7SXin LI@command{diff} does not properly ignore a multibyte space character. 450018fd37a7SXin LI 450118fd37a7SXin LIAlso, @command{diff} currently assumes that each byte is one column 450218fd37a7SXin LIwide, and this assumption is incorrect in some locales, e.g., locales 450318fd37a7SXin LIthat use UTF-8 encoding. This causes problems with the @option{-y} or 450418fd37a7SXin LI@option{--side-by-side} option of @command{diff}. 450518fd37a7SXin LI 450618fd37a7SXin LIThese problems need to be fixed without unduly affecting the 450718fd37a7SXin LIperformance of the utilities in unibyte environments. 450818fd37a7SXin LI 450918fd37a7SXin LIThe IBM GNU/Linux Technology Center Internationalization Team has 451018fd37a7SXin LIproposed 451118fd37a7SXin LI@uref{http://oss.software.ibm.com/developer/opensource/linux/patches/i18n/diffutils-2.7.2-i18n-0.1.patch.gz,patches 451218fd37a7SXin LIto support internationalized @command{diff}}. 451318fd37a7SXin LIUnfortunately, these patches are incomplete and are to an older 451418fd37a7SXin LIversion of @command{diff}, so more work needs to be done in this area. 451518fd37a7SXin LI 451618fd37a7SXin LI@node Changing Structure 451718fd37a7SXin LI@subsection Handling Changes to the Directory Structure 451818fd37a7SXin LI@cindex directory structure changes 451918fd37a7SXin LI 452018fd37a7SXin LI@command{diff} and @command{patch} do not handle some changes to directory 452118fd37a7SXin LIstructure. For example, suppose one directory tree contains a directory 452218fd37a7SXin LInamed @samp{D} with some subsidiary files, and another contains a file 452318fd37a7SXin LIwith the same name @samp{D}. @samp{diff -r} does not output enough 452418fd37a7SXin LIinformation for @command{patch} to transform the directory subtree into 452518fd37a7SXin LIthe file. 452618fd37a7SXin LI 452718fd37a7SXin LIThere should be a way to specify that a file has been removed without 452818fd37a7SXin LIhaving to include its entire contents in the patch file. There should 452918fd37a7SXin LIalso be a way to tell @command{patch} that a file was renamed, even if 453018fd37a7SXin LIthere is no way for @command{diff} to generate such information. 453118fd37a7SXin LIThere should be a way to tell @command{patch} that a file's time stamp 453218fd37a7SXin LIhas changed, even if its contents have not changed. 453318fd37a7SXin LI 453418fd37a7SXin LIThese problems can be fixed by extending the @command{diff} output format 453518fd37a7SXin LIto represent changes in directory structure, and extending @command{patch} 453618fd37a7SXin LIto understand these extensions. 453718fd37a7SXin LI 453818fd37a7SXin LI@node Special Files 453918fd37a7SXin LI@subsection Files that are Neither Directories Nor Regular Files 454018fd37a7SXin LI@cindex special files 454118fd37a7SXin LI 454218fd37a7SXin LISome files are neither directories nor regular files: they are unusual 454318fd37a7SXin LIfiles like symbolic links, device special files, named pipes, and 454418fd37a7SXin LIsockets. Currently, @command{diff} treats symbolic links as if they 454518fd37a7SXin LIwere the pointed-to files, except that a recursive @command{diff} 454618fd37a7SXin LIreports an error if it detects infinite loops of symbolic links (e.g., 454718fd37a7SXin LIsymbolic links to @file{..}). @command{diff} treats other special 454818fd37a7SXin LIfiles like regular files if they are specified at the top level, but 454918fd37a7SXin LIsimply reports their presence when comparing directories. This means 455018fd37a7SXin LIthat @command{patch} cannot represent changes to such files. For 455118fd37a7SXin LIexample, if you change which file a symbolic link points to, 455218fd37a7SXin LI@command{diff} outputs the difference between the two files, instead 455318fd37a7SXin LIof the change to the symbolic link. 455418fd37a7SXin LI 455518fd37a7SXin LI@c This might not be a good idea; is it wise for root to install devices 455618fd37a7SXin LI@c this way? 455718fd37a7SXin LI@command{diff} should optionally report changes to special files specially, 455818fd37a7SXin LIand @command{patch} should be extended to understand these extensions. 455918fd37a7SXin LI 456018fd37a7SXin LI@node Unusual File Names 456118fd37a7SXin LI@subsection File Names that Contain Unusual Characters 456218fd37a7SXin LI@cindex file names with unusual characters 456318fd37a7SXin LI 456418fd37a7SXin LIWhen a file name contains an unusual character like a newline or 456518fd37a7SXin LIwhite space, @samp{diff -r} generates a patch that @command{patch} cannot 456618fd37a7SXin LIparse. The problem is with format of @command{diff} output, not just with 456718fd37a7SXin LI@command{patch}, because with odd enough file names one can cause 456818fd37a7SXin LI@command{diff} to generate a patch that is syntactically correct but 456918fd37a7SXin LIpatches the wrong files. The format of @command{diff} output should be 457018fd37a7SXin LIextended to handle all possible file names. 457118fd37a7SXin LI 457218fd37a7SXin LI@node Time Stamp Order 457318fd37a7SXin LI@subsection Outputting Diffs in Time Stamp Order 457418fd37a7SXin LI 457518fd37a7SXin LIApplying @command{patch} to a multiple-file diff can result in files 457618fd37a7SXin LIwhose time stamps are out of order. @acronym{GNU} @command{patch} has 457718fd37a7SXin LIoptions to restore the time stamps of the updated files 457818fd37a7SXin LI(@pxref{Patching Time Stamps}), but sometimes it is useful to generate 457918fd37a7SXin LIa patch that works even if the recipient does not have @acronym{GNU} patch, 458018fd37a7SXin LIor does not use these options. One way to do this would be to 458118fd37a7SXin LIimplement a @command{diff} option to output diffs in time stamp order. 458218fd37a7SXin LI 458318fd37a7SXin LI@node Ignoring Changes 458418fd37a7SXin LI@subsection Ignoring Certain Changes 458518fd37a7SXin LI 458618fd37a7SXin LIIt would be nice to have a feature for specifying two strings, one in 458718fd37a7SXin LI@var{from-file} and one in @var{to-file}, which should be considered to 458818fd37a7SXin LImatch. Thus, if the two strings are @samp{foo} and @samp{bar}, then if 458918fd37a7SXin LItwo lines differ only in that @samp{foo} in file 1 corresponds to 459018fd37a7SXin LI@samp{bar} in file 2, the lines are treated as identical. 459118fd37a7SXin LI 459218fd37a7SXin LIIt is not clear how general this feature can or should be, or 459318fd37a7SXin LIwhat syntax should be used for it. 459418fd37a7SXin LI 459518fd37a7SXin LIA partial substitute is to filter one or both files before comparing, 459618fd37a7SXin LIe.g.: 459718fd37a7SXin LI 459818fd37a7SXin LI@example 459918fd37a7SXin LIsed 's/foo/bar/g' file1 | diff - file2 460018fd37a7SXin LI@end example 460118fd37a7SXin LI 460218fd37a7SXin LIHowever, this outputs the filtered text, not the original. 460318fd37a7SXin LI 460418fd37a7SXin LI@node Speedups 460518fd37a7SXin LI@subsection Improving Performance 460618fd37a7SXin LI 460718fd37a7SXin LIWhen comparing two large directory structures, one of which was 460818fd37a7SXin LIoriginally copied from the other with time stamps preserved (e.g., 460918fd37a7SXin LIwith @samp{cp -pR}), it would greatly improve performance if an option 461018fd37a7SXin LItold @command{diff} to assume that two files with the same size and 461118fd37a7SXin LItime stamps have the same content. @xref{diff Performance}. 461218fd37a7SXin LI 461318fd37a7SXin LI@node Bugs 461418fd37a7SXin LI@section Reporting Bugs 461518fd37a7SXin LI@cindex bug reports 461618fd37a7SXin LI@cindex reporting bugs 461718fd37a7SXin LI 461818fd37a7SXin LIIf you think you have found a bug in @acronym{GNU} @command{cmp}, 461918fd37a7SXin LI@command{diff}, @command{diff3}, or @command{sdiff}, please report it 462018fd37a7SXin LIby electronic mail to the 462118fd37a7SXin LI@uref{http://mail.gnu.org/mailman/listinfo/bug-gnu-utils,GNU utilities 462218fd37a7SXin LIbug report mailing list} @email{bug-gnu-utils@@gnu.org}. Please send 462318fd37a7SXin LIbug reports for @acronym{GNU} @command{patch} to 462418fd37a7SXin LI@email{bug-patch@@gnu.org}. Send as precise a description of the 462518fd37a7SXin LIproblem as you can, including the output of the @option{--version} 462618fd37a7SXin LIoption and sample input files that produce the bug, if applicable. If 462718fd37a7SXin LIyou have a nontrivial fix for the bug, please send it as well. If you 462818fd37a7SXin LIhave a patch, please send it too. It may simplify the maintainer's 462918fd37a7SXin LIjob if the patch is relative to a recent test release, which you can 463018fd37a7SXin LIfind in the directory @uref{ftp://alpha.gnu.org/gnu/diffutils/}. 463118fd37a7SXin LI 463218fd37a7SXin LI@node Copying This Manual 463318fd37a7SXin LI@appendix Copying This Manual 463418fd37a7SXin LI 463518fd37a7SXin LI@menu 463618fd37a7SXin LI* GNU Free Documentation License:: License for copying this manual. 463718fd37a7SXin LI@end menu 463818fd37a7SXin LI 463918fd37a7SXin LI@include fdl.texi 464018fd37a7SXin LI 464118fd37a7SXin LI@node Translations 464218fd37a7SXin LI@appendix Translations of This Manual 464318fd37a7SXin LI 464418fd37a7SXin LINishio Futoshi of the GNUjdoc project has prepared a Japanese 464518fd37a7SXin LItranslation of this manual. Its most recent version can be found at 464618fd37a7SXin LI@uref{http://openlab.ring.gr.jp/gnujdoc/cvsweb/cvsweb.cgi/gnujdoc/}. 464718fd37a7SXin LI 464818fd37a7SXin LI@node Index 464918fd37a7SXin LI@appendix Index 465018fd37a7SXin LI 465118fd37a7SXin LI@printindex cp 465218fd37a7SXin LI 465318fd37a7SXin LI@bye 4654