DIFF(1) | General Commands Manual | DIFF(1) |
diff
—
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-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-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-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-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-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-blanks ]
[--ignore-blank-lines ]
[--ignore-case ]
[--minimal ]
[--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-c
but produces a diff with
number lines of context.-c
-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-e
--ed
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
-e
flag, but in
reverse order. It cannot be digested by ed(1).--help
-n
-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
-U
number --unified
number-u
but produces a diff with
number lines of context.-u
-c
option. However, unlike
with -c
, all lines to be changed (added and/or
removed) are present in a single section.--version
-y
--side-by-side
Comparison options:
-A
algo, --algorithm
algodiff
supports 3 algorithms:
myers
patience
stone
The default diff algorithm when this flag is not given is
“myers
”.
diff
will fallback to the
“stone
” algorithm if the
“myers
” algorithm cannot be
supported with the given options and the algorithm has not been set
explicitly.
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
”.
If the diff algorithm is selected, but cannot be supported
with the given options diff
will produce an
error.
-a
--text
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
-b
--ignore-space-change
--color=
[when]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
-F
pattern, --show-function-line
pattern-p,
but display the last line that matches
provided pattern.-I
pattern
--ignore-matching-lines
pattern-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
-l
--paginate
-L
label --label
label-p
--show-c-function
-T
--initial-tab
-t
--expand-tabs
-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-blanks
-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-t
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
GFMTthe format is a string with special keywords:
--ignore-file-name-case
--no-ignore-file-name-case
--normal
--speed-large-files
--strip-trailing-cr
--suppress-common-lines
--tabsize
numberDirectory comparison options:
-N
--new-file
-P
--unidirectional-new-file
-r
--recursive
diff
recursively to common
subdirectories encountered.-S
name --starting-file
namediff
in the middle,
beginning with file name.-s
--report-identical-files
diff
to report files which are the same,
which are otherwise not mentioned.-X
file --exclude-from
file-X
options may be specified.-x
pattern --exclude
pattern-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.
XX
a
YY
XX
a
YY,ZZ
XX
d
YY
XX,YY
d
ZZ
XX
c
YY
XX,YY
c
ZZ
XX,YY
c
ZZ,QQ
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
diff
uses green for additions and
red for removals.The diff
utility exits with one of the
following values:
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).
cmp(1), comm(1), diff3(1), ed(1), patch(1), pr(1), sdiff(1), compat(5)
James W. Hunt and M. Douglas McIlroy, An Algorithm for Differential File Comparison, Computing Science Technical Report, Bell Laboratories 41, June 1976.
The diff
utility is compliant with the
IEEE Std 1003.1-2008 (“POSIX.1”)
specification.
The flags [-AaDdIiLlNnPpqSsTtwXxy
] are
extensions to that specification.
A diff
command appeared in
Version 6 AT&T UNIX.
libdiff was imported from the Game of Trees version control system and default algorithm was changed to Myers for FreeBSD 15.
February 26, 2024 | Mac OS X 14 |