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