File Viewing & Editing

patch

Apply a diff file (patch) to an original file to update it.

Synopsis

syntax
patch [OPTION]... [ORIGINAL [PATCHFILE]]

Examples

Apply a patch from stdin
patch < fix.patch
Apply stripping one path component
patch -p1 < feature.diff
Reverse a previously applied patch
patch -R < fix.patch
Test a patch without applying
patch --dry-run -p1 < test.patch

Common options

FlagDescription
-pStrip N leading path components
-RReverse the patch
-bMake backup before patching
--dry-runTest without applying changes

About patch

The `patch` command apply a diff file (patch) to an original file to update it. Text viewing and editing commands are fundamental tools in any Linux user's toolkit.

Linux treats almost everything as a file, so the ability to quickly inspect, filter, transform, and edit file contents from the command line is critical. These commands are regularly combined with pipes and redirects to build powerful data-processing pipelines.

The command accepts 4 commonly used flags shown above, though the full set of options is available in the man page (`man patch`). The 4 examples on this page cover typical real-world usage patterns that you can copy and adapt for your own workflows.

Related commands

More File Viewing & Editing Commands

Other commands in the File Viewing & Editing category

Related tools