1.\" $MirOS: src/usr.bin/diff/diff.1,v 1.3 2006/12/21 03:22:53 tg Exp $
2.\" $OpenBSD: diff.1,v 1.28 2005/01/06 00:28:53 jmc Exp $
3.\"
4.\" Copyright (c) 1980, 1990, 1993
5.\"	The Regents of the University of California.  All rights reserved.
6.\"
7.\" Redistribution and use in source and binary forms, with or without
8.\" modification, are permitted provided that the following conditions
9.\" are met:
10.\" 1. Redistributions of source code must retain the above copyright
11.\"    notice, this list of conditions and the following disclaimer.
12.\" 2. Redistributions in binary form must reproduce the above copyright
13.\"    notice, this list of conditions and the following disclaimer in the
14.\"    documentation and/or other materials provided with the distribution.
15.\" 3. Neither the name of the University nor the names of its contributors
16.\"    may be used to endorse or promote products derived from this software
17.\"    without specific prior written permission.
18.\"
19.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29.\" SUCH DAMAGE.
30.\"
31.\"     @(#)diff.1	8.1 (Berkeley) 6/30/93
32.\"
33.Dd July 21, 2003
34.Dt DIFF 1
35.Os
36.Sh NAME
37.Nm diff
38.Nd differential file and directory comparator
39.Sh SYNOPSIS
40.Nm diff
41.Op Fl abdilpqtTw
42.Op Fl I Ar pattern
43.Oo
44.Fl c | e | f |
45.Fl n | u
46.Oc
47.Op Fl L Ar label
48.Ar file1 file2
49.Nm diff
50.Op Fl abdilpqtTw
51.Op Fl I Ar pattern
52.Op Fl L Ar label
53.Fl C Ar number
54.Ar file1 file2
55.Nm diff
56.Op Fl abdilqtw
57.Op Fl I Ar pattern
58.Fl D Ar string
59.Ar file1 file2
60.Nm diff
61.Op Fl abdilpqtTw
62.Op Fl I Ar pattern
63.Op Fl L Ar label
64.Fl U Ar number
65.Ar file1 file2
66.Nm diff
67.Op Fl abdilNPpqtTw
68.Op Fl I Ar pattern
69.Oo
70.Fl c | e | f |
71.Fl n | u
72.Oc
73.Bk -words
74.Op Fl L Ar label
75.Op Fl r
76.Op Fl s
77.Op Fl S Ar name
78.Op Fl X Ar file
79.Op Fl x Ar pattern
80.Ek
81.Ar dir1 dir2
82.Sh DESCRIPTION
83The
84.Nm
85utility compares the contents of
86.Ar file1
87and
88.Ar file2
89and writes to the standard output the list of changes necessary to
90convert one file into the other.
91No output is produced if the files are identical.
92.Pp
93Output options (mutually exclusive):
94.Bl -tag -width Ds
95.It Fl c
96Produces a diff with 3 lines of context.
97With
98.Fl c
99the output format is modified slightly:
100the output begins with identification of the files involved and
101their creation dates and then each change is separated
102by a line with fifteen
103.Li * Ns 's .
104The lines removed from
105.Ar file1
106are marked with
107.Sq \&-\ \& ;
108those added to
109.Ar file2
110are marked
111.Sq \+\ \& .
112Lines which are changed from one file to the other are marked in
113both files with
114.Sq !\ \& .
115Changes which lie within 3 lines of each other are grouped together on
116output.
117.It Fl e
118Produces output in a form suitable as input for the editor utility,
119.Xr ed 1 ,
120which can then be used to convert file1 into file2.
121.Pp
122Extra commands are added to the output when comparing directories with
123.Fl e ,
124so that the result is a
125.Xr sh 1
126script for converting text files which are common to the two directories
127from their state in
128.Ar dir1
129to their state in
130.Ar dir2 .
131.It Fl f
132Identical output to that of the
133.Fl e
134flag, but in reverse order.
135It cannot be digested by
136.Xr ed 1 .
137.It Fl n
138Produces a script similar to that of
139.Fl e ,
140but in the opposite order and with a count of changed lines on each
141insert or delete command.
142This is the form used by
143.Xr rcsdiff 1 .
144.It Fl q
145Just print a line when the files differ.
146Does not output a list of changes.
147.It Fl u
148Produces a
149.Em unified
150diff with 3 lines of context.
151A unified diff is similar to the context diff produced by the
152.Fl c
153option.
154However, unlike with
155.Fl c ,
156all lines to be changed (added and/or removed) are present in
157a single section.
158.It Fl C Ar number
159Like
160.Fl c
161but produces a diff with
162.Ar number
163lines of context.
164.It Fl D Ar string
165Creates a merged version of
166.Ar file1
167and
168.Ar file2
169on the standard output, with C preprocessor controls included so that
170a compilation of the result without defining
171.Ar string
172is equivalent to compiling
173.Ar file1 ,
174while defining
175.Ar string
176will yield
177.Ar file2 .
178.It Fl U Ar number
179Like
180.Fl u
181but produces a diff with
182.Ar number
183lines of context.
184.El
185.Pp
186Comparison options:
187.Bl -tag -width Ds
188.It Fl a
189Treat all files as
190.Tn ASCII
191text.
192Normally
193.Nm
194will simply print
195.Dq Binary files ... differ
196if files contain binary characters.
197Use of this option forces
198.Nm
199to produce a diff.
200.It Fl b
201Causes trailing blanks (spaces and tabs) to be ignored, and other
202strings of blanks to compare equal.
203.It Fl d
204Try very hard to produce a diff as small as possible.
205This may consume a lot of processing power and memory when processing
206large files with many changes.
207This option also changes the modus operandi of
208.Nm
209to behave more like GNU diff wrt a set of relatively near changes.
210.It Fl I Ar pattern
211Ignores changes, insertions, and deletions whose lines match the
212extended regular expression
213.Ar pattern .
214Multiple
215.Fl I
216patterns may be specified.
217All lines in the change must match some pattern for the change to be
218ignored.
219See
220.Xr re_format 7
221for more information on regular expression patterns.
222.It Fl i
223Ignores the case of letters.
224E.g.,
225.Dq A
226will compare equal to
227.Dq a .
228.It Fl l
229Long output format; each text file
230.Nm diff Ns \'d
231is piped through
232.Xr pr 1
233to paginate it;
234other differences are remembered and summarized
235after all text file differences are reported.
236.It Fl L Ar label
237Print
238.Ar label
239instead of the first (and second, if this option is specified twice)
240file name and time in the context or unified diff header.
241.It Fl p
242With unified and context diffs, show with each change
243the first 40 characters of the last line before the context beginning
244with a letter, an underscore or a dollar sign.
245For C source code following standard layout conventions, this will
246show the prototype of the function the change applies to.
247.It Fl t
248Will expand tabs in output lines.
249Normal or
250.Fl c
251output adds character(s) to the front of each line which may screw up
252the indentation of the original source lines and make the output listing
253difficult to interpret.
254This option will preserve the original source's indentation.
255.It Fl T
256Print a tab rather than a space before the rest of the line for the
257normal, context or unified output formats.
258This makes the alignment of tabs in the line consistent.
259.It Fl w
260Is similar to
261.Fl b
262but causes whitespace (blanks and tabs) to be totally ignored.
263E.g.,
264.Dq if (\ \&a == b \&)
265will compare equal to
266.Dq if(a==b) .
267.El
268.Pp
269Directory comparison options:
270.Bl -tag -width Ds
271.It Fl N
272If a file is found in only one directory, act as if it was found in the
273other directory too but was of zero size.
274.It Fl P
275If a file is found only in
276.Ar dir2 ,
277act as if it was found in
278.Ar dir1
279too but was of zero size.
280.It Fl r
281Causes application of
282.Nm
283recursively to common subdirectories encountered.
284.It Fl s
285Causes
286.Nm
287to report files which are the same, which are otherwise not mentioned.
288.It Fl S Ar name
289Re-starts a directory
290.Nm
291in the middle, beginning with file
292.Ar name .
293.It Fl X Ar file
294Exclude files and subdirectories from comparison whose basenames match
295lines in
296.Ar file .
297Multiple
298.Fl X
299options may be specified.
300.It Fl x Ar pattern
301Exclude files and subdirectories from comparison whose basenames match
302.Ar pattern .
303Patterns are matched using shell-style globbing via
304.Xr fnmatch 3 .
305Multiple
306.Fl x
307options may be specified.
308.El
309.Pp
310If both arguments are directories,
311.Nm
312sorts the contents of the directories by name, and then runs the
313regular file
314.Nm
315algorithm, producing a change list,
316on text files which are different.
317Binary files which differ,
318common subdirectories, and files which appear in only one directory
319are described as such.
320In directory mode only regular files and directories are compared.
321If a non-regular file such as a device special file or
322.Tn FIFO
323is encountered, a diagnostic message is printed.
324.Pp
325If only one of
326.Ar file1
327and
328.Ar file2
329is a directory,
330.Nm
331is applied to the non-directory file and the file contained in
332the directory file with a filename that is the same as the
333last component of the non-directory file.
334.Pp
335If either
336.Ar file1
337or
338.Ar file2
339is
340.Sq Fl ,
341the standard input is
342used in its place.
343.Ss Output Style
344The default (without
345.Fl e ,
346.Fl c ,
347or
348.Fl n
349.\" -C
350options)
351output contains lines of these forms, where
352.Va XX , YY , ZZ , QQ
353are line numbers respective of file order.
354.Pp
355.Bl -tag -width "XX,YYcZZ,QQ" -compact
356.It Li XX Ns Ic a Ns Li YY
357At (the end of) line
358.Va XX
359of
360.Ar file1 ,
361append the contents
362of line
363.Va YY
364of
365.Ar file2
366to make them equal.
367.It Li XX Ns Ic a Ns Li YY,ZZ
368Same as above, but append the range of lines,
369.Va YY
370through
371.Va ZZ
372of
373.Ar file2
374to line
375.Va XX
376of file1.
377.It Li XX Ns Ic d Ns Li YY
378At line
379.Va XX
380delete
381the line.
382The value
383.Va YY
384tells to which line the change would bring
385.Ar file1
386in line with
387.Ar file1 .
388.It Li XX,YY Ns Ic d Ns Li ZZ
389Delete the range of lines
390.Va XX
391through
392.Va YY
393in
394.Ar file1 .
395.It Li XX Ns Ic c Ns Li YY
396Change the line
397.Va XX
398in
399.Ar file1
400to the line
401.Va YY
402in
403.Ar file2 .
404.It Li XX,YY Ns Ic c Ns Li ZZ
405Replace the range of specified lines with the line
406.Va ZZ .
407.It Li XX,YY Ns Ic c Ns Li ZZ,QQ
408Replace the range
409.Va XX , Ns Va YY
410from
411.Ar file1
412with the range
413.Va ZZ , Ns Va QQ
414from
415.Ar file2 .
416.El
417.Pp
418These lines resemble
419.Xr ed 1
420subcommands to convert
421.Ar file1
422into
423.Ar file2 .
424The line numbers before the action letters pertain to
425.Ar file1 ;
426those after pertain to
427.Ar file2 .
428Thus, by exchanging
429.Ic a
430for
431.Ic d
432and reading the line in reverse order, one can also
433determine how to convert
434.Ar file2
435into
436.Ar file1 .
437As in
438.Xr ed 1 ,
439identical
440pairs (where num1 = num2) are abbreviated as a single
441number.
442.Sh ENVIRONMENT
443.Bl -tag -width TMPDIR
444.It Ev TMPDIR
445If the environment variable
446.Ev TMPDIR
447exists,
448.Nm
449will use the directory specified by
450.Ev TMPDIR
451as the temporary directory.
452.El
453.Sh FILES
454.Bl -tag -width /tmp/diff.XXXXXXXX -compact
455.It Pa /tmp/diff. Ns Ar XXXXXXXX
456Temporary file used when comparing a device or the standard input.
457Note that the temporary file is unlinked as soon as it is created
458so it will not show up in a directory listing.
459.El
460.Sh DIAGNOSTICS
461The
462.Nm
463utility exits with one of the following values:
464.Pp
465.Bl -tag -width Ds -compact -offset indent
466.It \&0
467No differences were found.
468.It \&1
469Differences were found.
470.It \*(Gt\&1
471An error occurred.
472.El
473.Sh SEE ALSO
474.Xr cmp 1 ,
475.Xr comm 1 ,
476.Xr diff3 1 ,
477.Xr ed 1 ,
478.Xr pr 1 ,
479.Xr fnmatch 3 ,
480.Xr re_format 7
481.Sh STANDARDS
482The
483.Nm
484utility is expected to be a superset of the
485.St -p1003.1-2001
486specification.
487.Sh HISTORY
488A
489.Nm
490command appeared in
491.At v6 .
492.Sh BUGS
493When comparing directories with the
494.Fl b ,
495.Fl w
496or
497.Fl i
498options specified,
499.Nm
500first compares the files ala
501.Xr cmp 1 ,
502and then decides to run the
503.Nm
504algorithm if they are not equal.
505This may cause a small amount of spurious output if the files
506then turn out to be identical because the only differences are
507insignificant whitespace or case differences.
508