Linux patch Command: Apply Diff Files

The patch command applies a set of changes described in a diff file to one or more original files. It is the standard way to distribute and apply source code changes, security fixes, and configuration updates in Linux, and pairs directly with the diff
command.
This guide explains how to use the patch command in Linux with practical examples.
Syntax
The general syntax of the patch command is:
patch [OPTIONS] [ORIGINALFILE] [PATCHFILE]You can pass the patch file using the -i option or pipe it through standard input:
patch [OPTIONS] -i patchfile [ORIGINALFILE]
patch [OPTIONS] [ORIGINALFILE] < patchfilepatch Options
| Option | Long form | Description |
|---|---|---|
-i FILE | --input=FILE | Read the patch from FILE instead of stdin |
-p N | --strip=N | Strip N leading path components from file names in the patch |
-R | --reverse | Reverse the patch (undo a previously applied patch) |
-b | --backup | Back up the original file before patching |
--dry-run | Test the patch without making any changes | |
-d DIR | --directory=DIR | Change to DIR before doing anything |
-N | --forward | Skip patches that appear to be already applied |
-l | --ignore-whitespace | Ignore whitespace differences when matching lines |
-f | --force | Force apply even if the patch does not match cleanly |
-u | --unified | Interpret the patch as a unified diff |
Create and Apply a Basic Patch
The most common workflow is to use diff to generate a patch file and patch to apply it.
Start with two versions of a file. Here is the original:
print("Hello, World!")
print("Version 1")And the updated version:
print("Hello, Linux!")
print("Version 2")Use diff
with the -u flag to generate a unified diff and save it to a patch file:
diff -u hello.py hello_new.py > hello.patchThe hello.patch file will contain the following differences:
--- hello.py 2026-02-21 10:00:00.000000000 +0100
+++ hello_new.py 2026-02-21 10:05:00.000000000 +0100
@@ -1,2 +1,2 @@
-print("Hello, World!")
-print("Version 1")
+print("Hello, Linux!")
+print("Version 2")To apply the patch to the original file:
patch hello.py hello.patchpatching file hello.pyhello.py now contains the updated content from hello_new.py.
Dry Run Before Applying
Use --dry-run to test whether a patch will apply cleanly without actually modifying any files:
patch --dry-run hello.py hello.patchpatching file hello.pyIf the patch cannot be applied cleanly, patch reports the conflict without touching any files. This is useful before applying patches from external sources or when you are unsure whether a patch has already been applied.
Back Up the Original File
Use the -b option to create a backup of the original file before applying the patch. The backup is saved with a .orig extension:
patch -b hello.py hello.patchpatching file hello.pyAfter running this command, hello.py.orig contains the original unpatched file. You can restore it manually if needed.
Strip Path Components with -p
When a patch file is generated from a different directory structure than the one where you apply it, the file paths inside the patch may not match your local paths. Use -p N to strip N leading path components from the paths recorded in the patch.
For example, a patch generated with git diff will reference files like:
--- a/src/utils/hello.py
+++ b/src/utils/hello.pyTo apply this patch from the project root, use -p1 to strip the a/ and b/ prefixes:
patch -p1 < hello.patch-p1 is the standard option for patches generated by git diff and by diff -u from the root of a project directory. Without it, patch would look for a file named a/src/utils/hello.py on disk, which does not exist.
Reverse a Patch
Use the -R option to undo a previously applied patch and restore the original file:
patch -R hello.py hello.patchpatching file hello.pyThe file is restored to its state before the patch was applied.
Apply a Multi-File Patch
A single patch file can contain changes to multiple files. In this case, you do not specify a target file on the command line — patch reads the file names from the diff headers inside the patch:
patch -p1 < project.patchpatch processes each file name from the diff headers and applies the corresponding changes. Use -p1 when the patch was produced by git diff or diff -u from the project root.
Quick Reference
| Task | Command |
|---|---|
| Apply a patch | patch original.txt changes.patch |
| Apply from stdin | patch -p1 < changes.patch |
Specify patch file with -i | patch -i changes.patch original.txt |
| Dry run (test without applying) | patch --dry-run -p1 < changes.patch |
| Back up original before patching | patch -b original.txt changes.patch |
| Strip one path prefix (git patches) | patch -p1 < changes.patch |
| Reverse a patch | patch -R original.txt changes.patch |
| Ignore whitespace differences | patch -l original.txt changes.patch |
| Apply to a specific directory | patch -d /path/to/dir -p1 < changes.patch |
Troubleshooting
Hunk #1 FAILED at line N.
The patch does not match the current content of the file. This usually means the file has already been modified since the patch was created, or you are applying the patch against the wrong version. patch creates a .rej file containing the failed hunk so you can review and apply the change manually.
Reversed (or previously applied) patch detected! Assume -R?patch has detected that the changes in the patch are already present in the file — the patch may have been applied before. Answer y to reverse the patch or n to skip it. Use -N (--forward) to automatically skip already-applied patches without prompting.
patch: **** malformed patch at line N
The patch file is corrupted or not in a recognized format. Make sure the patch was generated with diff -u (unified format). Non-unified formats require the -c (context) or specific format flag to be passed to patch.
File paths in the patch do not match files on disk.
Adjust the -p N value. Run patch --dry-run -p0 < changes.patch first, then increment N (-p1, -p2) until patch finds the correct files.
FAQ
How do I create a patch file?
Use the diff command with the -u flag: diff -u original.txt updated.txt > changes.patch. The -u flag produces a unified diff, which is the format patch works with by default. See the diff command guide
for details.
What does -p1 mean?-p1 tells patch to strip one leading path component from file names in the patch. Patches generated by git diff prefix file paths with a/ and b/, so -p1 removes that prefix before patch looks for the file on disk.
How do I undo a patch I already applied?
Run patch -R originalfile patchfile. patch reads the diff in reverse and restores the original content.
What is a .rej file?
When patch cannot apply one or more sections of a patch (called hunks), it writes the failed hunks to a file with a .rej extension alongside the original file. You can open the .rej file and apply those changes manually.
What is the difference between patch and git apply?
Both apply diff files, but git apply is designed for use inside a Git repository and integrates with the Git index. patch is a standalone POSIX tool that works on any file regardless of version control.
Conclusion
The patch command is the standard tool for applying diff files in Linux. Use --dry-run to verify a patch before applying it, -b to keep a backup of the original, and -R to reverse changes when needed.
If you have any questions, feel free to leave a comment below.
Tags
Linuxize Weekly Newsletter
A quick weekly roundup of new tutorials, news, and tips.
About the authors

Dejan Panovski
Dejan Panovski is the founder of Linuxize, an RHCSA-certified Linux system administrator and DevOps engineer based in Skopje, Macedonia. Author of 800+ Linux tutorials with 20+ years of experience turning complex Linux tasks into clear, reliable guides.
View author page