xref: /openbsd-src/usr.bin/diff/diff.1 (revision 1ba3174772658d281f37df4d297ecec7213ddbe1)
1.\" $OpenBSD: diff.1,v 1.9 2003/07/04 17:37:07 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 abitw
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 abitw
48.Op Fl C Ar number
49.Ar file1 file2
50.Nm diff
51.Op Fl abitw
52.Op Fl D Ar string
53.Ar file1 file2
54.Nm diff
55.Op Fl abitw
56.Op Fl U Ar number
57.Ar file1 file2
58.Nm diff
59.Op Fl abitw
60.Oo
61.Fl c | Fl e | Fl f |
62.Fl n | Fl u
63.Oc
64.Op Fl l
65.Op Fl r
66.Op Fl s
67.Op Fl S Ar name
68.Ar dir1 dir2
69.Sh DESCRIPTION
70The
71.Nm
72utility compares the contents of
73.Ar file1
74and
75.Ar file2
76and writes to the standard output the list of changes necessary to
77convert one file into the other.
78No output is produced if the files are identical.
79.Pp
80Output options (mutually exclusive):
81.Bl -tag -width Ds
82.It Fl a
83Treat all files as ASCII.
84.It Fl c
85Produces a diff with 3 lines of context.
86With
87.Fl c
88the output format is modified slightly:
89the output beginning with identification of the files involved and
90their creation dates and then each change is separated
91by a line with a dozen *'s.
92The lines removed from
93.Ar file1
94are marked with
95.Sq \-\ \& ;
96those added to
97.Ar file2
98are marked
99.Sq \+\ \& .
100Lines which are changed from one file to the other are marked in
101both files with
102.Sq !\ \& .
103Changes which lie within 3 lines of each other are grouped together on output.
104.It Fl e
105Produces output in a form suitable as input for the editor utility,
106.Xr ed 1 ,
107which can then be used to convert file1 into file2.
108.Pp
109Extra commands are added to the output when comparing directories with
110.Fl e ,
111so that the result is a
112.Xr sh 1
113script for converting text files which are common to the two directories
114from their state in
115.Ar dir1
116to their state in
117.Ar dir2 .
118.It Fl f
119Identical output to that of the
120.Fl e
121flag, but in reverse order.
122It cannot be digested by
123.Xr ed 1 .
124.It Fl n
125Produces a script similar to that of
126.Fl e ,
127but in the opposite order and with a count of changed lines on each
128insert or delete command.
129This is the form used by
130.Xr rcsdiff 1 .
131.It Fl u
132Produces a
133.Em unified
134diff with 3 lines of context.
135A unified diff is similar to the context diff produced by the
136.Fl c
137option.
138However, unlike with
139.Fl c ,
140all lines to be changed (added and/or removed) are present in
141a single section.
142.It Fl C Ar number
143Like
144.Fl c
145but produces a diff with
146.Ar number
147lines of context.
148.It Fl D Ar string
149Creates a merged version of
150.Ar file1
151and
152.Ar file2
153on the standard output, with C preprocessor controls included so that
154a compilation of the result without defining
155.Ar string
156is equivalent to compiling
157.Ar file1 ,
158while defining
159.Ar string
160will yield
161.Ar file2 .
162.It Fl U Ar number
163Like
164.Fl u
165but produces a diff with
166.Ar number
167lines of context.
168.El
169.Pp
170Comparison options:
171.Bl -tag -width Ds
172.It Fl b
173Causes trailing blanks (spaces and tabs) to be ignored, and other
174strings of blanks to compare equal.
175.It Fl i
176Ignores the case of letters.
177E.g.,
178.Dq A
179will compare equal to
180.Dq a .
181.It Fl t
182Will expand tabs in output lines.
183Normal or
184.Fl c
185output adds character(s) to the front of each line which may screw up
186the indentation of the original source lines and make the output listing
187difficult to interpret.
188This option will preserve the original source's indentation.
189.It Fl w
190Is similar to
191.Fl b
192but causes whitespace (blanks and tabs) to be totally ignored.
193E.g.,
194.Dq if (\ \&a == b \&)
195will compare equal to
196.Dq if(a==b) .
197.El
198.Pp
199Directory comparison options:
200.Bl -tag -width Ds
201.It Fl l
202Long output format; each text file
203.Nm diff Ns \'d
204is piped through
205.Xr pr 1
206to paginate it;
207other differences are remembered and summarized
208after all text file differences are reported.
209.It Fl r
210Causes application of
211.Nm
212recursively to common subdirectories encountered.
213.It Fl s
214Causes
215.Nm
216to report files which are the same, which are otherwise not mentioned.
217.It Fl S Ar name
218Re-starts a directory
219.Nm
220in the middle, beginning with file
221.Ar name .
222.El
223.Pp
224If both arguments are directories,
225.Nm
226sorts the contents of the directories by name, and then runs the
227regular file
228.Nm
229algorithm, producing a change list,
230on text files which are different.
231Binary files which differ,
232common subdirectories, and files which appear in only one directory
233are described as such.
234.Pp
235If only one of
236.Ar file1
237and
238.Ar file2
239is a directory,
240.Nm
241is applied to the non-directory file and the file contained in
242the directory file with a filename that is the same as the
243last component of the non-directory file.
244.Pp
245If either
246.Ar file1
247or
248.Ar file2
249is
250.Sq Fl ,
251the standard input is
252used in its place.
253.Ss Output Style
254The default (without
255.Fl e ,
256.Fl c ,
257or
258.Fl n
259.\" -C
260options)
261output contains lines of these forms, where
262.Va XX , YY , ZZ , QQ
263are line numbers respective of file order.
264.Pp
265.Bl -tag -width "XX,YYcZZ,QQ" -compact
266.It Li XX Ns Ic a Ns Li YY
267At (the end of) line
268.Va XX
269of
270.Ar file1 ,
271append the contents
272of line
273.Va YY
274of
275.Ar file2
276to make them equal.
277.It Li XX Ns Ic a Ns Li YY,ZZ
278Same as above, but append the range of lines,
279.Va YY
280through
281.Va ZZ
282of
283.Ar file2
284to line
285.Va XX
286of file1.
287.It Li XX Ns Ic d Ns Li YY
288At line
289.Va XX
290delete
291the line.
292The value
293.Va YY
294tells to which line the change would bring
295.Ar file1
296in line with
297.Ar file1 .
298.It Li XX,YY Ns Ic d Ns Li ZZ
299Delete the range of lines
300.Va XX
301through
302.Va YY
303in
304.Ar file1 .
305.It Li XX Ns Ic c Ns Li YY
306Change the line
307.Va XX
308in
309.Ar file1
310to the line
311.Va YY
312in
313.Ar file2 .
314.It Li XX,YY Ns Ic c Ns Li ZZ
315Replace the range of specified lines with the line
316.Va ZZ .
317.It Li XX,YY Ns Ic c Ns Li ZZ,QQ
318Replace the range
319.Va XX , Ns YY
320from
321.Ar file1
322with the range
323.Va ZZ , Ns QQ
324from
325.Ar file2 .
326.El
327.Pp
328These lines resemble
329.Xr ed 1
330subcommands to convert
331.Ar file1
332into
333.Ar file2 .
334The line numbers before the action letters pertain to
335.Ar file1 ;
336those after pertain to
337.Ar file2 .
338Thus, by exchanging
339.Ic a
340for
341.Ic d
342and reading the line in reverse order, one can also
343determine how to convert
344.Ar file2
345into
346.Ar file1 .
347As in
348.Xr ed 1 ,
349identical
350pairs (where num1 = num2) are abbreviated as a single
351number.
352.Sh ENVIRONMENT
353.Bl -tag -width TMPDIR
354.It Ev TMPDIR
355If the environment variable
356.Ev TMPDIR
357exists,
358.Nm
359will use the directory specified by
360.Ev TMPDIR
361as the temporary directory.
362.El
363.Sh FILES
364.Bl -tag -width /tmp/diff1.XXXXXXXX -compact
365.It Pa /tmp/diff1.XXXXXXXX
366Temporary file used when
367.Ar file1
368is a device or stdin.
369.It Pa /tmp/diff2.XXXXXXXX
370Temporary file used when
371.Ar file2
372is a device or stdin.
373.It Pa /usr/bin/diff
374For directory diffs.
375.El
376.Sh DIAGNOSTICS
377The
378.Nm
379utility exits with one of the following values:
380.Pp
381.Bl -tag -width Ds -compact -offset indent
382.It \&0
383No differences were found.
384.It \&1
385Differences were found.
386.It \*[Gt]\&1
387An error occurred.
388.El
389.Sh SEE ALSO
390.Xr cc 1 ,
391.Xr cmp 1 ,
392.Xr comm 1 ,
393.Xr diff3 1 ,
394.Xr ed 1
395.Sh HISTORY
396A
397.Nm
398command appeared in
399.At v6 .
400.Sh BUGS
401The
402.Fl f
403and
404.Fl e
405options
406do not provide special handling for lines on which the
407first and only character is
408.Dq Li \&. .
409This can cause problems for
410.Xr ed 1 .
411.Pp
412When comparing directories with the
413.Fl b ,
414.Fl w
415or
416.Fl i
417options specified,
418.Nm
419first compares the files ala
420.Xr cmp 1 ,
421and then decides to run the
422.Nm
423algorithm if they are not equal.
424This may cause a small amount of spurious output if the files
425then turn out to be identical because the only differences are
426insignificant whitespace or case differences.
427