xref: /openbsd-src/usr.bin/diff/diff.1 (revision 26da422a584575cba0ba737a764f69ee925b2b54)
1.\" $OpenBSD: diff.1,v 1.3 2003/06/25 03:02:33 tedu Exp $
2.\" Copyright (c) 1980, 1990, 1993
3.\"	The Regents of the University of California.  All rights reserved.
4.\"
5.\" Redistribution and use in source and binary forms, with or without
6.\" modification, are permitted provided that the following conditions
7.\" are met:
8.\" 1. Redistributions of source code must retain the above copyright
9.\"    notice, this list of conditions and the following disclaimer.
10.\" 2. Redistributions in binary form must reproduce the above copyright
11.\"    notice, this list of conditions and the following disclaimer in the
12.\"    documentation and/or other materials provided with the distribution.
13.\" 3. All advertising materials mentioning features or use of this software
14.\"    must display the following acknowledgement:
15.\"	This product includes software developed by the University of
16.\"	California, Berkeley and its contributors.
17.\" 4. Neither the name of the University nor the names of its contributors
18.\"    may be used to endorse or promote products derived from this software
19.\"    without specific prior written permission.
20.\"
21.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31.\" SUCH DAMAGE.
32.\"
33.\"     @(#)diff.1	8.1 (Berkeley) 6/30/93
34.\"
35.Dd June 30, 1993
36.Dt DIFF 1
37.Os BSD 4
38.Sh NAME
39.Nm diff
40.Nd differential file and directory comparator
41.Sh SYNOPSIS
42.Nm diff
43.Op Fl cefhn
44.Op Fl biwt
45.Ar file1 file2
46.Nm diff
47.Op Fl D Ns Ar string
48.Op Fl biw
49.Ar file1 file2
50.Nm diff
51.Op Fl l
52.Op Fl r
53.Op Fl s
54.Op Fl cefhn
55.Op Fl biwt
56.Op Fl S Ns Ar name
57.Ar dir1 dir2
58.Sh DESCRIPTION
59The
60.Nm diff
61utility compares the contents of
62.Ar file1
63and
64.Ar file2
65and writes to the standard output the list of changes necessary to
66convert one file into the other.
67No output is produced if the files are identical.
68.Pp
69Output options (mutually exclusive):
70.Bl -tag -width Ds
71.It Fl c
72produces a diff with lines of context.
73The default is to present 3 lines of context and may be changed, e.g., to 10, by
74.Fl c10 .
75With
76.Fl c
77the output format is modified slightly:
78the output beginning with identification of the files involved and
79their creation dates and then each change is separated
80by a line with a dozen *'s.
81The lines removed from
82.Ar file1
83are marked with `\(mi '; those added to
84.Ar file2
85are marked `+ '.  Lines which are changed from one
86file to the other are marked in both files with `! '.
87Changes which lie within <context> lines of each other are grouped
88together on output.  (This is a change from the previous ``diff -c''
89but the resulting output is usually much easier to interpret.)
90.It Fl e
91produces output in a form suitable as input for the editor utility,
92.Xr ed 1 ,
93which can then be used to convert file1 into file2.
94.Pp
95Extra commands are added to the output when comparing directories with
96.Fl e ,
97so that the result is a
98.Xr sh  1
99script for converting text files which are common to the two directories
100from their state in
101.Ar dir1
102to their state in
103.Ar dir2 .
104.It Fl f
105identical output to that of the
106.Fl e
107flag, but in reverse order.  It cannot
108be digested by
109.Xr ed 1 .
110.It Fl h
111Invokes an alternate algorithm which can handle files of very long lengths.
112There is a trade off. The algorithm can only deal with changes which are
113clearly delimited and brief. Long sections of changes and overlaps will
114confuse it.
115.It Fl n
116produces a script similar to that of
117.Fl e ,
118but in the opposite order and with a count of changed lines on each
119insert or delete command.  This is the form used by
120.Xr rcsdiff  1  .
121.It Fl D Ns Ar string
122creates a merged version of
123.Ar file1
124and
125.Ar file2
126on the standard output, with C preprocessor controls included so that
127a compilation of the result without defining
128.Ar string
129is equivalent
130to compiling
131.Ar file1 ,
132while defining
133.Ar string
134will yield
135.Ar file2 .
136.El
137.Pp
138Comparison options:
139.Bl -tag -width Ds
140.It Fl b
141causes trailing blanks (spaces and tabs) to be ignored, and other
142strings of blanks to compare equal.
143.It Fl i
144ignores the case of letters.  E.g., ``A'' will compare equal to ``a''.
145.It Fl t
146will expand tabs in output lines.  Normal or
147.Fl c
148output adds character(s) to the front of each line which may screw up
149the indentation of the original source lines and make the output listing
150difficult to interpret.  This option will preserve the original source's
151indentation.
152.It Fl w
153is similar to
154.Fl b
155but causes whitespace (blanks and tabs) to be totally ignored.  E.g.,
156``if\ (\ a\ ==\ b\ )'' will compare equal to ``if(a==b)''.
157.El
158.Pp
159Directory comparison options:
160.Bl -tag -width Ds
161.It Fl l
162long output format; each text file
163.Nm diff Ns \'d
164is piped through
165.Xr pr  1
166to paginate it,
167other differences are remembered and summarized
168after all text file differences are reported.
169.It Fl r
170causes application of
171.Nm diff
172recursively to common subdirectories encountered.
173.It Fl s
174causes
175.Nm diff
176to report files which are the same, which are otherwise not mentioned.
177.It Fl S Ns Ar name
178re-starts a directory
179.Nm diff
180in the middle beginning with file
181.Ar name .
182.El
183.Pp
184If both arguments are directories,
185.Nm diff
186sorts the contents of the directories by name, and then runs the
187regular file
188.Nm diff
189algorithm, producing a change list,
190on text files which are different.
191Binary files which differ,
192common subdirectories, and files which appear in only one directory
193are described as such.
194.Pp
195If only one of
196.Ar file1
197and
198.Ar file2
199is a directory,
200.Nm diff
201is applied to the non-directory file and the file contained in
202the directory file with a filename that is the same as the
203last component of the non-directory file.
204.Pp
205If either
206.Ar file1
207or
208.Ar file2
209is
210.Sq Fl ,
211the standard input is
212used in its place.
213.Ss Output Style
214The default (without
215.Fl e ,
216.Fl c ,
217or
218.Fl n
219.\"  -C
220options)
221output contains lines of these forms, where
222.Va XX , YY , ZZ , QQ
223are line numbers respective of file order.
224.Pp
225.Bl -tag -width "XX,YYcZZ,QQ" -compact
226.It Li XX Ns Ic a Ns Li YY
227At (the end of) line
228.Va XX
229of
230.Ar file1 ,
231append the contents
232of line
233.Va YY
234of
235.Ar file2
236to make them equal.
237.It Li XX Ns Ic a Ns Li YY,ZZ
238Same as above, but append the range of lines,
239.Va YY
240through
241.Va ZZ
242of
243.Ar file2
244to line
245.Va XX
246of file1.
247.It Li XX Ns Ic d Ns Li YY
248At line
249.Va XX
250delete
251the line. The value
252.Va YY
253tells to which line the change
254would bring
255.Ar file1
256in line with
257.Ar file1 .
258.It Li XX,YY Ns Ic d Ns Li ZZ
259Delete the range of lines
260.Va XX
261through
262.Va YY
263in
264.Ar file1 .
265.It Li XX Ns Ic c Ns Li YY
266Change the line
267.Va XX
268in
269.Ar file1
270to the line
271.Va YY
272in
273.Ar file2.
274.It Li XX,YY Ns Ic c Ns Li ZZ
275Replace the range of specified lines with the line
276.Va ZZ .
277.It Li XX,YY Ns Ic c Ns Li ZZ,QQ
278Replace the range
279.Va XX , Ns YY
280from
281.Ar file1
282with the range
283.Va ZZ , Ns QQ
284from
285.Ar file2 .
286.El
287.Pp
288These lines resemble
289.Xr ed 1
290subcommands to convert
291.Ar file1
292into
293.Ar file2 .
294The line numbers before the action letters pertain to
295.Ar file1 ;
296those after pertain to
297.Ar file2 .
298Thus, by exchanging
299.Ic a
300for
301.Ic d
302and reading the line in reverse order, one can also
303determine how to convert
304.Ar file2
305into
306.Ar file1 .
307As in
308.Xr ed 1 ,
309identical
310pairs (where num1 = num2) are abbreviated as a single
311number.
312.Sh ENVIRONMENT
313.Bl -tag -width TMPDIR
314.It Ev TMPDIR
315If the environment variable
316.Ev TMPDIR
317exists,
318.Nm diff
319will use the directory specified by
320.Ev TMPDIR
321as the temporary directory.
322.El
323.Sh FILES
324.Bl -tag -width /usr/bin/diffh -compact
325.It Pa /tmp/d?????
326.It Pa /usr/bin/diffh
327Alternate algorithm version (used by option
328.Fl h ) .
329.It Pa /usr/bin/diff
330for directory diffs
331.It Pa /usr/bin/pr
332used by the
333.Fl l
334option.
335.El
336.Sh SEE ALSO
337.Xr cmp 1 ,
338.Xr cc 1 ,
339.Xr comm 1 ,
340.Xr ed 1 ,
341.Xr diff3 1
342.br
343.ne 1i
344.Sh DIAGNOSTICS
345The
346.Nm diff
347utility exits with one of the following values:
348.Pp
349.Bl -tag -width Ds -compact -offset indent
350.It \&0
351No differences were found.
352.It \&1
353Differences were found.
354.It "\&>\&1"
355An error occurred.
356.El
357.Sh BUGS
358The
359.Fl f
360and
361.Fl e
362options
363do not provide special handling for lines on which the
364first and only character is
365.Dq Li \&. .
366This can cause problems for
367.Xr ed 1 .
368.Pp
369When comparing directories with the
370.Fl b ,
371.Fl w
372or
373.Fl i
374options specified,
375.Nm diff
376first compares the files ala
377.Ar cmp ,
378and then decides to run the
379.Nm diff
380algorithm if they are not equal.
381This may cause a small amount of spurious output if the files
382then turn out to be identical because the only differences are
383insignificant white space or case differences.
384.Sh HISTORY
385A
386.Nm
387command appeared in
388.At v6 .
389