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