xref: /original-bsd/usr.bin/diff/diff/diff.1 (revision 8fbb78b3)
1.\" Copyright (c) 1980, 1990 The Regents of the University of California.
2.\" All rights reserved.
3.\"
4.\" %sccs.include.redist.roff%
5.\"
6.\"     @(#)diff.1	6.7 (Berkeley) 04/24/91
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.Bl -tag -width Ds
43.It 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.It 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.It Fl f
77identical output of the
78.Fl e
79flag, but in reverse order.  It cannot
80be digested by
81.Xr ed 1 .
82.It 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.It 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.It D Ns Ar string
94creates a merged version of
95.Ar file1
96and
97.Ar file2
98on the standard output, with C preprocessor controls included so that
99a compilation of the result without defining
100.Ar string
101is equivalent
102to compiling
103.Ar file1 ,
104while defining
105.Ar string
106will yield
107.Ar file2 .
108.El
109.Pp
110Comparison options:
111.Bl -tag -width Ds
112.It Fl b
113causes trailing blanks (spaces and tabs) to be ignored, and other
114strings of blanks to compare equal.
115.It Fl i
116ignores the case of letters.  E.g., ``A'' will compare equal to ``a''.
117.It Fl t
118will expand tabs in output lines.  Normal or
119.Fl c
120output adds character(s) to the front of each line which may screw up
121the indentation of the original source lines and make the output listing
122difficult to interpret.  This option will preserve the original source's
123indentation.
124.It Fl w
125is similar to
126.Fl b
127but causes whitespace (blanks and tabs) to be totally ignored.  E.g.,
128``if\ (\ a\ ==\ b\ )'' will compare equal to ``if(a==b)''.
129.El
130.Pp
131Directory comparison options:
132.Bl -tag -width Ds
133.It Fl l
134long output format; each text file
135.Nm diff \'d
136is piped through
137.Xr pr  1
138to paginate it,
139other differences are remembered and summarized
140after all text file differences are reported.
141.It Fl r
142causes application of
143.Nm diff
144recursively to common subdirectories encountered.
145.It Fl s
146causes
147.Nm diff
148to report files which are the same, which are otherwise not mentioned.
149.It S Ns Ar name
150re-starts a directory
151.Nm diff
152in the middle beginning with file
153.Ar name .
154.El
155.Pp
156If both arguments are directories,
157.Nm diff
158sorts the contents of the directories by name, and then runs the
159regular file
160.Nm diff
161algorithm, producing a change list,
162on text files which are different.
163Binary files which differ,
164common subdirectories, and files which appear in only one directory
165are described as such.
166.Pp
167If only one of
168.Ar file1
169and
170.Ar file2
171is a directory,
172.Nm diff
173is applied to the non-directory file and the file contained in
174the directory file with a filename that is the same as the
175last component of the non-directory file.
176.Pp
177If either the
178.Ar file1
179or
180.Ar file2
181is
182.Sq Fl ,
183the standard input is
184used in its place.
185.Ss Output Style
186The default (without
187.Fl e ,
188.Fl c ,
189or
190.Fl n
191.\"  -C
192options)
193output contains lines of these forms, where
194.Va XX , YY , ZZ , QQ
195are line numbers respective of file order.
196.Pp
197.Bl -tag -width "XX,YYcZZ,QQ" -compact
198.It Li XX Ns Ic a Ns Li YY
199At (the end of) line
200.Va XX
201of
202.Ar file1 ,
203append the contents
204of line
205.Va YY
206of
207.Ar file2
208to make them equal.
209.It Li XX Ns Ic a Ns Li YY,ZZ
210Same as above, but append the range of lines,
211.Va YY
212through
213.Va ZZ
214of
215.Ar file2
216to line
217.Va XX
218of file1.
219.It Li XX Ns Ic d Ns Li YY
220At line
221.Va XX
222delete
223the line. The value
224.Va YY
225tells to which line the change
226would bring
227.Ar file1
228in line with
229.Ar file1 .
230.It Li XX,YY Ns Ic d Ns Li ZZ
231Delete the range of lines
232.Va XX
233through
234.Va YY
235in
236.Ar file1 .
237.It Li XX Ns Ic c Ns Li YY
238Change the line
239.Va XX
240in
241.Ar file1
242to the line
243.Va YY
244in
245.Ar file2.
246.It Li XX,YY Ns Ic c Ns Li ZZ
247Replace the range of specified lines with the line
248.Va ZZ .
249.It Li XX,YY Ns Ic c Ns Li ZZ,QQ
250Replace the range
251.Va XX , Ns YY
252from
253.Ar file1
254with the range
255.Va ZZ , Ns QQ
256from
257.Ar file2 .
258.El
259.Pp
260These lines resemble
261.Xr ed 1
262subcommands to convert
263.Ar file1
264into
265.Ar file2 .
266The line numbers before the action letters pertain to
267.Ar file1 ;
268those after pertain to
269.Ar file2 .
270Thus, by exchanging
271.Ic a
272for
273.Ic d
274and reading the line in reverse order, one can also
275determine how to convert
276.Ar file2
277into
278.Ar file1 .
279As in
280.Xr ed 1 ,
281identical
282pairs (where num1 = num2) are abbreviated as a single
283number.
284.Sh ENVIRONMENT
285.Bl -tag -width TMPDIR
286.It Ev TMPDIR
287If the environment variable
288.Ev TMPDIR
289exists,
290.Nm diff
291will use the directory specified by
292.Ev TMPDIR
293as the temporary directory.
294.El
295.Sh FILES
296.Bl -tag -width /usr/bin/diffh -compact
297.It Pa /tmp/d?????
298.It Pa /usr/bin/diffh
299Alternate algorithm version (used by option
300.Fl h ) .
301.It Pa /usr/bin/diff
302for directory diffs
303.It Pa /usr/bin/pr
304used by the
305.Fl l
306option.
307.El
308.Sh SEE ALSO
309.Xr cmp 1 ,
310.Xr cc 1 ,
311.Xr comm 1 ,
312.Xr ed 1 ,
313.Xr diff3 1
314.Sh DIAGNOSTICS
315The
316.Nm diff
317utility exits with one of the following values:
318.Pp
319.Bl -tag -width Ds -compact -offset indent
320.It \&0
321No differences were found.
322.It \&1
323Differences were found.
324.It "\&>\&1"
325An error occurred.
326.El
327.Sh BUGS
328The
329.Fl f
330and
331.Fl e
332options
333do not provide special handling for lines on which the
334first and only character is
335.Dq Li \&. .
336This can cause problems for
337.Xr ed 1 .
338.Pp
339When comparing directories with the
340.Fl b ,
341.Fl w
342or
343.Fl i
344options specified,
345.Nm diff
346first compares the files ala
347.Ar cmp ,
348and then decides to run the
349.Nm diff
350algorithm if they are not equal.
351This may cause a small amount of spurious output if the files
352then turn out to be identical because the only differences are
353insignificant white space or case differences.
354.Sh HISTORY
355A
356.Nm
357command appeared in
358.At v6 .
359