How To

patch Command in Linux: Apply Diff Files and Reverse Changes

The patch command in Linux applies a set of changes from a diff file to one or more original files. It is the standard way to distribute source code changes, security fixes, and configuration updates, and pairs directly with the diff command.

This guide covers how to use the patch command in Linux with practical examples.

How the patch Command Works in Linux

patch reads a diff file and applies the changes it describes to the target file. The standard workflow is to generate the patch with diff -u and apply it with patch.

Generate a unified diff:

bashdiff -u hello.py hello_new.py > hello.patch

Apply it to the original file:

bashpatch hello.py hello.patch
patching file hello.py

You can also read the patch from standard input or specify it with -i:

bashpatch -p1 < hello.patchpatch -i hello.patch hello.py

The unified format (diff -u) is what patch expects by default. Non-unified formats require the -c flag. The patch exits with code 0 on full success, 1 if some hunks failed to apply, and 2 on a fatal error such as a missing file.

Apply a Patch, Dry Run, Backup, and Reverse

Dry run before applying. Use --dry-run to verify a patch applies cleanly without touching any files. This is essential before applying patches from external sources or multi-file patches where you are unsure about path alignment:

bashpatch --dry-run hello.py hello.patch

If conflicts exist, patch reports them without making any changes to disk.

Back up the original. Use -b to save the original with a .orig extension before patching. The backup lets you restore the unmodified file manually if the result is wrong:

bashpatch -b hello.py hello.patch

hello.py.orig now contains the pre-patch version.

Reverse a patch. Use -R to undo a previously applied patch and restore the file to its state before the patch was applied:

bashpatch -R hello.py hello.patch

Use -N in automated workflows to skip patches that are already applied. This prevents the interactive “Reversed (or previously applied) patch detected! Assume -R?” prompt.

Strip Path Components, Multi-File Patches, and Troubleshooting

The -p N option strips N leading path components from filenames in the patch header. Patches from git diff prefix filenames with a/ and b/:

--- a/src/utils/hello.py+++ b/src/utils/hello.py

Use -p1 to strip one component (a/ or b/), mapping the path to what exists on disk. Without -p1patch looks for a/src/utils/hello.py, which does not exist:

bashpatch -p1 < hello.patch

-p1 is the standard option for patches from git diff and from diff -u run at the project root.

Multi-file patches. When a patch contains changes to multiple files, omit the target filename. patch reads the filenames directly from the diff headers inside the patch file:

bashpatch -p1 < project.patch

Common errors:

  • Hunk #1 FAILED at line N — the file has been modified since the patch was created, or the wrong version is being patched. patch creates a .rej file with the failed hunks so you can apply them manually
  • Reversed (or previously applied) patch detected! — the changes are already present. Use -N to skip silently in scripts
  • malformed patch at line N — the patch is not in unified format. Regenerate with diff -u

Run patch --dry-run before any multi-file or externally sourced patch. Use -b to keep a backup and -p1 for patches from git diff. Leave a comment below if you run into any issues.

Cyber Defence

Recent Posts

basename Command in Linux: Strip Directory Paths and Suffixes

The basename command in Linux extracts the last component of a file path, removing the leading directory…

8 minutes ago

timeout Command in Linux: Kill Long-Running Commands Safely

The timeout command in Linux runs a command with a time limit and terminates it when the limit is reached. It is part of GNU coreutils, available on virtually every Linux distribution. It is most useful for commands with no built-in timeout option, such as ping, curl, tcpdump, or a custom script that might hang indefinitely. How the timeout Command Works in Linux The syntax is: bashtimeout [OPTIONS] DURATION COMMAND [ARG]...…

13 minutes ago

rmmod Command in Linux: Remove Kernel Modules and Blacklisting

The rmmod command in Linux removes a loaded module from the running kernel. Because the kernel has…

24 hours ago

free Command in Linux: Check Memory Usage and Interpret Output

The free command in Linux gives you a quick summary of RAM and swap usage. It reads…

1 day ago

diff Command in Linux: Compare Files and Create Patches

The diff command in Linux compares two text files line by line and shows the lines that…

1 day ago

Flush DNS Cache on Windows, Linux, and macOS: Quick Commands

DNS cache is a temporary database your OS and browser build as you browse. Each time you visit a website, the domain-to-IP mapping is saved locally, cutting out the round trip to a remote DNS server on repeat visits. Stale entries are the most common reason to flush the DNS cache: a server moves to a new IP, but your OS or browser still routes to the old address. This guide covers how to clear the DNS cache on Windows, Linux, and macOS, and how to flush the separate cache stored inside Chrome and Firefox. Flush DNS Cache on Windows The command is the same on Windows 10, Windows 11, and earlier supported releases. Open Command Prompt with administrator privileges. Type cmd in the Windows search bar, right-click Command Prompt, and select Run as…

1 day ago