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