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