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