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