xref: /openbsd-src/usr.bin/diff/diff.1 (revision 4ec4b3d54a5517b053ea56c6a0ae89ce93183500)
1.\" $OpenBSD: diff.1,v 1.11 2003/07/06 20:48:59 millert 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 June 25, 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 abitw
41.Oo
42.Fl c | Fl e | Fl f |
43.Fl n | Fl u
44.Oc
45.Ar file1 file2
46.Nm diff
47.Op Fl abitw
48.Op Fl C Ar number
49.Ar file1 file2
50.Nm diff
51.Op Fl abitw
52.Op Fl D Ar string
53.Ar file1 file2
54.Nm diff
55.Op Fl abitw
56.Op Fl U Ar number
57.Ar file1 file2
58.Nm diff
59.Op Fl abiNtw
60.Oo
61.Fl c | Fl e | Fl f |
62.Fl n | Fl u
63.Oc
64.Bk -words
65.Op Fl r
66.Op Fl s
67.Op Fl S Ar name
68.Op Fl X Ar file
69.Op Fl x Ar pattern
70.Ek
71.Ar dir1 dir2
72.Sh DESCRIPTION
73The
74.Nm
75utility compares the contents of
76.Ar file1
77and
78.Ar file2
79and writes to the standard output the list of changes necessary to
80convert one file into the other.
81No output is produced if the files are identical.
82.Pp
83Output options (mutually exclusive):
84.Bl -tag -width Ds
85.It Fl c
86Produces a diff with 3 lines of context.
87With
88.Fl c
89the output format is modified slightly:
90the output beginning with identification of the files involved and
91their creation dates and then each change is separated
92by a line with a dozen *'s.
93The lines removed from
94.Ar file1
95are marked with
96.Sq \-\ \& ;
97those added to
98.Ar file2
99are marked
100.Sq \+\ \& .
101Lines which are changed from one file to the other are marked in
102both files with
103.Sq !\ \& .
104Changes which lie within 3 lines of each other are grouped together on output.
105.It Fl e
106Produces output in a form suitable as input for the editor utility,
107.Xr ed 1 ,
108which can then be used to convert file1 into file2.
109.Pp
110Extra commands are added to the output when comparing directories with
111.Fl e ,
112so that the result is a
113.Xr sh 1
114script for converting text files which are common to the two directories
115from their state in
116.Ar dir1
117to their state in
118.Ar dir2 .
119.It Fl f
120Identical output to that of the
121.Fl e
122flag, but in reverse order.
123It cannot be digested by
124.Xr ed 1 .
125.It Fl n
126Produces a script similar to that of
127.Fl e ,
128but in the opposite order and with a count of changed lines on each
129insert or delete command.
130This is the form used by
131.Xr rcsdiff 1 .
132.It Fl u
133Produces a
134.Em unified
135diff with 3 lines of context.
136A unified diff is similar to the context diff produced by the
137.Fl c
138option.
139However, unlike with
140.Fl c ,
141all lines to be changed (added and/or removed) are present in
142a single section.
143.It Fl C Ar number
144Like
145.Fl c
146but produces a diff with
147.Ar number
148lines of context.
149.It Fl D Ar string
150Creates a merged version of
151.Ar file1
152and
153.Ar file2
154on the standard output, with C preprocessor controls included so that
155a compilation of the result without defining
156.Ar string
157is equivalent to compiling
158.Ar file1 ,
159while defining
160.Ar string
161will yield
162.Ar file2 .
163.It Fl U Ar number
164Like
165.Fl u
166but produces a diff with
167.Ar number
168lines of context.
169.El
170.Pp
171Comparison options:
172.Bl -tag -width Ds
173.It Fl a
174Treat all files as ASCII.
175.It Fl b
176Causes trailing blanks (spaces and tabs) to be ignored, and other
177strings of blanks to compare equal.
178.It Fl i
179Ignores the case of letters.
180E.g.,
181.Dq A
182will compare equal to
183.Dq a .
184.It Fl t
185Will expand tabs in output lines.
186Normal or
187.Fl c
188output adds character(s) to the front of each line which may screw up
189the indentation of the original source lines and make the output listing
190difficult to interpret.
191This option will preserve the original source's indentation.
192.It Fl w
193Is similar to
194.Fl b
195but causes whitespace (blanks and tabs) to be totally ignored.
196E.g.,
197.Dq if (\ \&a == b \&)
198will compare equal to
199.Dq if(a==b) .
200.El
201.Pp
202Directory comparison options:
203.Bl -tag -width Ds
204.It Fl N
205If a file is found in only one directory, act as if it was found in the
206other directory too but was of zero size.
207.It Fl r
208Causes application of
209.Nm
210recursively to common subdirectories encountered.
211.It Fl s
212Causes
213.Nm
214to report files which are the same, which are otherwise not mentioned.
215.It Fl S Ar name
216Re-starts a directory
217.Nm
218in the middle, beginning with file
219.Ar name .
220.It Fl X Ar file
221Exclude files and subdirectories from comparison whose basenames match
222lines in
223.Ar file .
224Multiple
225.Fl X
226options may be specified.
227.It Fl x Ar pattern
228Exclude files and subdirectories from comparison whose basenames match
229.Ar pattern .
230Patterns are matched using shell-style globbing via
231.Xr fnmatch 3 .
232Multiple
233.Fl x
234options may be specified.
235.El
236.Pp
237If both arguments are directories,
238.Nm
239sorts the contents of the directories by name, and then runs the
240regular file
241.Nm
242algorithm, producing a change list,
243on text files which are different.
244Binary files which differ,
245common subdirectories, and files which appear in only one directory
246are described as such.
247.Pp
248If only one of
249.Ar file1
250and
251.Ar file2
252is a directory,
253.Nm
254is applied to the non-directory file and the file contained in
255the directory file with a filename that is the same as the
256last component of the non-directory file.
257.Pp
258If either
259.Ar file1
260or
261.Ar file2
262is
263.Sq Fl ,
264the standard input is
265used in its place.
266.Ss Output Style
267The default (without
268.Fl e ,
269.Fl c ,
270or
271.Fl n
272.\" -C
273options)
274output contains lines of these forms, where
275.Va XX , YY , ZZ , QQ
276are line numbers respective of file order.
277.Pp
278.Bl -tag -width "XX,YYcZZ,QQ" -compact
279.It Li XX Ns Ic a Ns Li YY
280At (the end of) line
281.Va XX
282of
283.Ar file1 ,
284append the contents
285of line
286.Va YY
287of
288.Ar file2
289to make them equal.
290.It Li XX Ns Ic a Ns Li YY,ZZ
291Same as above, but append the range of lines,
292.Va YY
293through
294.Va ZZ
295of
296.Ar file2
297to line
298.Va XX
299of file1.
300.It Li XX Ns Ic d Ns Li YY
301At line
302.Va XX
303delete
304the line.
305The value
306.Va YY
307tells to which line the change would bring
308.Ar file1
309in line with
310.Ar file1 .
311.It Li XX,YY Ns Ic d Ns Li ZZ
312Delete the range of lines
313.Va XX
314through
315.Va YY
316in
317.Ar file1 .
318.It Li XX Ns Ic c Ns Li YY
319Change the line
320.Va XX
321in
322.Ar file1
323to the line
324.Va YY
325in
326.Ar file2 .
327.It Li XX,YY Ns Ic c Ns Li ZZ
328Replace the range of specified lines with the line
329.Va ZZ .
330.It Li XX,YY Ns Ic c Ns Li ZZ,QQ
331Replace the range
332.Va XX , Ns YY
333from
334.Ar file1
335with the range
336.Va ZZ , Ns QQ
337from
338.Ar file2 .
339.El
340.Pp
341These lines resemble
342.Xr ed 1
343subcommands to convert
344.Ar file1
345into
346.Ar file2 .
347The line numbers before the action letters pertain to
348.Ar file1 ;
349those after pertain to
350.Ar file2 .
351Thus, by exchanging
352.Ic a
353for
354.Ic d
355and reading the line in reverse order, one can also
356determine how to convert
357.Ar file2
358into
359.Ar file1 .
360As in
361.Xr ed 1 ,
362identical
363pairs (where num1 = num2) are abbreviated as a single
364number.
365.Sh ENVIRONMENT
366.Bl -tag -width TMPDIR
367.It Ev TMPDIR
368If the environment variable
369.Ev TMPDIR
370exists,
371.Nm
372will use the directory specified by
373.Ev TMPDIR
374as the temporary directory.
375.El
376.Sh FILES
377.Bl -tag -width /tmp/diff1.XXXXXXXX -compact
378.It Pa /tmp/diff1.XXXXXXXX
379Temporary file used when
380.Ar file1
381is a device or stdin.
382.It Pa /tmp/diff2.XXXXXXXX
383Temporary file used when
384.Ar file2
385is a device or stdin.
386.It Pa /usr/bin/diff
387For directory diffs.
388.El
389.Sh DIAGNOSTICS
390The
391.Nm
392utility exits with one of the following values:
393.Pp
394.Bl -tag -width Ds -compact -offset indent
395.It \&0
396No differences were found.
397.It \&1
398Differences were found.
399.It \*[Gt]\&1
400An error occurred.
401.El
402.Sh SEE ALSO
403.Xr cmp 1 ,
404.Xr comm 1 ,
405.Xr diff3 1 ,
406.Xr ed 1 ,
407.Xr fnmatch 3
408.Sh HISTORY
409A
410.Nm
411command appeared in
412.At v6 .
413.Sh BUGS
414The
415.Fl f
416and
417.Fl e
418options
419do not provide special handling for lines on which the
420first and only character is
421.Dq Li \&. .
422This can cause problems for
423.Xr ed 1 .
424.Pp
425When comparing directories with the
426.Fl b ,
427.Fl w
428or
429.Fl i
430options specified,
431.Nm
432first compares the files ala
433.Xr cmp 1 ,
434and then decides to run the
435.Nm
436algorithm if they are not equal.
437This may cause a small amount of spurious output if the files
438then turn out to be identical because the only differences are
439insignificant whitespace or case differences.
440