xref: /openbsd/usr.bin/diff/diff.1 (revision 4cfece93)
1.\" $OpenBSD: diff.1,v 1.49 2020/02/08 01:09:58 jsg 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: February 8 2020 $
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 ASCII text.
187Normally
188.Nm
189will simply print
190.Dq Binary files ... differ
191if files contain binary characters.
192Use of this option forces
193.Nm
194to produce a diff.
195.It Fl b
196Causes trailing blanks (spaces and tabs) to be ignored, and other
197strings of blanks to compare equal.
198.It Fl d
199Try very hard to produce a diff as small as possible.
200This may consume a lot of processing power and memory when processing
201large files with many changes.
202.It Fl I Ar pattern
203Ignores changes, insertions, and deletions whose lines match the
204extended regular expression
205.Ar pattern .
206Multiple
207.Fl I
208patterns may be specified.
209All lines in the change must match some pattern for the change to be
210ignored.
211See
212.Xr re_format 7
213for more information on regular expression patterns.
214.It Fl i
215Ignores the case of letters.
216E.g.,
217.Dq A
218will compare equal to
219.Dq a .
220.It Fl L Ar label
221Print
222.Ar label
223instead of the first (and second, if this option is specified twice)
224file name and time in the context or unified diff header.
225.It Fl p
226With unified and context diffs, show with each change
227the first 40 characters of the last line before the context beginning
228with a letter, an underscore or a dollar sign.
229For C source code following standard layout conventions, this will
230show the prototype of the function the change applies to.
231.It Fl T
232Print a tab rather than a space before the rest of the line for the
233normal, context or unified output formats.
234This makes the alignment of tabs in the line consistent.
235.It Fl t
236Will expand tabs in output lines.
237Normal or
238.Fl c
239output adds character(s) to the front of each line which may screw up
240the indentation of the original source lines and make the output listing
241difficult to interpret.
242This option will preserve the original source's indentation.
243.It Fl w
244Is similar to
245.Fl b
246but causes whitespace (blanks and tabs) to be totally ignored.
247E.g.,
248.Dq if (\ \&a == b \&)
249will compare equal to
250.Dq if(a==b) .
251.El
252.Pp
253Directory comparison options:
254.Bl -tag -width Ds
255.It Fl N
256If a file is found in only one directory, act as if it was found in the
257other directory too but was of zero size.
258.It Fl P
259If a file is found only in
260.Ar dir2 ,
261act as if it was found in
262.Ar dir1
263too but was of zero size.
264.It Fl r
265Causes application of
266.Nm
267recursively to common subdirectories encountered.
268.It Fl S Ar name
269Re-starts a directory
270.Nm
271in the middle, beginning with file
272.Ar name .
273.It Fl s
274Causes
275.Nm
276to report files which are the same, which are otherwise not mentioned.
277.It Fl X Ar file
278Exclude files and subdirectories from comparison whose basenames match
279lines in
280.Ar file .
281Multiple
282.Fl X
283options may be specified.
284.It Fl x Ar pattern
285Exclude files and subdirectories from comparison whose basenames match
286.Ar pattern .
287Patterns are matched using shell-style globbing via
288.Xr fnmatch 3 .
289Multiple
290.Fl x
291options may be specified.
292.El
293.Pp
294If both arguments are directories,
295.Nm
296sorts the contents of the directories by name, and then runs the
297regular file
298.Nm
299algorithm, producing a change list,
300on text files which are different.
301Binary files which differ,
302common subdirectories, and files which appear in only one directory
303are described as such.
304In directory mode only regular files and directories are compared.
305If a non-regular file such as a device special file or FIFO
306is encountered, a diagnostic message is printed.
307.Pp
308If only one of
309.Ar file1
310and
311.Ar file2
312is a directory,
313.Nm
314is applied to the non-directory file and the file contained in
315the directory file with a filename that is the same as the
316last component of the non-directory file.
317.Pp
318If either
319.Ar file1
320or
321.Ar file2
322is
323.Sq - ,
324the standard input is
325used in its place.
326.Ss Output Style
327The default (without
328.Fl e ,
329.Fl c ,
330or
331.Fl n
332.\" -C
333options)
334output contains lines of these forms, where
335.Va XX , YY , ZZ , QQ
336are line numbers respective of file order.
337.Pp
338.Bl -tag -width "XX,YYcZZ,QQ" -compact
339.It Li XX Ns Ic a Ns Li YY
340At (the end of) line
341.Va XX
342of
343.Ar file1 ,
344append the contents
345of line
346.Va YY
347of
348.Ar file2
349to make them equal.
350.It Li XX Ns Ic a Ns Li YY,ZZ
351Same as above, but append the range of lines,
352.Va YY
353through
354.Va ZZ
355of
356.Ar file2
357to line
358.Va XX
359of file1.
360.It Li XX Ns Ic d Ns Li YY
361At line
362.Va XX
363delete
364the line.
365The value
366.Va YY
367tells to which line the change would bring
368.Ar file1
369in line with
370.Ar file2 .
371.It Li XX,YY Ns Ic d Ns Li ZZ
372Delete the range of lines
373.Va XX
374through
375.Va YY
376in
377.Ar file1 .
378.It Li XX Ns Ic c Ns Li YY
379Change the line
380.Va XX
381in
382.Ar file1
383to the line
384.Va YY
385in
386.Ar file2 .
387.It Li XX,YY Ns Ic c Ns Li ZZ
388Replace the range of specified lines with the line
389.Va ZZ .
390.It Li XX,YY Ns Ic c Ns Li ZZ,QQ
391Replace the range
392.Va XX , Ns Va YY
393from
394.Ar file1
395with the range
396.Va ZZ , Ns Va QQ
397from
398.Ar file2 .
399.El
400.Pp
401These lines resemble
402.Xr ed 1
403subcommands to convert
404.Ar file1
405into
406.Ar file2 .
407The line numbers before the action letters pertain to
408.Ar file1 ;
409those after pertain to
410.Ar file2 .
411Thus, by exchanging
412.Ic a
413for
414.Ic d
415and reading the line in reverse order, one can also
416determine how to convert
417.Ar file2
418into
419.Ar file1 .
420As in
421.Xr ed 1 ,
422identical
423pairs (where num1 = num2) are abbreviated as a single
424number.
425.Sh FILES
426.Bl -tag -width /tmp/diff.XXXXXXXX -compact
427.It Pa /tmp/diff. Ns Ar XXXXXXXX
428Temporary file used when comparing a device or the standard input.
429Note that the temporary file is unlinked as soon as it is created
430so it will not show up in a directory listing.
431.El
432.Sh EXIT STATUS
433The
434.Nm
435utility exits with one of the following values:
436.Pp
437.Bl -tag -width Ds -offset indent -compact
438.It 0
439No differences were found.
440.It 1
441Differences were found.
442.It >1
443An error occurred.
444.El
445.Sh SEE ALSO
446.Xr cmp 1 ,
447.Xr comm 1 ,
448.Xr diff3 1 ,
449.Xr ed 1 ,
450.Xr patch 1 ,
451.Xr sdiff 1
452.Rs
453.%A James W. Hunt
454.%A M. Douglas McIlroy
455.%T "An Algorithm for Differential File Comparison"
456.%J Computing Science Technical Report
457.%Q Bell Laboratories 41
458.%D June 1976
459.Re
460.Sh STANDARDS
461The
462.Nm
463utility is compliant with the
464.St -p1003.1-2008
465specification.
466.Pp
467The flags
468.Op Fl aDdIiLlNnPpqSsTtwXx
469are extensions to that specification.
470.Sh HISTORY
471A
472.Nm
473command appeared in
474.At v5 .
475