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.
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.
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.
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 -p1, patch 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 manuallyReversed (or previously applied) patch detected! — the changes are already present. Use -N to skip silently in scriptsmalformed patch at line N — the patch is not in unified format. Regenerate with diff -uRun 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.
The basename command in Linux extracts the last component of a file path, removing the leading directory…
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]...…
The rmmod command in Linux removes a loaded module from the running kernel. Because the kernel has…
The free command in Linux gives you a quick summary of RAM and swap usage. It reads…
The diff command in Linux compares two text files line by line and shows the lines that…
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…