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