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