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