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