maux
Star on GitHub Buy me a coffee

Manual / patch

patch(1)

PATCH(1) General Commands Manual PATCH(1)

patch - apply a diff to a file

patch [ -b ] [ -R ] [ -p num ] [ -i patchfile ] [ -o outfile ] [ file ]

Patch reads a difference listing — what diff(1) writes — and applies it to a file, which is how a change made somewhere else arrives here.

The listing is read from patchfile if -i is given and from the standard input otherwise. Its FORM is detected, never declared: all three of diff's output forms are understood, the unified one (@@ -l,s +l,s @@), the context one (*** l,l **** / --- l,l ----) and the normal one (3c3, 3a4, 3,5d2). Text before the first hunk is skipped, so a patch that arrived inside a mail message still applies.

The file to patch is the file operand if one is given. Otherwise it is taken from the listing's own header lines — the old name first, then the new one, the first of the two that can be opened.

Each hunk is matched EXACTLY: every one of its context and deleted lines must equal, whole line for whole line, a run of lines in the file. Where that run sits is guessed from the line number the hunk states, corrected by the lines earlier hunks have already added or removed; if the guess misses, the run is searched for outward from it. A hunk that matches nowhere is REJECTED: the file keeps its original text there, and the hunk is written, verbatim and in the form it arrived in, to file.rej.

Save the original, unpatched contents as file.orig before writing anything. Without it nothing is saved.
Apply the listing BACKWARDS, as though its two files had been given to diff in the other order — which is how a change is undone.
Delete num leading pathname components from every pathname the listing's header names, so that a patch made in another directory still names a file here.
Read the listing from patchfile instead of the standard input.
Write the patched result to outfile and leave the original file untouched.

/usr/man/PATCH this page

the original contents, with -b
the hunks that could not be applied

diff(1), ed(1)

Exit status is 0 if every hunk applied, 1 if one or more hunks were rejected, and 2 for an error — a usage error, a patch or target that cannot be read, a listing with no hunk in it, a malformed hunk, or an input past the limits in BUGS. Progress and failure messages go to standard error, not standard output: the two reference implementations word them differently, so nothing here should be parsed for them.

Capacity is fixed at build time and an input past it is REFUSED, never silently truncated: 250 lines and 12000 bytes of text in the file being patched, 120 lines and 2400 bytes in any ONE hunk.

A derived name must still be a legal ProDOS name — 15 characters including the extension, enforced by the pathname parser rather than at creation — so a base name longer than 11 characters is TRUNCATED to 11 before .orig or .rej is appended.

One file per invocation. A listing that names several files (what diff -r would produce, which this diff cannot) applies only its first.

There is no FUZZ factor and no -l (match ignoring white space): a context line that differs by so much as a blank makes its hunk fail. There is no -N either, so a patch that has already been applied is not recognised as such — its hunks simply fail and are rejected.

Every line written ends in a newline, including the last, so patching a file whose final line had none adds one.

September 17, 2026 maux