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