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