xref: /freebsd/usr.bin/diff/diff.1 (revision 565518046c9e721ea6ff608d44f1e89d344e5e02)
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 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.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 n
286Produces a script similar to that of
287.Fl e ,
288but in the opposite order and with a count of changed lines on each
289insert or delete command.
290This is the form used by rcsdiff.
291.It Fl q -brief
292Just print a line when the files differ.
293Does not output a list of changes.
294.It Fl U Ar number Fl -unified Ar number
295Like
296.Fl u
297but produces a diff with
298.Ar number
299lines of context.
300.It Fl u
301Produces a
302.Em unified
303diff with 3 lines of context.
304A unified diff is similar to the context diff produced by the
305.Fl c
306option.
307However, unlike with
308.Fl c ,
309all lines to be changed (added and/or removed) are present in
310a single section.
311.It Fl y Fl -side-by-side
312Output in two columns with a marker between them.
313The marker can be one
314of the following:
315.Pp
316.Bl -tag -width Ds -offset indent -compact
317.It space
318Corresponding lines are identical.
319.It '|'
320Corresponding lines are different.
321.It '<'
322Files differ and only the first file contains the line.
323.It '>'
324Files differ and only the second file contains the line.
325.El
326.El
327.Pp
328Comparison options:
329.Bl -tag -width Ds
330.It Fl a -text
331Treat all files as ASCII text.
332Normally
333.Nm
334will simply print
335.Dq Binary files ... differ
336if files contain binary characters.
337Use of this option forces
338.Nm
339to produce a diff.
340.It Fl B Fl -ignore-blank-lines
341Causes chunks that include only blank lines to be ignored.
342.It Fl b -ignore-space-change
343Causes trailing blanks (spaces and tabs) to be ignored, and other
344strings of blanks to compare equal.
345.It Fl -color= Ns Oo Ar when Oc
346Color the additions green, and removals red, or the value in the
347.Ev DIFFCOLORS
348environment variable.
349The possible values of
350.Ar when
351are
352.Dq Cm never ,
353.Dq Cm always
354and
355.Dq Cm auto .
356.Cm auto
357will use color if the output is a tty and the
358.Ev COLORTERM
359environment variable is set to a non-empty string.
360.It Fl d -minimal
361Try very hard to produce a diff as small as possible.
362This may consume a lot of processing power and memory when processing
363large files with many changes.
364.It Fl F Ar pattern, Fl -show-function-line Ar pattern
365Like
366.Fl p,
367but display the last line that matches provided pattern.
368.It Fl I Ar pattern Fl -ignore-matching-lines Ar pattern
369Ignores changes, insertions, and deletions whose lines match the
370extended regular expression
371.Ar pattern .
372Multiple
373.Fl I
374patterns may be specified.
375All lines in the change must match some pattern for the change to be
376ignored.
377See
378.Xr re_format 7
379for more information on regular expression patterns.
380.It Fl i -ignore-case
381Ignores the case of letters.
382E.g.,
383.Dq A
384will compare equal to
385.Dq a .
386.It Fl l -paginate
387Pass the output through
388.Xr pr 1
389to paginate it.
390.It Fl L Ar label Fl -label Ar label
391Print
392.Ar label
393instead of the first (and second, if this option is specified twice)
394file name and time in the context or unified diff header.
395.It Fl p -show-c-function
396With unified and context diffs, show with each change
397the first 40 characters of the last line before the context beginning
398with a letter, an underscore or a dollar sign.
399For C and Objective-C source code following standard layout conventions, this
400will show the prototype of the function the change applies to.
401.It Fl T -initial-tab
402Print a tab rather than a space before the rest of the line for the
403normal, context or unified output formats.
404This makes the alignment of tabs in the line consistent.
405.It Fl t -expand-tabs
406Will expand tabs in output lines.
407Normal or
408.Fl c
409output adds character(s) to the front of each line which may screw up
410the indentation of the original source lines and make the output listing
411difficult to interpret.
412This option will preserve the original source's indentation.
413.It Fl w -ignore-all-blanks
414Is similar to
415.Fl b -ignore-space-change
416but causes whitespace (blanks and tabs) to be totally ignored.
417E.g.,
418.Dq if (\ \&a == b \&)
419will compare equal to
420.Dq if(a==b) .
421.It Fl W Ar number Fl -width Ar number
422Output at most
423.Ar number
424columns when using side by side format.
425The default value is 130.
426.It Fl -changed-group-format Ar GFMT
427Format input groups in the provided
428.Pp
429the format is a string with special keywords:
430.Bl -tag -width %<
431.It %<
432lines from FILE1
433.It %<
434lines from FILE2
435.El
436.It Fl -ignore-file-name-case
437ignore case when comparing file names
438.It Fl -no-ignore-file-name-case
439do not ignore case wen comparing file names (default)
440.It Fl -normal
441default diff output
442.It Fl -speed-large-files
443stub option for compatibility with GNU diff
444.It Fl -strip-trailing-cr
445strip carriage return on input files
446.It Fl -suppress-common-lines
447Do not output common lines when using the side by side format
448.It Fl -tabsize Ar number
449Number of spaces representing a tab (default 8)
450.El
451.Pp
452Directory comparison options:
453.Bl -tag -width Ds
454.It Fl N -new-file
455If a file is found in only one directory, act as if it was found in the
456other directory too but was of zero size.
457.It Fl P -unidirectional-new-file
458If a file is found only in
459.Ar dir2 ,
460act as if it was found in
461.Ar dir1
462too but was of zero size.
463.It Fl r -recursive
464Causes application of
465.Nm
466recursively to common subdirectories encountered.
467.It Fl S Ar name Fl -starting-file Ar name
468Re-starts a directory
469.Nm
470in the middle, beginning with file
471.Ar name .
472.It Fl s -report-identical-files
473Causes
474.Nm
475to report files which are the same, which are otherwise not mentioned.
476.It Fl X Ar file Fl -exclude-from Ar file
477Exclude files and subdirectories from comparison whose basenames match
478lines in
479.Ar file .
480Multiple
481.Fl X
482options may be specified.
483.It Fl x Ar pattern Fl -exclude Ar pattern
484Exclude files and subdirectories from comparison whose basenames match
485.Ar pattern .
486Patterns are matched using shell-style globbing via
487.Xr fnmatch 3 .
488Multiple
489.Fl x
490options may be specified.
491.El
492.Pp
493If both arguments are directories,
494.Nm
495sorts the contents of the directories by name, and then runs the
496regular file
497.Nm
498algorithm, producing a change list,
499on text files which are different.
500Binary files which differ,
501common subdirectories, and files which appear in only one directory
502are described as such.
503In directory mode only regular files and directories are compared.
504If a non-regular file such as a device special file or FIFO is encountered,
505a diagnostic message is printed.
506.Pp
507If only one of
508.Ar file1
509and
510.Ar file2
511is a directory,
512.Nm
513is applied to the non-directory file and the file contained in
514the directory file with a filename that is the same as the
515last component of the non-directory file.
516.Pp
517If either
518.Ar file1
519or
520.Ar file2
521is
522.Sq - ,
523the standard input is
524used in its place.
525.Ss Output Style
526The default (without
527.Fl e ,
528.Fl c ,
529or
530.Fl n -rcs
531.\" -C
532options)
533output contains lines of these forms, where
534.Va XX , YY , ZZ , QQ
535are line numbers respective of file order.
536.Pp
537.Bl -tag -width "XX,YYcZZ,QQ" -compact
538.It Li XX Ns Ic a Ns Li YY
539At (the end of) line
540.Va XX
541of
542.Ar file1 ,
543append the contents
544of line
545.Va YY
546of
547.Ar file2
548to make them equal.
549.It Li XX Ns Ic a Ns Li YY,ZZ
550Same as above, but append the range of lines,
551.Va YY
552through
553.Va ZZ
554of
555.Ar file2
556to line
557.Va XX
558of file1.
559.It Li XX Ns Ic d Ns Li YY
560At line
561.Va XX
562delete
563the line.
564The value
565.Va YY
566tells to which line the change would bring
567.Ar file1
568in line with
569.Ar file2 .
570.It Li XX,YY Ns Ic d Ns Li ZZ
571Delete the range of lines
572.Va XX
573through
574.Va YY
575in
576.Ar file1 .
577.It Li XX Ns Ic c Ns Li YY
578Change the line
579.Va XX
580in
581.Ar file1
582to the line
583.Va YY
584in
585.Ar file2 .
586.It Li XX,YY Ns Ic c Ns Li ZZ
587Replace the range of specified lines with the line
588.Va ZZ .
589.It Li XX,YY Ns Ic c Ns Li ZZ,QQ
590Replace the range
591.Va XX , Ns Va YY
592from
593.Ar file1
594with the range
595.Va ZZ , Ns Va QQ
596from
597.Ar file2 .
598.El
599.Pp
600These lines resemble
601.Xr ed 1
602subcommands to convert
603.Ar file1
604into
605.Ar file2 .
606The line numbers before the action letters pertain to
607.Ar file1 ;
608those after pertain to
609.Ar file2 .
610Thus, by exchanging
611.Ic a
612for
613.Ic d
614and reading the line in reverse order, one can also
615determine how to convert
616.Ar file2
617into
618.Ar file1 .
619As in
620.Xr ed 1 ,
621identical
622pairs (where num1 = num2) are abbreviated as a single
623number.
624.Sh ENVIRONMENT
625.Bl -tag -width DIFFCOLORS
626.It Ev DIFFCOLORS
627The value of this variable is the form
628.Ar add Ns : Ns Ar rm ,
629where
630.Ar add
631is the ASCII escape sequence for additions and
632.Ar rm
633is the ASCII escape sequence for deletions.
634If this is unset,
635.Nm
636uses green for additions and red for removals.
637.El
638.Sh FILES
639.Bl -tag -width /tmp/diff.XXXXXXXX -compact
640.It Pa /tmp/diff.XXXXXXXX
641Temporary file used when comparing a device or the standard input.
642Note that the temporary file is unlinked as soon as it is created
643so it will not show up in a directory listing.
644.El
645.Sh EXIT STATUS
646The
647.Nm
648utility exits with one of the following values:
649.Pp
650.Bl -tag -width Ds -offset indent -compact
651.It 0
652No differences were found.
653.It 1
654Differences were found.
655.It >1
656An error occurred.
657.El
658.Sh EXAMPLES
659Compare
660.Pa old_dir
661and
662.Pa new_dir
663recursively generating an unified diff and treating files found only in one
664of those directories as new files:
665.Bd -literal -offset indent
666$ diff -ruN /path/to/old_dir /path/to/new_dir
667.Ed
668.Pp
669Same as above but excluding files matching the expressions
670.Dq *.h
671and
672.Dq *.c :
673.Bd -literal -offset indent
674$ diff -ruN -x '*.h' -x '*.c' /path/to/old_dir /path/to/new_dir
675.Ed
676.Pp
677Show a single line indicating if the files differ:
678.Bd -literal -offset indent
679$ diff -q /boot/loader.conf /boot/defaults/loader.conf
680Files /boot/loader.conf and /boot/defaults/loader.conf differ
681.Ed
682.Pp
683Assuming a file named
684.Pa example.txt
685with the following contents:
686.Bd -literal -offset indent
687FreeBSD is an operating system
688Linux is a kernel
689OpenBSD is an operating system
690.Ed
691.Pp
692Compare stdin with
693.Pa example.txt
694excluding from the comparison those lines containing either
695.Qq Linux
696or
697.Qq Open :
698.Bd -literal -offset indent
699$ echo "FreeBSD is an operating system" | diff -q -I 'Linux|Open' example.txt -
700.Ed
701.Sh SEE ALSO
702.Xr cmp 1 ,
703.Xr comm 1 ,
704.Xr diff3 1 ,
705.Xr ed 1 ,
706.Xr patch 1 ,
707.Xr pr 1 ,
708.Xr sdiff 1
709.Rs
710.%A James W. Hunt
711.%A M. Douglas McIlroy
712.%T "An Algorithm for Differential File Comparison"
713.%J Computing Science Technical Report
714.%Q Bell Laboratories 41
715.%D June 1976
716.Re
717.Sh STANDARDS
718The
719.Nm
720utility is compliant with the
721.St -p1003.1-2008
722specification.
723.Pp
724The flags
725.Op Fl aDdIiLlNnPpqSsTtwXxy
726are extensions to that specification.
727.Sh HISTORY
728A
729.Nm
730command appeared in
731.At v6 .
732