Skip to content
Tutorials Center
My learning

Topics

Linux Command Reference ยท Viewing and Searching Files

Linux patch command

Reference for the Linux patch command: apply unified diffs to files and source trees, strip path prefixes with -p, test with --dry-run and reverse patches.

patch reads a description of changes (usually produced by diff -u) and applies it to files. A patch is a small text file, so a change can be mailed, reviewed, stored in version control and applied later, instead of shipping whole copies of the changed files.

patch is tolerant: it matches hunks by their context lines, not only by line numbers, so a patch usually still applies if the target changed elsewhere. When a hunk cannot be placed, patch saves it in a .rej file and tells you.

Terminal
patch [OPTION]... [ORIGFILE [PATCHFILE]]
patch -p1 < change.patch

Try it

Bash Create and apply a patch

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Bash Trees, -p1, dry runs and reverting

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Syntax

Terminal
patch [-pNUM] [-R] [-N] [-b] [--dry-run] [-i PATCHFILE] [-d DIR] [FILE]
Option Description
-pN Strip N leading path components from file names in the patch. -p1 turns a/src/x.c into src/x.c.
-i FILE Read the patch from FILE instead of standard input.
-R Reverse: undo a patch that was already applied.
-N Ignore patches that look already applied (or reversed).
--dry-run Show what would happen without changing any file.
-b Keep a backup of each changed file as name.orig.
-d DIR Change to DIR before doing anything.
-F N, --fuzz=N Allow up to N lines of context to differ (default 2).
-l Ignore differences in white space when matching.
-E Remove files that become empty.
--no-backup-if-mismatch Do not leave .orig files when a hunk needed fuzz or an offset.

Returns: 0 when every hunk applied, 1 when some hunks failed (written to .rej files), 2 for serious trouble.

Usage notes

  • Choosing -p. Look at the file names in the patch headers. If they start with a/ and b/ (as Git produces), use -p1 from the project root. If the paths are already relative to your folder, omit -p. A wrong value makes patch ask which file to patch, or fail with can't find file to patch.
  • Always try --dry-run first on a patch from someone else. It reports hunks that would fail without touching the tree.
  • Failed hunks. Look for file.rej next to the file. It contains the hunks that did not apply, in diff format, so you can merge them by hand.
  • Offsets and fuzz. Messages like Hunk #1 succeeded at 12 (offset 3 lines) are normal. fuzz means context lines were ignored to find a match; check the result with extra care.
  • Order matters. Apply a series of patches in the order they were made; each one assumes the result of the previous one.
  • Git patches. patch -p1 understands the textual part of git diff output. Git itself has git apply, which is stricter and can also handle renames and binary data.
  • Line endings. A patch made against LF files may fail on CRLF files; convert line endings or use --binary carefully.
  • Not a security boundary. A patch can modify any path it names, so inspect patches from untrusted sources before applying them.