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