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