xref: /openbsd-src/usr.bin/diff/diff.1 (revision 0eea0d082377cb9c3ec583313dc4d52b7b6a4d6d)
1.\" $OpenBSD: diff.1,v 1.26 2004/06/20 18:47:45 otto 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 .
190.It Fl b
191Causes trailing blanks (spaces and tabs) to be ignored, and other
192strings of blanks to compare equal.
193.It Fl d
194Try very hard to produce a diff as small as possible.
195This may consume a lot of processing power and memory when processing
196large files with many changes.
197.It Fl I Ar pattern
198Ignores changes, insertions, and deletions whose lines match the
199extended regular expression
200.Ar pattern .
201Multiple
202.Fl I
203patterns may be specified.
204All lines in the change must match some pattern for the change to be
205ignored.
206See
207.Xr re_format 7
208for more information on regular expression patterns.
209.It Fl i
210Ignores the case of letters.
211E.g.,
212.Dq A
213will compare equal to
214.Dq a .
215.It Fl l
216Long output format; each text file
217.Nm diff Ns \'d
218is piped through
219.Xr pr 1
220to paginate it;
221other differences are remembered and summarized
222after all text file differences are reported.
223.It Fl L Ar label
224Print
225.Ar label
226instead of the first file name and time in the context or unified diff header.
227.It Fl p
228With unified and context diffs, show with each change
229the first 40 characters of the last line before the context beginning
230with a letter, an underscore or a dollar sign.
231For C source code following standard layout conventions, this will
232show the prototype of the function the change applies to.
233.It Fl t
234Will expand tabs in output lines.
235Normal or
236.Fl c
237output adds character(s) to the front of each line which may screw up
238the indentation of the original source lines and make the output listing
239difficult to interpret.
240This option will preserve the original source's indentation.
241.It Fl T
242Print a tab rather than a space before the rest of the line for the
243normal, context or unified output formats.
244This makes the alignment of tabs in the line consistent.
245.It Fl w
246Is similar to
247.Fl b
248but causes whitespace (blanks and tabs) to be totally ignored.
249E.g.,
250.Dq if (\ \&a == b \&)
251will compare equal to
252.Dq if(a==b) .
253.El
254.Pp
255Directory comparison options:
256.Bl -tag -width Ds
257.It Fl N
258If a file is found in only one directory, act as if it was found in the
259other directory too but was of zero size.
260.It Fl P
261If a file is found only in
262.Ar dir2 ,
263act as if it was found in
264.Ar dir1
265too but was of zero size.
266.It Fl r
267Causes application of
268.Nm
269recursively to common subdirectories encountered.
270.It Fl s
271Causes
272.Nm
273to report files which are the same, which are otherwise not mentioned.
274.It Fl S Ar name
275Re-starts a directory
276.Nm
277in the middle, beginning with file
278.Ar name .
279.It Fl X Ar file
280Exclude files and subdirectories from comparison whose basenames match
281lines in
282.Ar file .
283Multiple
284.Fl X
285options may be specified.
286.It Fl x Ar pattern
287Exclude files and subdirectories from comparison whose basenames match
288.Ar pattern .
289Patterns are matched using shell-style globbing via
290.Xr fnmatch 3 .
291Multiple
292.Fl x
293options may be specified.
294.El
295.Pp
296If both arguments are directories,
297.Nm
298sorts the contents of the directories by name, and then runs the
299regular file
300.Nm
301algorithm, producing a change list,
302on text files which are different.
303Binary files which differ,
304common subdirectories, and files which appear in only one directory
305are described as such.
306In directory mode only regular files and directories are compared.
307If a non-regular file such as a device special file or
308.Tn FIFO
309is encountered, a diagnostic message is printed.
310.Pp
311If only one of
312.Ar file1
313and
314.Ar file2
315is a directory,
316.Nm
317is applied to the non-directory file and the file contained in
318the directory file with a filename that is the same as the
319last component of the non-directory file.
320.Pp
321If either
322.Ar file1
323or
324.Ar file2
325is
326.Sq Fl ,
327the standard input is
328used in its place.
329.Ss Output Style
330The default (without
331.Fl e ,
332.Fl c ,
333or
334.Fl n
335.\" -C
336options)
337output contains lines of these forms, where
338.Va XX , YY , ZZ , QQ
339are line numbers respective of file order.
340.Pp
341.Bl -tag -width "XX,YYcZZ,QQ" -compact
342.It Li XX Ns Ic a Ns Li YY
343At (the end of) line
344.Va XX
345of
346.Ar file1 ,
347append the contents
348of line
349.Va YY
350of
351.Ar file2
352to make them equal.
353.It Li XX Ns Ic a Ns Li YY,ZZ
354Same as above, but append the range of lines,
355.Va YY
356through
357.Va ZZ
358of
359.Ar file2
360to line
361.Va XX
362of file1.
363.It Li XX Ns Ic d Ns Li YY
364At line
365.Va XX
366delete
367the line.
368The value
369.Va YY
370tells to which line the change would bring
371.Ar file1
372in line with
373.Ar file1 .
374.It Li XX,YY Ns Ic d Ns Li ZZ
375Delete the range of lines
376.Va XX
377through
378.Va YY
379in
380.Ar file1 .
381.It Li XX Ns Ic c Ns Li YY
382Change the line
383.Va XX
384in
385.Ar file1
386to the line
387.Va YY
388in
389.Ar file2 .
390.It Li XX,YY Ns Ic c Ns Li ZZ
391Replace the range of specified lines with the line
392.Va ZZ .
393.It Li XX,YY Ns Ic c Ns Li ZZ,QQ
394Replace the range
395.Va XX , Ns Va YY
396from
397.Ar file1
398with the range
399.Va ZZ , Ns Va QQ
400from
401.Ar file2 .
402.El
403.Pp
404These lines resemble
405.Xr ed 1
406subcommands to convert
407.Ar file1
408into
409.Ar file2 .
410The line numbers before the action letters pertain to
411.Ar file1 ;
412those after pertain to
413.Ar file2 .
414Thus, by exchanging
415.Ic a
416for
417.Ic d
418and reading the line in reverse order, one can also
419determine how to convert
420.Ar file2
421into
422.Ar file1 .
423As in
424.Xr ed 1 ,
425identical
426pairs (where num1 = num2) are abbreviated as a single
427number.
428.Sh ENVIRONMENT
429.Bl -tag -width TMPDIR
430.It Ev TMPDIR
431If the environment variable
432.Ev TMPDIR
433exists,
434.Nm
435will use the directory specified by
436.Ev TMPDIR
437as the temporary directory.
438.El
439.Sh FILES
440.Bl -tag -width /tmp/diff.XXXXXXXX -compact
441.It Pa /tmp/diff. Ns Ar XXXXXXXX
442Temporary file used when comparing a device or the standard input.
443Note that the temporary file is unlinked as soon as it is created
444so it will not show up in a directory listing.
445.El
446.Sh DIAGNOSTICS
447The
448.Nm
449utility exits with one of the following values:
450.Pp
451.Bl -tag -width Ds -compact -offset indent
452.It \&0
453No differences were found.
454.It \&1
455Differences were found.
456.It \*[Gt]\&1
457An error occurred.
458.El
459.Sh SEE ALSO
460.Xr cmp 1 ,
461.Xr comm 1 ,
462.Xr diff3 1 ,
463.Xr ed 1 ,
464.Xr pr 1 ,
465.Xr fnmatch 3 ,
466.Xr re_format 7
467.Sh STANDARDS
468The
469.Nm
470utility is expected to be a superset of the
471.St -p1003.1-2001
472specification.
473.Sh HISTORY
474A
475.Nm
476command appeared in
477.At v6 .
478.Sh BUGS
479When comparing directories with the
480.Fl b ,
481.Fl w
482or
483.Fl i
484options specified,
485.Nm
486first compares the files ala
487.Xr cmp 1 ,
488and then decides to run the
489.Nm
490algorithm if they are not equal.
491This may cause a small amount of spurious output if the files
492then turn out to be identical because the only differences are
493insignificant whitespace or case differences.
494