xref: /freebsd/usr.bin/diff/diff.1 (revision cbb3ec25236ba72f91cbdf23f8b78b9d1af0cedf)
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 March 10, 2022
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.
432.It Fl -changed-group-format Ar GFMT
433Format input groups in the provided
434.Pp
435the format is a string with special keywords:
436.Bl -tag -width %<
437.It %<
438lines from FILE1
439.It %<
440lines from FILE2
441.El
442.It Fl -ignore-file-name-case
443ignore case when comparing file names
444.It Fl -no-ignore-file-name-case
445do not ignore case wen comparing file names (default)
446.It Fl -normal
447default diff output
448.It Fl -speed-large-files
449stub option for compatibility with GNU diff
450.It Fl -strip-trailing-cr
451strip carriage return on input files
452.It Fl -suppress-common-lines
453Do not output common lines when using the side by side format
454.It Fl -tabsize Ar number
455Number of spaces representing a tab (default 8)
456.El
457.Pp
458Directory comparison options:
459.Bl -tag -width Ds
460.It Fl N -new-file
461If a file is found in only one directory, act as if it was found in the
462other directory too but was of zero size.
463.It Fl P -unidirectional-new-file
464If a file is found only in
465.Ar dir2 ,
466act as if it was found in
467.Ar dir1
468too but was of zero size.
469.It Fl r -recursive
470Causes application of
471.Nm
472recursively to common subdirectories encountered.
473.It Fl S Ar name Fl -starting-file Ar name
474Re-starts a directory
475.Nm
476in the middle, beginning with file
477.Ar name .
478.It Fl s -report-identical-files
479Causes
480.Nm
481to report files which are the same, which are otherwise not mentioned.
482.It Fl X Ar file Fl -exclude-from Ar file
483Exclude files and subdirectories from comparison whose basenames match
484lines in
485.Ar file .
486Multiple
487.Fl X
488options may be specified.
489.It Fl x Ar pattern Fl -exclude Ar pattern
490Exclude files and subdirectories from comparison whose basenames match
491.Ar pattern .
492Patterns are matched using shell-style globbing via
493.Xr fnmatch 3 .
494Multiple
495.Fl x
496options may be specified.
497.El
498.Pp
499If both arguments are directories,
500.Nm
501sorts the contents of the directories by name, and then runs the
502regular file
503.Nm
504algorithm, producing a change list,
505on text files which are different.
506Binary files which differ,
507common subdirectories, and files which appear in only one directory
508are described as such.
509In directory mode only regular files and directories are compared.
510If a non-regular file such as a device special file or FIFO is encountered,
511a diagnostic message is printed.
512.Pp
513If only one of
514.Ar file1
515and
516.Ar file2
517is a directory,
518.Nm
519is applied to the non-directory file and the file contained in
520the directory file with a filename that is the same as the
521last component of the non-directory file.
522.Pp
523If either
524.Ar file1
525or
526.Ar file2
527is
528.Sq - ,
529the standard input is
530used in its place.
531.Ss Output Style
532The default (without
533.Fl e ,
534.Fl c ,
535or
536.Fl n -rcs
537.\" -C
538options)
539output contains lines of these forms, where
540.Va XX , YY , ZZ , QQ
541are line numbers respective of file order.
542.Pp
543.Bl -tag -width "XX,YYcZZ,QQ" -compact
544.It Li XX Ns Ic a Ns Li YY
545At (the end of) line
546.Va XX
547of
548.Ar file1 ,
549append the contents
550of line
551.Va YY
552of
553.Ar file2
554to make them equal.
555.It Li XX Ns Ic a Ns Li YY,ZZ
556Same as above, but append the range of lines,
557.Va YY
558through
559.Va ZZ
560of
561.Ar file2
562to line
563.Va XX
564of file1.
565.It Li XX Ns Ic d Ns Li YY
566At line
567.Va XX
568delete
569the line.
570The value
571.Va YY
572tells to which line the change would bring
573.Ar file1
574in line with
575.Ar file2 .
576.It Li XX,YY Ns Ic d Ns Li ZZ
577Delete the range of lines
578.Va XX
579through
580.Va YY
581in
582.Ar file1 .
583.It Li XX Ns Ic c Ns Li YY
584Change the line
585.Va XX
586in
587.Ar file1
588to the line
589.Va YY
590in
591.Ar file2 .
592.It Li XX,YY Ns Ic c Ns Li ZZ
593Replace the range of specified lines with the line
594.Va ZZ .
595.It Li XX,YY Ns Ic c Ns Li ZZ,QQ
596Replace the range
597.Va XX , Ns Va YY
598from
599.Ar file1
600with the range
601.Va ZZ , Ns Va QQ
602from
603.Ar file2 .
604.El
605.Pp
606These lines resemble
607.Xr ed 1
608subcommands to convert
609.Ar file1
610into
611.Ar file2 .
612The line numbers before the action letters pertain to
613.Ar file1 ;
614those after pertain to
615.Ar file2 .
616Thus, by exchanging
617.Ic a
618for
619.Ic d
620and reading the line in reverse order, one can also
621determine how to convert
622.Ar file2
623into
624.Ar file1 .
625As in
626.Xr ed 1 ,
627identical
628pairs (where num1 = num2) are abbreviated as a single
629number.
630.Sh ENVIRONMENT
631.Bl -tag -width DIFFCOLORS
632.It Ev DIFFCOLORS
633The value of this variable is the form
634.Ar add Ns : Ns Ar rm ,
635where
636.Ar add
637is the ASCII escape sequence for additions and
638.Ar rm
639is the ASCII escape sequence for deletions.
640If this is unset,
641.Nm
642uses green for additions and red for removals.
643.El
644.Sh FILES
645.Bl -tag -width /tmp/diff.XXXXXXXX -compact
646.It Pa /tmp/diff.XXXXXXXX
647Temporary file used when comparing a device or the standard input.
648Note that the temporary file is unlinked as soon as it is created
649so it will not show up in a directory listing.
650.El
651.Sh EXIT STATUS
652The
653.Nm
654utility exits with one of the following values:
655.Pp
656.Bl -tag -width Ds -offset indent -compact
657.It 0
658No differences were found.
659.It 1
660Differences were found.
661.It >1
662An error occurred.
663.El
664.Pp
665The
666.Fl -help
667and
668.Fl -version
669options exit with a status of 0.
670.Sh EXAMPLES
671Compare
672.Pa old_dir
673and
674.Pa new_dir
675recursively generating an unified diff and treating files found only in one
676of those directories as new files:
677.Bd -literal -offset indent
678$ diff -ruN /path/to/old_dir /path/to/new_dir
679.Ed
680.Pp
681Same as above but excluding files matching the expressions
682.Dq *.h
683and
684.Dq *.c :
685.Bd -literal -offset indent
686$ diff -ruN -x '*.h' -x '*.c' /path/to/old_dir /path/to/new_dir
687.Ed
688.Pp
689Show a single line indicating if the files differ:
690.Bd -literal -offset indent
691$ diff -q /boot/loader.conf /boot/defaults/loader.conf
692Files /boot/loader.conf and /boot/defaults/loader.conf differ
693.Ed
694.Pp
695Assuming a file named
696.Pa example.txt
697with the following contents:
698.Bd -literal -offset indent
699FreeBSD is an operating system
700Linux is a kernel
701OpenBSD is an operating system
702.Ed
703.Pp
704Compare stdin with
705.Pa example.txt
706excluding from the comparison those lines containing either
707.Qq Linux
708or
709.Qq Open :
710.Bd -literal -offset indent
711$ echo "FreeBSD is an operating system" | diff -q -I 'Linux|Open' example.txt -
712.Ed
713.Sh SEE ALSO
714.Xr cmp 1 ,
715.Xr comm 1 ,
716.Xr diff3 1 ,
717.Xr ed 1 ,
718.Xr patch 1 ,
719.Xr pr 1 ,
720.Xr sdiff 1
721.Rs
722.%A James W. Hunt
723.%A M. Douglas McIlroy
724.%T "An Algorithm for Differential File Comparison"
725.%J Computing Science Technical Report
726.%Q Bell Laboratories 41
727.%D June 1976
728.Re
729.Sh STANDARDS
730The
731.Nm
732utility is compliant with the
733.St -p1003.1-2008
734specification.
735.Pp
736The flags
737.Op Fl aDdIiLlNnPpqSsTtwXxy
738are extensions to that specification.
739.Sh HISTORY
740A
741.Nm
742command appeared in
743.At v6 .
744