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