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