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