1 .\" $OpenBSD: diff.1,v 1.47 2015/11/24 19:35:41 jmc Exp $
3 .\" Copyright (c) 1980, 1990, 1993
4 .\" The Regents of the University of California. All rights reserved.
6 .\" Redistribution and use in source and binary forms, with or without
7 .\" modification, are permitted provided that the following conditions
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.
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
35 .Nd differential file and directory comparator
44 .Op Fl -color Ns = Ns Ar when
45 .Op Fl -changed-group-format Ar GFMT
49 .Op Fl -ignore-all-space
51 .Op Fl -ignore-space-change
54 .Op Fl -no-ignore-file-name-case
57 .Op Fl -show-c-function
59 .Op Fl -speed-large-files
60 .Op Fl -strip-trailing-cr
61 .Op Fl -tabsize Ar number
64 .Op Fl I Ar pattern | Fl -ignore-matching-lines Ar pattern
65 .Op Fl F Ar pattern | Fl -show-function-line Ar pattern
66 .Op Fl L Ar label | Fl -label Ar label
70 .Op Fl I Ar pattern | Fl -ignore-matching-lines Ar pattern
71 .Op Fl F Ar pattern | Fl -show-function-line Ar pattern
72 .Op Fl L Ar label | Fl -label Ar label
74 .Op Fl -color Ns = Ns Ar when
75 .Op Fl -changed-group-format Ar GFMT
79 .Op Fl -ignore-all-space
81 .Op Fl -ignore-space-change
84 .Op Fl -no-ignore-file-name-case
88 .Op Fl -show-c-function
89 .Op Fl -speed-large-files
91 .Op Fl -strip-trailing-cr
92 .Op Fl -tabsize Ar number
94 .Fl C Ar number | -context Ar number
98 .Op Fl I Ar pattern | Fl -ignore-matching-lines Ar pattern
100 .Op Fl -color Ns = Ns Ar when
101 .Op Fl -changed-group-format Ar GFMT
105 .Op Fl -ignore-all-space
107 .Op Fl -ignore-space-change
110 .Op Fl -no-ignore-file-name-case
114 .Op Fl -show-c-function
115 .Op Fl -speed-large-files
116 .Op Fl -starting-file
117 .Op Fl -strip-trailing-cr
118 .Op Fl -tabsize Ar number
120 .Fl D Ar string | Fl -ifdef Ar string
124 .Op Fl I Ar pattern | Fl -ignore-matching-lines Ar pattern
125 .Op Fl F Ar pattern | Fl -show-function-line Ar pattern
126 .Op Fl L Ar label | Fl -label Ar label
128 .Op Fl -color Ns = Ns Ar when
129 .Op Fl -changed-group-format Ar GFMT
133 .Op Fl -ignore-all-space
135 .Op Fl -ignore-space-change
138 .Op Fl -no-ignore-file-name-case
142 .Op Fl -show-c-function
143 .Op Fl -speed-large-files
144 .Op Fl -starting-file
145 .Op Fl -strip-trailing-cr
146 .Op Fl -tabsize Ar number
148 .Fl U Ar number | Fl -unified Ar number
151 .Op Fl aBbdilNPprsTtw
157 .Op Fl -color Ns = Ns Ar when
158 .Op Fl -changed-group-format Ar GFMT
163 .Op Fl -ignore-all-space
165 .Op Fl -ignore-space-change
169 .Op Fl -no-ignore-file-name-case
174 .Op Fl -report-identical-files
175 .Op Fl -show-c-function
176 .Op Fl -speed-large-files
177 .Op Fl -strip-trailing-cr
178 .Op Fl -tabsize Ar number
180 .Op Fl -unidirectional-new-file
182 .Op Fl I Ar pattern | Fl -ignore-matching-lines Ar pattern
183 .Op Fl F Ar pattern | Fl -show-function-line Ar pattern
185 .Op Fl L Ar label | Fl -label Ar label
186 .Op Fl S Ar name | Fl -starting-file Ar name
187 .Op Fl X Ar file | Fl -exclude-from Ar file
188 .Op Fl x Ar pattern | Fl -exclude Ar pattern
193 .Op Fl -color Ns = Ns Ar when
195 .Op Fl -ignore-all-blanks
196 .Op Fl -ignore-blank-lines
199 .Op Fl -no-ignore-file-name-case
200 .Op Fl -strip-trailing-cr
201 .Op Fl -suppress-common-lines
202 .Op Fl -tabsize Ar number
205 .Fl y | Fl -side-by-side
213 utility compares the contents of
217 and writes to the standard output the list of changes necessary to
218 convert one file into the other.
219 No output is produced if the files are identical.
221 Output options (mutually exclusive):
223 .It Fl C Ar number Fl -context Ar number
226 but produces a diff with
230 Produces a diff with 3 lines of context.
233 the output format is modified slightly:
234 the output begins with identification of the files involved and
235 their creation dates and then each change is separated
236 by a line with fifteen
238 The lines removed from
246 Lines which are changed from one file to the other are marked in
249 Changes which lie within 3 lines of each other are grouped together on
251 .It Fl D Ar string Fl -ifdef Ar string
252 Creates a merged version of
256 on the standard output, with C preprocessor controls included so that
257 a compilation of the result without defining
259 is equivalent to compiling
266 Produces output in a form suitable as input for the editor utility,
268 which can then be used to convert file1 into file2.
270 Extra commands are added to the output when comparing directories with
272 so that the result is a
274 script for converting text files which are common to the two directories
280 Identical output to that of the
282 flag, but in reverse order.
283 It cannot be digested by
286 This option prints a summary to stdout and exits with status 0.
288 Produces a script similar to that of
290 but in the opposite order and with a count of changed lines on each
291 insert or delete command.
292 This is the form used by rcsdiff.
294 Just print a line when the files differ.
295 Does not output a list of changes.
296 .It Fl U Ar number Fl -unified Ar number
299 but produces a diff with
305 diff with 3 lines of context.
306 A unified diff is similar to the context diff produced by the
311 all lines to be changed (added and/or removed) are present in
314 This option prints a version string to stdout and exits with status 0.
315 .It Fl y Fl -side-by-side
316 Output in two columns with a marker between them.
317 The marker can be one
320 .Bl -tag -width Ds -offset indent -compact
322 Corresponding lines are identical.
324 Corresponding lines are different.
326 Files differ and only the first file contains the line.
328 Files differ and only the second file contains the line.
335 Treat all files as ASCII text.
339 .Dq Binary files ... differ
340 if files contain binary characters.
341 Use of this option forces
344 .It Fl B Fl -ignore-blank-lines
345 Causes chunks that include only blank lines to be ignored.
346 .It Fl b -ignore-space-change
347 Causes trailing blanks (spaces and tabs) to be ignored, and other
348 strings of blanks to compare equal.
349 .It Fl -color= Ns Oo Ar when Oc
350 Color the additions green, and removals red, or the value in the
352 environment variable.
353 The possible values of
361 will use color if the output is a tty and the
363 environment variable is set to a non-empty string.
365 Try very hard to produce a diff as small as possible.
366 This may consume a lot of processing power and memory when processing
367 large files with many changes.
368 .It Fl F Ar pattern, Fl -show-function-line Ar pattern
371 but display the last line that matches provided pattern.
372 .It Fl I Ar pattern Fl -ignore-matching-lines Ar pattern
373 Ignores changes, insertions, and deletions whose lines match the
374 extended regular expression
378 patterns may be specified.
379 All lines in the change must match some pattern for the change to be
383 for more information on regular expression patterns.
384 .It Fl i -ignore-case
385 Ignores the case of letters.
388 will compare equal to
391 Pass the output through
394 .It Fl L Ar label Fl -label Ar label
397 instead of the first (and second, if this option is specified twice)
398 file name and time in the context or unified diff header.
399 .It Fl p -show-c-function
400 With unified and context diffs, show with each change
401 the first 40 characters of the last line before the context beginning
402 with a letter, an underscore or a dollar sign.
403 For C and Objective-C source code following standard layout conventions, this
404 will show the prototype of the function the change applies to.
405 .It Fl T -initial-tab
406 Print a tab rather than a space before the rest of the line for the
407 normal, context or unified output formats.
408 This makes the alignment of tabs in the line consistent.
409 .It Fl t -expand-tabs
410 Will expand tabs in output lines.
413 output adds character(s) to the front of each line which may screw up
414 the indentation of the original source lines and make the output listing
415 difficult to interpret.
416 This option will preserve the original source's indentation.
417 .It Fl w -ignore-all-blanks
419 .Fl b -ignore-space-change
420 but causes whitespace (blanks and tabs) to be totally ignored.
422 .Dq if (\ \&a == b \&)
423 will compare equal to
425 .It Fl W Ar number Fl -width Ar number
428 columns when using side by side format.
429 The default value is 130.
434 will always align the second column to a tab stop, so values of
436 smaller than approximately five times the value of
438 may yield surprising results.
439 .It Fl -changed-group-format Ar GFMT
440 Format input groups in the provided
442 the format is a string with special keywords:
449 .It Fl -ignore-file-name-case
450 ignore case when comparing file names
451 .It Fl -no-ignore-file-name-case
452 do not ignore case when comparing file names (default)
455 .It Fl -speed-large-files
456 stub option for compatibility with GNU diff
457 .It Fl -strip-trailing-cr
458 strip carriage return on input files
459 .It Fl -suppress-common-lines
460 Do not output common lines when using the side by side format
461 .It Fl -tabsize Ar number
462 Number of spaces representing a tab (default 8)
465 Directory comparison options:
468 If a file is found in only one directory, act as if it was found in the
469 other directory too but was of zero size.
470 .It Fl P -unidirectional-new-file
471 If a file is found only in
473 act as if it was found in
475 too but was of zero size.
477 Causes application of
479 recursively to common subdirectories encountered.
480 .It Fl S Ar name Fl -starting-file Ar name
481 Re-starts a directory
483 in the middle, beginning with file
485 .It Fl s -report-identical-files
488 to report files which are the same, which are otherwise not mentioned.
489 .It Fl X Ar file Fl -exclude-from Ar file
490 Exclude files and subdirectories from comparison whose basenames match
495 options may be specified.
496 .It Fl x Ar pattern Fl -exclude Ar pattern
497 Exclude files and subdirectories from comparison whose basenames match
499 Patterns are matched using shell-style globbing via
503 options may be specified.
506 If both arguments are directories,
508 sorts the contents of the directories by name, and then runs the
511 algorithm, producing a change list,
512 on text files which are different.
513 Binary files which differ,
514 common subdirectories, and files which appear in only one directory
515 are described as such.
516 In directory mode only regular files and directories are compared.
517 If a non-regular file such as a device special file or FIFO is encountered,
518 a diagnostic message is printed.
526 is applied to the non-directory file and the file contained in
527 the directory file with a filename that is the same as the
528 last component of the non-directory file.
536 the standard input is
546 output contains lines of these forms, where
547 .Va XX , YY , ZZ , QQ
548 are line numbers respective of file order.
550 .Bl -tag -width "XX,YYcZZ,QQ" -compact
551 .It Li XX Ns Ic a Ns Li YY
562 .It Li XX Ns Ic a Ns Li YY,ZZ
563 Same as above, but append the range of lines,
572 .It Li XX Ns Ic d Ns Li YY
579 tells to which line the change would bring
583 .It Li XX,YY Ns Ic d Ns Li ZZ
584 Delete the range of lines
590 .It Li XX Ns Ic c Ns Li YY
599 .It Li XX,YY Ns Ic c Ns Li ZZ
600 Replace the range of specified lines with the line
602 .It Li XX,YY Ns Ic c Ns Li ZZ,QQ
615 subcommands to convert
619 The line numbers before the action letters pertain to
621 those after pertain to
627 and reading the line in reverse order, one can also
628 determine how to convert
635 pairs (where num1 = num2) are abbreviated as a single
638 .Bl -tag -width DIFFCOLORS
640 The value of this variable is the form
641 .Ar add Ns : Ns Ar rm ,
644 is the ASCII escape sequence for additions and
646 is the ASCII escape sequence for deletions.
649 uses green for additions and red for removals.
652 .Bl -tag -width /tmp/diff.XXXXXXXX -compact
653 .It Pa /tmp/diff.XXXXXXXX
654 Temporary file used when comparing a device or the standard input.
655 Note that the temporary file is unlinked as soon as it is created
656 so it will not show up in a directory listing.
661 utility exits with one of the following values:
663 .Bl -tag -width Ds -offset indent -compact
665 No differences were found.
667 Differences were found.
676 options exit with a status of 0.
682 recursively generating an unified diff and treating files found only in one
683 of those directories as new files:
684 .Bd -literal -offset indent
685 $ diff -ruN /path/to/old_dir /path/to/new_dir
688 Same as above but excluding files matching the expressions
692 .Bd -literal -offset indent
693 $ diff -ruN -x '*.h' -x '*.c' /path/to/old_dir /path/to/new_dir
696 Show a single line indicating if the files differ:
697 .Bd -literal -offset indent
698 $ diff -q /boot/loader.conf /boot/defaults/loader.conf
699 Files /boot/loader.conf and /boot/defaults/loader.conf differ
702 Assuming a file named
704 with the following contents:
705 .Bd -literal -offset indent
706 FreeBSD is an operating system
708 OpenBSD is an operating system
713 excluding from the comparison those lines containing either
717 .Bd -literal -offset indent
718 $ echo "FreeBSD is an operating system" | diff -q -I 'Linux|Open' example.txt -
730 .%A M. Douglas McIlroy
731 .%T "An Algorithm for Differential File Comparison"
732 .%J Computing Science Technical Report
733 .%Q Bell Laboratories 41
739 utility is compliant with the
744 .Op Fl aDdIiLlNnPpqSsTtwXxy
745 are extensions to that specification.