Manual / diff
diff(1)
| DIFF(1) | General Commands Manual | DIFF(1) |
NAME
diff - compare two text files, or two directory trees, line by line
SYNOPSIS
diff [ -rb ] [ -e | -u | -c ] [ -U n ] [ -C n ] file1 file2
DESCRIPTION
Diff reports the lines that must change to turn file1 into file2. The edit script it reports is MINIMAL: both files are read whole, a longest-common-subsequence table is filled over whole lines, and the script is recovered by backtracking that table — not a heuristic that may report more changes than are needed.
With no option the output is the classic ed-script-compatible form: “2c2” style commands, with the deleted lines marked “< ” and the added lines marked “> ” around a “---” separator.
If both operands are directories, the entries of the two are compared in the byte order of their names (the C locale, so “Banana” precedes “apple”): a name in only one is reported as “Only in dir: name”, a pair of plain files is compared, and a pair of directories is named as “Common subdirectories: d1 and d2” (or, with -r, compared in turn). A name that is a directory in one and a plain file in the other is reported as “File d1 is a directory while file d2 is a regular file” and counts as a difference. Each pair of files that differs is preceded by a line “diff options d1/x d2/x” with the options as they were typed. If only one operand is a directory, the other is compared with the file of the same name in it.
OPTIONS
- -r
- Compare directories recursively.
- -b
- Treat trailing white space as nothing, and any run of white space as equal to any other run.
- -e
- Write a pure ed(1) script — commands only, no marked context lines — with the hunks in REVERSE order, so that earlier line numbers are still valid as ed applies each edit in turn.
- -u
- Write a UNIFIED context diff: two header lines naming the files, then one “@@ -l,s +l,s @@” hunk header per group of changes, followed by the changed and surrounding lines interleaved, prefixed “ ” (context), “-” (deleted) and “+” (added). This is the form patch(1) prefers.
- -c
- Write a CONTEXT diff: two header lines, then per group a “***************” separator, the file1 section (lines prefixed “ ”, “- ” or “! ”) and the file2 section (prefixed “ ”, “+ ” or “! ”). A section whose own side has no changed line at all is written as its header line alone, with no body — what every other diff does for a pure insertion or a pure deletion.
- -U n
- -C n
- Select the unified or context form AND set the number of copied context lines to n, instead of the default 3. Two groups of changes are printed as ONE hunk exactly while at most 2n unchanged lines separate them.
FILES
/usr/man/DIFF this page
SEE ALSO
DIAGNOSTICS
Exit status is 0 if the files are identical (and nothing is written at all), 1 if differences were found, and 2 for a usage error, a file that cannot be opened, or an input too large for the tables or the name arena (see BUGS), which is reported as “diff: input too large” on standard error. A directory tree is never compared from a truncated listing: it is refused whole.
BUGS
Capacity is fixed at build time and a file past it is REFUSED, never silently truncated: 120 lines per file, 6000 bytes of text across both files, and a comparison table bounded by (lines1+1)×(lines2+1) ≤ 10000 — roughly 99 lines against 99.
The -u and -c header lines carry the pathname alone. POSIX leaves the timestamp that usually follows it unspecified, and a ProDOS file has no meaningful modification time on a machine that booted without a clock, so none is written. patch(1) here reads the name to a tab OR to end of line, so both forms apply.
-r reads each directory whole into a 12000-byte name arena (17 bytes an entry, both listings of every level still being walked live in it) and closes it before anything else opens, so the walk never holds more than the two files being compared. A tree whose listings do not fit, one more than 16 directories deep, or one whose pathname passes the 64 characters ProDOS allows, is refused with exit status 2. Each pair of files is still bounded as above.
Where the hosts disagree the page follows POSIX: a file against a directory of the same name is a difference, exit 1 (GNU says 1, BSD says 0). The same file named twice is identical and is not opened twice.
| October 8, 2026 | maux |