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