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