DIFFCOLORS
The value of this variable is the form add:rm, where add is the ASCII escape sequence for additions and rm is the ASCII escape sequence for deletions. If this is unset, diff uses green for additions and red for removals.
Differential file and directory comparator
diff [-aBbdipTtw]
[{-c | -e | -f | -n | -q | -u | -y}]
[{-A
algo | --algorithm
algo}]
[--brief]
[--color=when]
[--changed-group-format
GFMT]
[--ed]
[--expand-tabs]
[--forward-ed]
[--ignore-all-space]
[--ignore-case]
[--ignore-space-change]
[--initial-tab]
[--minimal]
[--no-dereference]
[--no-ignore-file-name-case]
[--normal]
[--rcs]
[--show-c-function]
[--starting-file]
[--speed-large-files]
[--strip-trailing-cr]
[--tabsize
number]
[--text]
[--unified]
[{-I
pattern | --ignore-matching-lines
pattern}]
[{-F
pattern | --show-function-line
pattern}]
[{-L
label | --label
label}]
file1
file2
diff {[-aBbdilpTtw]
[{-A
algo | --algorithm
algo}]
[{-I
pattern | --ignore-matching-lines
pattern}]
[{-F
pattern | --show-function-line
pattern}]
[{-L
label | --label
label}]
[--brief]
[--color=when]
[--changed-group-format
GFMT]
[--ed]
[--expand-tabs]
[--forward-ed]
[--ignore-all-space]
[--ignore-case]
[--ignore-space-change]
[--initial-tab]
[--minimal]
[--no-dereference]
[--no-ignore-file-name-case]
[--normal]
[--paginate]
[--rcs]
[--show-c-function]
[--speed-large-files]
[--starting-file]
[--strip-trailing-cr]
[--tabsize
number]
[--text]
-C
number | --context
number
file1
file2}
diff {[-aBbdiltw]
[{-A
algo | --algorithm
algo}]
[{-I
pattern | --ignore-matching-lines
pattern}]
[--brief]
[--color=when]
[--changed-group-format
GFMT]
[--ed]
[--expand-tabs]
[--forward-ed]
[--ignore-all-space]
[--ignore-case]
[--ignore-space-change]
[--initial-tab]
[--minimal]
[--no-dereference]
[--no-ignore-file-name-case]
[--normal]
[--paginate]
[--rcs]
[--show-c-function]
[--speed-large-files]
[--starting-file]
[--strip-trailing-cr]
[--tabsize
number]
[--text]
-D
string | --ifdef
string
file1
file2}
diff {[-aBbdilpTtw]
[{-A
algo | --algorithm
algo}]
[{-I
pattern | --ignore-matching-lines
pattern}]
[{-F
pattern | --show-function-line
pattern}]
[{-L
label | --label
label}]
[--brief]
[--color=when]
[--changed-group-format
GFMT]
[--ed]
[--expand-tabs]
[--forward-ed]
[--ignore-all-space]
[--ignore-case]
[--ignore-space-change]
[--initial-tab]
[--minimal]
[--no-dereference]
[--no-ignore-file-name-case]
[--normal]
[--paginate]
[--rcs]
[--show-c-function]
[--speed-large-files]
[--starting-file]
[--strip-trailing-cr]
[--tabsize
number]
[--text]
-U
number | --unified
number
file1
file2}
diff [-aBbdilNPprsTtw]
[{-c | -e | -f | -n | -q | -u}]
[{-A
algo | --algorithm
algo}]
[--brief]
[--color=when]
[--changed-group-format
GFMT]
[--context]
[--ed]
[--expand-tabs]
[--forward-ed]
[--ignore-all-space]
[--ignore-case]
[--ignore-space-change]
[--initial-tab]
[--minimal]
[--new-file]
[--no-dereference]
[--no-ignore-file-name-case]
[--normal]
[--paginate]
[--rcs]
[--recursive]
[--report-identical-files]
[--show-c-function]
[--speed-large-files]
[--strip-trailing-cr]
[--tabsize
number]
[--text]
[--unidirectional-new-file]
[--unified]
[{-I
pattern | --ignore-matching-lines
pattern}]
[{-F
pattern | --show-function-line
pattern}]
[{-L
label | --label
label}]
[{-S
name | --starting-file
name}]
[{-X
file | --exclude-from
file}]
[{-x
pattern | --exclude
pattern}]
dir1
dir2
diff {[-aBbditwW]
[--color=when]
[--expand-tabs]
[--ignore-all-space]
[--ignore-blank-lines]
[--ignore-case]
[--minimal]
[--no-dereference]
[--no-ignore-file-name-case]
[--strip-trailing-cr]
[--suppress-common-lines]
[--tabsize
number]
[--text]
[--width]
-y | --side-by-side
file1
file2}
diff [--help]
[--version]
The diff utility compares the contents of file1 and file2 and writes to the standard output the list of changes necessary to convert one file into the other. No output is produced if the files are identical.
Output options (mutually exclusive):
-C number --context number
Like -c but produces a diff with number lines of context.
-c
Produces a diff with 3 lines of context. With -c the output format is modified slightly: the output begins with identification of the files involved and their creation dates and then each change is separated by a line with fifteen *'s. The lines removed from file1 are marked with ‘- ’; those added to file2 are marked ‘+ ’. Lines which are changed from one file to the other are marked in both files with ‘! ’. Changes which lie within 3 lines of each other are grouped together on output.
-D string --ifdef string
Creates a merged version of file1 and file2 on the standard output, with C preprocessor controls included so that a compilation of the result without defining string is equivalent to compiling file1, while defining string will yield file2.
-e --ed
Produces output in a form suitable as input for the editor utility, ed(1), which can then be used to convert file1 into file2.
Note that when comparing directories with -e, the resulting file may no longer be interpreted as an ed(1) script. Output is added to indicate which file each set of ed(1) commands applies to. These hunks can be manually extracted to produce an ed(1) script, which can also be applied with patch(1).
-f --forward-ed
Identical output to that of the -e flag, but in reverse order. It cannot be digested by ed(1).
--help
This option prints a summary to stdout and exits with status 0.
-n
Produces a script similar to that of -e, but in the opposite order and with a count of changed lines on each insert or delete command. This is the form used by rcsdiff.
-q --brief
Just print a line when the files differ. Does not output a list of changes.
-U number --unified number
Like -u but produces a diff with number lines of context.
-u
Produces a unified diff with 3 lines of context. A unified diff is similar to the context diff produced by the -c option. However, unlike with -c, all lines to be changed (added and/or removed) are present in a single section.
--version
This option prints a version string to stdout and exits with status 0.
-y --side-by-side
Output in two columns with a marker between them. The marker can be one of the following:
Corresponding lines are identical.
Corresponding lines are different.
Files differ and only the first file contains the line.
Files differ and only the second file contains the line.
Comparison options:
-A algo, --algorithm algo
Configure the algorithm used when comparing files. diff supports 3 algorithms:
myersThe Myers diff algorithm finds the shortest edit which transforms one input into the other. It generally runs in O(N+D²) time, requiring O(N) space, where N is the sum of the lengths of the inputs and D is the length of the difference between them, with a theoretical O(N·D) worst case. If it encounters worst-case input, the implementation used by diff falls back to a less optimal but faster algorithm.
patienceThe Patience variant of the Myers algorithm attempts to create more aesthetically pleasing diff output by logically grouping lines.
stoneThe Stone algorithm (commonly known as Hunt-McIlroy or Hunt-Szymanski) looks for the longest common subsequence between compared files. Stone encounters worst case performance when there are long common subsequences. In large files this can lead to a significant performance impact. The Stone algorithm is maintained for compatibility.
The default algorithm is affected by the POSIXLY_CORRECT and POSIX_PEDANTIC environment variables. When either variable is set the default algorithm will be “stone”. Otherwise, the diff utility defaults to the Myers algorithm, but will fall back to the Stone algorithm if the input or output options are not supported by the Myers implementation.
-a --text
Treat all files as ASCII text. Normally diff will simply print “Binary files ... differ” if files contain binary characters. Use of this option forces diff to produce a diff.
-B --ignore-blank-lines
Causes chunks that include only blank lines to be ignored.
-b --ignore-space-change
Causes trailing blanks (spaces and tabs) to be ignored, and other strings of blanks to compare equal.
--color=[when]
Color the additions green, and removals red, or the value in the DIFFCOLORS environment variable. The possible values of when are “never”, “always” and “auto”. auto will use color if the output is a tty and the COLORTERM environment variable is set to a non-empty string.
-d --minimal
Try very hard to produce a diff as small as possible. This may consume a lot of processing power and memory when processing large files with many changes.
-F pattern, --show-function-line pattern
Like -p, but display the last line that matches provided pattern.
-I pattern --ignore-matching-lines pattern
Ignores changes, insertions, and deletions whose lines match the extended regular expression pattern. Multiple -I patterns may be specified. All lines in the change must match some pattern for the change to be ignored. See re_format(7) for more information on regular expression patterns.
-i --ignore-case
Ignores the case of letters. E.g., “A” will compare equal to “a”.
-l --paginate
Pass the output through pr(1) to paginate it.
-L label --label label
Print label instead of the first (and second, if this option is specified twice) file name and time in the context or unified diff header.
-p --show-c-function
With unified and context diffs, show with each change the first 40 characters of the last line before the context beginning with a letter, an underscore or a dollar sign. For C and Objective-C source code following standard layout conventions, this will show the prototype of the function the change applies to.
-T --initial-tab
Print a tab rather than a space before the rest of the line for the normal, context or unified output formats. This makes the alignment of tabs in the line consistent.
-t --expand-tabs
Will expand tabs in output lines. Normal or -c output adds character(s) to the front of each line which may screw up the indentation of the original source lines and make the output listing difficult to interpret. This option will preserve the original source's indentation.
-w --ignore-all-space
Is similar to -b --ignore-space-change but causes whitespace (blanks and tabs) to be totally ignored. E.g., “if ( a == b )” will compare equal to “if(a==b)”.
-W number --width number
Output at most number columns when using side by side format. The default value is 130. Note that unless -t was specified, diff will always align the second column to a tab stop, so values of --width smaller than approximately five times the value of --tabsize may yield surprising results.
--changed-group-format GFMT
Format input groups in the provided
the format is a string with special keywords:
lines from FILE1
lines from FILE2
--ignore-file-name-case
ignore case when comparing file names
--no-dereference
do not follow symbolic links
--no-ignore-file-name-case
do not ignore case when comparing file names (default)
--normal
default diff output
--speed-large-files
stub option for compatibility with GNU diff
--strip-trailing-cr
strip carriage return on input files
--suppress-common-lines
Do not output common lines when using the side by side format
--tabsize number
Number of spaces representing a tab (default 8)
Directory comparison options:
-N --new-file
If a file is found in only one directory, act as if it was found in the other directory too but was of zero size.
-P --unidirectional-new-file
If a file is found only in dir2, act as if it was found in dir1 too but was of zero size.
-r --recursive
Causes application of diff recursively to common subdirectories encountered.
-S name --starting-file name
Re-starts a directory diff in the middle, beginning with file name.
-s --report-identical-files
Causes diff to report files which are the same, which are otherwise not mentioned.
-X file --exclude-from file
Exclude files and subdirectories from comparison whose basenames match lines in file. Multiple -X options may be specified.
-x pattern --exclude pattern
Exclude files and subdirectories from comparison whose basenames match pattern. Patterns are matched using shell-style globbing via fnmatch(3). Multiple -x options may be specified.
If both arguments are directories, diff sorts the contents of the directories by name, and then runs the regular file diff algorithm, producing a change list, on text files which are different. Binary files which differ, common subdirectories, and files which appear in only one directory are described as such. In directory mode only regular files and directories are compared. If a non-regular file such as a device special file or FIFO is encountered, a diagnostic message is printed.
If only one of file1 and file2 is a directory, diff is applied to the non-directory file and the file contained in the directory file with a filename that is the same as the last component of the non-directory file.
If either file1 or file2 is ‘-’, the standard input is used in its place.
The default (without -e, -c, or -n --rcs options) output contains lines of these forms, where XX, YY, ZZ, QQ are line numbers respective of file order.
aYYAt (the end of) line XX of file1, append the contents of line YY of file2 to make them equal.
aYY,ZZSame as above, but append the range of lines, YY through ZZ of file2 to line XX of file1.
dYYAt line XX delete the line. The value YY tells to which line the change would bring file1 in line with file2.
dZZDelete the range of lines XX through YY in file1.
cYYChange the line XX in file1 to the line YY in file2.
cZZReplace the range of specified lines with the line ZZ.
cZZ,QQReplace the range XX,YY from file1 with the range ZZ,QQ from file2.
These lines resemble ed(1) subcommands to convert file1 into file2. The line numbers before the action letters pertain to file1; those after pertain to file2. Thus, by exchanging a for d and reading the line in reverse order, one can also determine how to convert file2 into file1. As in ed(1), identical pairs (where num1 = num2) are abbreviated as a single number.
DIFFCOLORS
The value of this variable is the form add:rm, where add is the ASCII escape sequence for additions and rm is the ASCII escape sequence for deletions. If this is unset, diff uses green for additions and red for removals.
/tmp/diff.XXXXXXXX
Temporary file used when comparing a device or the standard input. Note that the temporary file is unlinked as soon as it is created so it will not show up in a directory listing.
The diff utility exits with one of the following values:
0
No differences were found.
1
Differences were found.
>1
An error occurred.
The --help and --version options exit with a status of 0.
Compare old_dir and new_dir recursively generating an unified diff and treating files found only in one of those directories as new files:
$ diff -ruN /path/to/old_dir /path/to/new_dir
Same as above but excluding files matching the expressions “*.h” and “*.c”:
$ diff -ruN -x '*.h' -x '*.c' /path/to/old_dir /path/to/new_dir
Show a single line indicating if the files differ:
$ diff -q /boot/loader.conf /boot/defaults/loader.conf Files /boot/loader.conf and /boot/defaults/loader.conf differ
Assuming a file named example.txt with the following contents:
FreeBSD is an operating system Linux is a kernel OpenBSD is an operating system
Compare stdin with example.txt excluding from the comparison those lines containing either "Linux" or "Open":
$ echo "FreeBSD is an operating system" | diff -q -I 'Linux|Open' example.txt -
The unified diff format's timestamps are formatted differently in legacy mode. By default, diff does not include nanoseconds or a timezone in unified diff timestamps. In legacy mode, nanoseconds and a timezone are both included. Note that patch(1) may not be able to process timestamps in the legacy format.
For more information about legacy mode, see compat(5).
163 releases since Mac OS X 10.0; the text changed 8 times.
--algorithm since macOS 13.3;
--changed-group-format since macOS 13.0;
--color since macOS 13.0;
--context since macOS 13.0;
--exclude since macOS 13.0;
--exclude-from since macOS 13.0;
--forward-ed since macOS 13.0;
--help since Mac OS X 10.4;
--ifdef since macOS 13.0;
--ignore-all-space since macOS 26.4;
--ignore-file-name-case since Mac OS X 10.4;
--ignore-matching-lines since macOS 13.0;
--no-dereference since macOS 26.4;
--no-ignore-file-name-case since Mac OS X 10.4;
--normal since Mac OS X 10.4;
--show-function-line since macOS 13.0;
--starting-file since macOS 13.0;
--strip-trailing-cr since Mac OS X 10.4;
--tabsize since macOS 13.0;
--unified since macOS 13.0;
--width since macOS 13.0;
-A since macOS 13.1;
-C since macOS 13.0;
-D since macOS 13.0;
-F since macOS 13.0;
-I since macOS 13.0;
-L since macOS 13.0;
-P since macOS 13.0;
-S since macOS 13.0;
-U since macOS 13.0;
-W since macOS 13.0;
-X since macOS 13.0;
-c since macOS 13.0;
-f since macOS 13.0;
-u since macOS 13.0;
-x since macOS 13.0.
--GTYPE-group-format until macOS 12.5;
--LTYPE-line-format until macOS 12.5;
--from-file until macOS 12.5;
--horizon-lines until macOS 12.5;
--ignore-all-blanks until macOS 26.3;
--ignore-tab-expansion until macOS 12.5;
--left-column until macOS 12.5;
--line-format until macOS 12.5;
--new-group-format until Mac OS X 10.3.9;
--new-line-format until Mac OS X 10.3.9;
--old-group-format until Mac OS X 10.3.9;
--old-line-format until Mac OS X 10.3.9;
--rcs until macOS 12.5;
--sdiff-merge-assist until Mac OS X 10.3.9;
--to-file until macOS 12.5;
--unchanged-group-format until Mac OS X 10.3.9;
--unchanged-line-format until Mac OS X 10.3.9;
-E until macOS 12.5;
-H until Mac OS X 10.3.9;
-algorithm until macOS 13.2;
-h until Mac OS X 10.3.9;
-lines until Mac OS X 10.3.9;
-v until macOS 12.5.