xref: /freebsd/usr.bin/diff/diff.1 (revision cb6b5e8ab9cfe3938855826fa3de0f58dacd364e)
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 7, 2024
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 A Ar algo | Fl -algorithm Ar algo
44 .Op Fl -brief
45 .Op Fl -color Ns = Ns Ar when
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 F Ar pattern | Fl -show-function-line 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 A Ar algo | Fl -algorithm Ar algo
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 A Ar algo | Fl -algorithm Ar algo
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 A Ar algo | Fl -algorithm Ar algo
128 .Op Fl I Ar pattern | Fl -ignore-matching-lines Ar pattern
129 .Op Fl F Ar pattern | Fl -show-function-line Ar pattern
130 .Op Fl L Ar label | Fl -label Ar label
131 .Op Fl -brief
132 .Op Fl -color Ns = Ns Ar when
133 .Op Fl -changed-group-format Ar GFMT
134 .Op Fl -ed
135 .Op Fl -expand-tabs
136 .Op Fl -forward-ed
137 .Op Fl -ignore-all-space
138 .Op Fl -ignore-case
139 .Op Fl -ignore-space-change
140 .Op Fl -initial-tab
141 .Op Fl -minimal
142 .Op Fl -no-ignore-file-name-case
143 .Op Fl -normal
144 .Op Fl -paginate
145 .Op Fl -rcs
146 .Op Fl -show-c-function
147 .Op Fl -speed-large-files
148 .Op Fl -starting-file
149 .Op Fl -strip-trailing-cr
150 .Op Fl -tabsize Ar number
151 .Op Fl -text
152 .Fl U Ar number | Fl -unified Ar number
153 .Ar file1 file2
154 .Nm diff
155 .Op Fl aBbdilNPprsTtw
156 .Oo
157 .Fl c | e | f |
158 .Fl n | q | u
159 .Oc
160 .Op Fl A Ar algo | Fl -algorithm Ar algo
161 .Op Fl -brief
162 .Op Fl -color Ns = Ns Ar when
163 .Op Fl -changed-group-format Ar GFMT
164 .Op Fl -context
165 .Op Fl -ed
166 .Op Fl -expand-tabs
167 .Op Fl -forward-ed
168 .Op Fl -ignore-all-space
169 .Op Fl -ignore-case
170 .Op Fl -ignore-space-change
171 .Op Fl -initial-tab
172 .Op Fl -minimal
173 .Op Fl -new-file
174 .Op Fl -no-ignore-file-name-case
175 .Op Fl -normal
176 .Op Fl -paginate
177 .Op Fl -rcs
178 .Op Fl -recursive
179 .Op Fl -report-identical-files
180 .Op Fl -show-c-function
181 .Op Fl -speed-large-files
182 .Op Fl -strip-trailing-cr
183 .Op Fl -tabsize Ar number
184 .Op Fl -text
185 .Op Fl -unidirectional-new-file
186 .Op Fl -unified
187 .Op Fl I Ar pattern | Fl -ignore-matching-lines Ar pattern
188 .Op Fl F Ar pattern | Fl -show-function-line Ar pattern
189 .Bk -words
190 .Op Fl L Ar label | Fl -label Ar label
191 .Op Fl S Ar name | Fl -starting-file Ar name
192 .Op Fl X Ar file | Fl -exclude-from Ar file
193 .Op Fl x Ar pattern | Fl -exclude Ar pattern
194 .Ek
195 .Ar dir1 dir2
196 .Nm diff
197 .Op Fl aBbditwW
198 .Op Fl -color Ns = Ns Ar when
199 .Op Fl -expand-tabs
200 .Op Fl -ignore-all-blanks
201 .Op Fl -ignore-blank-lines
202 .Op Fl -ignore-case
203 .Op Fl -minimal
204 .Op Fl -no-ignore-file-name-case
205 .Op Fl -strip-trailing-cr
206 .Op Fl -suppress-common-lines
207 .Op Fl -tabsize Ar number
208 .Op Fl -text
209 .Op Fl -width
210 .Fl y | Fl -side-by-side
211 .Ar file1 file2
212 .Nm diff
213 .Op Fl -help
214 .Op Fl -version
215 .Sh DESCRIPTION
216 The
217 .Nm
218 utility compares the contents of
219 .Ar file1
220 and
221 .Ar file2
222 and writes to the standard output the list of changes necessary to
223 convert one file into the other.
224 No output is produced if the files are identical.
225 .Pp
226 Output options (mutually exclusive):
227 .Bl -tag -width Ds
228 .It Fl C Ar number Fl -context Ar number
229 Like
230 .Fl c
231 but produces a diff with
232 .Ar number
233 lines of context.
234 .It Fl c
235 Produces a diff with 3 lines of context.
236 With
237 .Fl c
238 the output format is modified slightly:
239 the output begins with identification of the files involved and
240 their creation dates and then each change is separated
241 by a line with fifteen
242 .Li * Ns 's .
243 The lines removed from
244 .Ar file1
245 are marked with
246 .Sq \&-\ \& ;
247 those added to
248 .Ar file2
249 are marked
250 .Sq +\ \& .
251 Lines which are changed from one file to the other are marked in
252 both files with
253 .Sq !\ \& .
254 Changes which lie within 3 lines of each other are grouped together on
255 output.
256 .It Fl D Ar string Fl -ifdef Ar string
257 Creates a merged version of
258 .Ar file1
259 and
260 .Ar file2
261 on the standard output, with C preprocessor controls included so that
262 a compilation of the result without defining
263 .Ar string
264 is equivalent to compiling
265 .Ar file1 ,
266 while defining
267 .Ar string
268 will yield
269 .Ar file2 .
270 .It Fl e -ed
271 Produces output in a form suitable as input for the editor utility,
272 .Xr ed 1 ,
273 which can then be used to convert file1 into file2.
274 .Pp
275 Extra commands are added to the output when comparing directories with
276 .Fl e ,
277 so that the result is a
278 .Xr sh 1
279 script for converting text files which are common to the two directories
280 from their state in
281 .Ar dir1
282 to their state in
283 .Ar dir2 .
284 Note that when comparing directories with
285 .Fl e ,
286 the resulting file may no longer be interpreted as an
287 .Xr ed 1
288 script.
289 Output is added to indicate which file each set of
290 .Xr ed 1
291 commands applies to.
292 These hunks can be manually extracted to produce an
293 .Xr ed 1
294 script, which can also be applied with
295 .Xr patch 1 .
296 .It Fl f -forward-ed
297 Identical output to that of the
298 .Fl e
299 flag, but in reverse order.
300 It cannot be digested by
301 .Xr ed 1 .
302 .It Fl -help
303 This option prints a summary to stdout and exits with status 0.
304 .It Fl n
305 Produces a script similar to that of
306 .Fl e ,
307 but in the opposite order and with a count of changed lines on each
308 insert or delete command.
309 This is the form used by rcsdiff.
310 .It Fl q -brief
311 Just print a line when the files differ.
312 Does not output a list of changes.
313 .It Fl U Ar number Fl -unified Ar number
314 Like
315 .Fl u
316 but produces a diff with
317 .Ar number
318 lines of context.
319 .It Fl u
320 Produces a
321 .Em unified
322 diff with 3 lines of context.
323 A unified diff is similar to the context diff produced by the
324 .Fl c
325 option.
326 However, unlike with
327 .Fl c ,
328 all lines to be changed (added and/or removed) are present in
329 a single section.
330 .It Fl -version
331 This option prints a version string to stdout and exits with status 0.
332 .It Fl y Fl -side-by-side
333 Output in two columns with a marker between them.
334 The marker can be one
335 of the following:
336 .Pp
337 .Bl -tag -width Ds -offset indent -compact
338 .It space
339 Corresponding lines are identical.
340 .It '|'
341 Corresponding lines are different.
342 .It '<'
343 Files differ and only the first file contains the line.
344 .It '>'
345 Files differ and only the second file contains the line.
346 .El
347 .El
348 .Pp
349 Comparison options:
350 .Bl -tag -width Ds
351 .It Fl A Ar algo, Fl -algorithm Ar algo
352 Configure the algorithm used when comparing files.
353 .Nm
354 supports 3 algorithms:
355 .Pp
356 .Bl -tag -width Ds -compact
357 .It Cm myers
358 The Myers diff algorithm finds the shortest edit which transforms one
359 input into the other.
360 It generally runs in O(N+D\(S2) time, requiring O(N) space, where N is
361 the sum of the lengths of the inputs and D is the length of the
362 difference between them, with a theoretical O(N\(pcD) worst case.
363 If it encounters worst-case input, the implementation used by
364 .Nm
365 falls back to a less optimal but faster algorithm.
366 .It Cm patience
367 The Patience variant of the Myers algorithm attempts to create more
368 aesthetically pleasing diff output by logically grouping lines.
369 .It Cm stone
370 The Stone algorithm (commonly known as Hunt-McIlroy or Hunt-Szymanski)
371 looks for the longest common subsequence between compared files.
372 Stone encounters worst case performance when there are long common
373 subsequences.
374 In large files this can lead to a significant performance impact.
375 The Stone algorithm is maintained for compatibility.
376 .El
377 .Pp
378 The
379 .Nm
380 utility defaults to the Myers algorithm, but will fall back to the
381 Stone algorithm if the input or output options are not supported by
382 the Myers implementation.
383 .It Fl a -text
384 Treat all files as ASCII text.
385 Normally
386 .Nm
387 will simply print
388 .Dq Binary files ... differ
389 if files contain binary characters.
390 Use of this option forces
391 .Nm
392 to produce a diff.
393 .It Fl B Fl -ignore-blank-lines
394 Causes chunks that include only blank lines to be ignored.
395 .It Fl b -ignore-space-change
396 Causes trailing blanks (spaces and tabs) to be ignored, and other
397 strings of blanks to compare equal.
398 .It Fl -color= Ns Oo Ar when Oc
399 Color the additions green, and removals red, or the value in the
400 .Ev DIFFCOLORS
401 environment variable.
402 The possible values of
403 .Ar when
404 are
405 .Dq Cm never ,
406 .Dq Cm always
407 and
408 .Dq Cm auto .
409 .Cm auto
410 will use color if the output is a tty and the
411 .Ev COLORTERM
412 environment variable is set to a non-empty string.
413 .It Fl d -minimal
414 Try very hard to produce a diff as small as possible.
415 This may consume a lot of processing power and memory when processing
416 large files with many changes.
417 .It Fl F Ar pattern, Fl -show-function-line Ar pattern
418 Like
419 .Fl p,
420 but display the last line that matches provided pattern.
421 .It Fl I Ar pattern Fl -ignore-matching-lines Ar pattern
422 Ignores changes, insertions, and deletions whose lines match the
423 extended regular expression
424 .Ar pattern .
425 Multiple
426 .Fl I
427 patterns may be specified.
428 All lines in the change must match some pattern for the change to be
429 ignored.
430 See
431 .Xr re_format 7
432 for more information on regular expression patterns.
433 .It Fl i -ignore-case
434 Ignores the case of letters.
435 E.g.,
436 .Dq A
437 will compare equal to
438 .Dq a .
439 .It Fl l -paginate
440 Pass the output through
441 .Xr pr 1
442 to paginate it.
443 .It Fl L Ar label Fl -label Ar label
444 Print
445 .Ar label
446 instead of the first (and second, if this option is specified twice)
447 file name and time in the context or unified diff header.
448 .It Fl p -show-c-function
449 With unified and context diffs, show with each change
450 the first 40 characters of the last line before the context beginning
451 with a letter, an underscore or a dollar sign.
452 For C and Objective-C source code following standard layout conventions, this
453 will show the prototype of the function the change applies to.
454 .It Fl T -initial-tab
455 Print a tab rather than a space before the rest of the line for the
456 normal, context or unified output formats.
457 This makes the alignment of tabs in the line consistent.
458 .It Fl t -expand-tabs
459 Will expand tabs in output lines.
460 Normal or
461 .Fl c
462 output adds character(s) to the front of each line which may screw up
463 the indentation of the original source lines and make the output listing
464 difficult to interpret.
465 This option will preserve the original source's indentation.
466 .It Fl w -ignore-all-blanks
467 Is similar to
468 .Fl b -ignore-space-change
469 but causes whitespace (blanks and tabs) to be totally ignored.
470 E.g.,
471 .Dq if (\ \&a == b \&)
472 will compare equal to
473 .Dq if(a==b) .
474 .It Fl W Ar number Fl -width Ar number
475 Output at most
476 .Ar number
477 columns when using side by side format.
478 The default value is 130.
479 Note that unless
480 .It Fl t
481 was specified,
482 .Nm
483 will always align the second column to a tab stop, so values of
484 .Fl -width
485 smaller than approximately five times the value of
486 .Fl -tabsize
487 may yield surprising results.
488 .It Fl -changed-group-format Ar GFMT
489 Format input groups in the provided
490 .Pp
491 the format is a string with special keywords:
492 .Bl -tag -width %<
493 .It %<
494 lines from FILE1
495 .It %<
496 lines from FILE2
497 .El
498 .It Fl -ignore-file-name-case
499 ignore case when comparing file names
500 .It Fl -no-ignore-file-name-case
501 do not ignore case when comparing file names (default)
502 .It Fl -normal
503 default diff output
504 .It Fl -speed-large-files
505 stub option for compatibility with GNU diff
506 .It Fl -strip-trailing-cr
507 strip carriage return on input files
508 .It Fl -suppress-common-lines
509 Do not output common lines when using the side by side format
510 .It Fl -tabsize Ar number
511 Number of spaces representing a tab (default 8)
512 .El
513 .Pp
514 Directory comparison options:
515 .Bl -tag -width Ds
516 .It Fl N -new-file
517 If a file is found in only one directory, act as if it was found in the
518 other directory too but was of zero size.
519 .It Fl P -unidirectional-new-file
520 If a file is found only in
521 .Ar dir2 ,
522 act as if it was found in
523 .Ar dir1
524 too but was of zero size.
525 .It Fl r -recursive
526 Causes application of
527 .Nm
528 recursively to common subdirectories encountered.
529 .It Fl S Ar name Fl -starting-file Ar name
530 Re-starts a directory
531 .Nm
532 in the middle, beginning with file
533 .Ar name .
534 .It Fl s -report-identical-files
535 Causes
536 .Nm
537 to report files which are the same, which are otherwise not mentioned.
538 .It Fl X Ar file Fl -exclude-from Ar file
539 Exclude files and subdirectories from comparison whose basenames match
540 lines in
541 .Ar file .
542 Multiple
543 .Fl X
544 options may be specified.
545 .It Fl x Ar pattern Fl -exclude Ar pattern
546 Exclude files and subdirectories from comparison whose basenames match
547 .Ar pattern .
548 Patterns are matched using shell-style globbing via
549 .Xr fnmatch 3 .
550 Multiple
551 .Fl x
552 options may be specified.
553 .El
554 .Pp
555 If both arguments are directories,
556 .Nm
557 sorts the contents of the directories by name, and then runs the
558 regular file
559 .Nm
560 algorithm, producing a change list,
561 on text files which are different.
562 Binary files which differ,
563 common subdirectories, and files which appear in only one directory
564 are described as such.
565 In directory mode only regular files and directories are compared.
566 If a non-regular file such as a device special file or FIFO is encountered,
567 a diagnostic message is printed.
568 .Pp
569 If only one of
570 .Ar file1
571 and
572 .Ar file2
573 is a directory,
574 .Nm
575 is applied to the non-directory file and the file contained in
576 the directory file with a filename that is the same as the
577 last component of the non-directory file.
578 .Pp
579 If either
580 .Ar file1
581 or
582 .Ar file2
583 is
584 .Sq - ,
585 the standard input is
586 used in its place.
587 .Ss Output Style
588 The default (without
589 .Fl e ,
590 .Fl c ,
591 or
592 .Fl n -rcs
593 .\" -C
594 options)
595 output contains lines of these forms, where
596 .Va XX , YY , ZZ , QQ
597 are line numbers respective of file order.
598 .Pp
599 .Bl -tag -width "XX,YYcZZ,QQ" -compact
600 .It Li XX Ns Ic a Ns Li YY
601 At (the end of) line
602 .Va XX
603 of
604 .Ar file1 ,
605 append the contents
606 of line
607 .Va YY
608 of
609 .Ar file2
610 to make them equal.
611 .It Li XX Ns Ic a Ns Li YY,ZZ
612 Same as above, but append the range of lines,
613 .Va YY
614 through
615 .Va ZZ
616 of
617 .Ar file2
618 to line
619 .Va XX
620 of file1.
621 .It Li XX Ns Ic d Ns Li YY
622 At line
623 .Va XX
624 delete
625 the line.
626 The value
627 .Va YY
628 tells to which line the change would bring
629 .Ar file1
630 in line with
631 .Ar file2 .
632 .It Li XX,YY Ns Ic d Ns Li ZZ
633 Delete the range of lines
634 .Va XX
635 through
636 .Va YY
637 in
638 .Ar file1 .
639 .It Li XX Ns Ic c Ns Li YY
640 Change the line
641 .Va XX
642 in
643 .Ar file1
644 to the line
645 .Va YY
646 in
647 .Ar file2 .
648 .It Li XX,YY Ns Ic c Ns Li ZZ
649 Replace the range of specified lines with the line
650 .Va ZZ .
651 .It Li XX,YY Ns Ic c Ns Li ZZ,QQ
652 Replace the range
653 .Va XX , Ns Va YY
654 from
655 .Ar file1
656 with the range
657 .Va ZZ , Ns Va QQ
658 from
659 .Ar file2 .
660 .El
661 .Pp
662 These lines resemble
663 .Xr ed 1
664 subcommands to convert
665 .Ar file1
666 into
667 .Ar file2 .
668 The line numbers before the action letters pertain to
669 .Ar file1 ;
670 those after pertain to
671 .Ar file2 .
672 Thus, by exchanging
673 .Ic a
674 for
675 .Ic d
676 and reading the line in reverse order, one can also
677 determine how to convert
678 .Ar file2
679 into
680 .Ar file1 .
681 As in
682 .Xr ed 1 ,
683 identical
684 pairs (where num1 = num2) are abbreviated as a single
685 number.
686 .Sh ENVIRONMENT
687 .Bl -tag -width DIFFCOLORS
688 .It Ev DIFFCOLORS
689 The value of this variable is the form
690 .Ar add Ns : Ns Ar rm ,
691 where
692 .Ar add
693 is the ASCII escape sequence for additions and
694 .Ar rm
695 is the ASCII escape sequence for deletions.
696 If this is unset,
697 .Nm
698 uses green for additions and red for removals.
699 .El
700 .Sh FILES
701 .Bl -tag -width /tmp/diff.XXXXXXXX -compact
702 .It Pa /tmp/diff.XXXXXXXX
703 Temporary file used when comparing a device or the standard input.
704 Note that the temporary file is unlinked as soon as it is created
705 so it will not show up in a directory listing.
706 .El
707 .Sh EXIT STATUS
708 The
709 .Nm
710 utility exits with one of the following values:
711 .Pp
712 .Bl -tag -width Ds -offset indent -compact
713 .It 0
714 No differences were found.
715 .It 1
716 Differences were found.
717 .It >1
718 An error occurred.
719 .El
720 .Pp
721 The
722 .Fl -help
723 and
724 .Fl -version
725 options exit with a status of 0.
726 .Sh EXAMPLES
727 Compare
728 .Pa old_dir
729 and
730 .Pa new_dir
731 recursively generating an unified diff and treating files found only in one
732 of those directories as new files:
733 .Bd -literal -offset indent
734 $ diff -ruN /path/to/old_dir /path/to/new_dir
735 .Ed
736 .Pp
737 Same as above but excluding files matching the expressions
738 .Dq *.h
739 and
740 .Dq *.c :
741 .Bd -literal -offset indent
742 $ diff -ruN -x '*.h' -x '*.c' /path/to/old_dir /path/to/new_dir
743 .Ed
744 .Pp
745 Show a single line indicating if the files differ:
746 .Bd -literal -offset indent
747 $ diff -q /boot/loader.conf /boot/defaults/loader.conf
748 Files /boot/loader.conf and /boot/defaults/loader.conf differ
749 .Ed
750 .Pp
751 Assuming a file named
752 .Pa example.txt
753 with the following contents:
754 .Bd -literal -offset indent
755 FreeBSD is an operating system
756 Linux is a kernel
757 OpenBSD is an operating system
758 .Ed
759 .Pp
760 Compare stdin with
761 .Pa example.txt
762 excluding from the comparison those lines containing either
763 .Qq Linux
764 or
765 .Qq Open :
766 .Bd -literal -offset indent
767 $ echo "FreeBSD is an operating system" | diff -q -I 'Linux|Open' example.txt -
768 .Ed
769 .Sh SEE ALSO
770 .Xr cmp 1 ,
771 .Xr comm 1 ,
772 .Xr diff3 1 ,
773 .Xr ed 1 ,
774 .Xr patch 1 ,
775 .Xr pr 1 ,
776 .Xr sdiff 1
777 .Rs
778 .%A James W. Hunt
779 .%A M. Douglas McIlroy
780 .%T "An Algorithm for Differential File Comparison"
781 .%J Computing Science Technical Report
782 .%Q Bell Laboratories 41
783 .%D June 1976
784 .Re
785 .Sh STANDARDS
786 The
787 .Nm
788 utility is compliant with the
789 .St -p1003.1-2008
790 specification.
791 .Pp
792 The flags
793 .Op Fl AaDdIiLlNnPpqSsTtwXxy
794 are extensions to that specification.
795 .Sh HISTORY
796 A
797 .Nm
798 command appeared in
799 .At v6 .
800 The
801 .Nm
802 implementation used in
803 .Fx
804 was GNU diff until
805 .Fx 11.4 .
806 This was replaced in
807 .Fx 12.0
808 by a BSD-licensed implementation written by
809 .An Todd Miller .
810 Some GNUisms were lost in the process.
811 .Pp
812 libdiff was imported from the Game of Trees version control system and default
813 algorithm was changed to Myers for FreeBSD 15.
814