
patch Command in Linux
The patch command in Linux applies a diff or patch file to the original. It takes a patch file (patchfile) created by the diff program and applies the changes to one or more original files, updating them with the modifications. It is commonly used to update source code, configuration files, or text documents.
Table of Contents
Here is a comprehensive guide to the options available with the patch command â
Syntax of patch Command
The syntax of the patch command in Linux is as follows −
patch -p<num> < patchfile
In the above syntax, the -p<num> is used to specify how many leading directory levels to remove from file paths in the patch, and < patchfile is used to read the patch file and apply the changes.
Patch files may include full or relative file paths. The -p<num> option determines how many leading directory components to remove before applying the patch.
The other method to apply the patch is given below −
patch [options] [originalfile [patchfile]]
The [options] field is used to mention flags that modify the behavior of the patch. The [originalfile] is the file to be patched, and [patchfile] is the file containing the changes (optional if provided via standard input).
patch Command Options
The options of the Linux patch command are listed below −
Option | Long Form | Description |
---|---|---|
-p NUM | --strip=NUM | Strip NUM leading components from file names. |
-F LINES | --fuzz LINES | Set the fuzz factor to LINES for inexact matching. |
-l | --ignore-whitespace | Ignore white space changes between patch and input. |
-c | --context | Interpret the patch as a context difference. |
-ed | --e | Interpret the patch as an ed script. |
-n | --normal | Interpret the patch as a normal difference. |
-u | --unified | Interpret the patch as a unified difference. |
-N | --forward | Ignore patches that appear to be reversed or already applied. |
-R | --reverse | Assume patches were created with old and new files swapped. |
-i PATCHFILE | --input=PATCHFILE | Read patch from PATCHFILE instead of stdin. |
-t | --batch | Ask no questions; skip bad prerequisite patches; assume reversed. |
-f | --force | Like -t, but ignore bad prerequisite patches and assume unreversed. |
-s | --quiet, --silent | Work silently unless an error occurs. |
--verbose | Output extra information about the work being done. | |
--dry-run | Do not actually change any files; just print what would happen. | |
-d DIR | --directory=DIR | Change the working directory to DIR first. |
--reject-format=FORMAT | Create context or unified rejects. | |
--binary | Read and write data in binary mode. | |
-v | --version | Output version info. |
--help | Output this help. |
The output options are listed in the following table −
Option | Long Form | Description |
---|---|---|
-o FILE | --output=FILE | Output patched files to FILE. |
-r FILE | --reject-file=FILE | Output rejects to FILE. |
-D NAME | --ifdef=NAME | Make merged if-then-else output using NAME. |
--merge | Merge using conflict markers instead of creating reject files. | |
-E | --remove-empty-files | Remove output files that are empty after patching. |
-Z | --set-utc | Set times of patched files, assuming diff uses UTC (GMT). |
-T | --set-time | Likewise, assuming local time. |
--quoting-style=WORD | Output file names using quoting style WORD. |
The backup and version control options are given below −
Option | Long Form | Description |
---|---|---|
-b | --backup | Back up the original contents of each file. |
--backup-if-mismatch | Back up if the patch does not match exactly. | |
--no-backup-if-mismatch | Back up mismatches only if otherwise requested. | |
-V STYLE | --version-control=STYLE | Use STYLE version control (simple, numbered, existing). |
-B PREFIX | --prefix=PREFIX | Prepend PREFIX to backup file names. |
-Y PREFIX | --basename-prefix=PREFIX | Prepend PREFIX to backup file basenames. |
-z SUFFIX | --suffix=SUFFIX | Append SUFFIX to backup file names. |
-g NUM | --get=NUM | Get files from RCS, etc., if positive; ask if negative. |
Examples patch Command in Linux
This section explores how to use the patch command in Linux with examples −
- Applying Patch to a File
- Applying Patch with Specific Directory Strip Level
- Reversing the Patch
- Checking a Patch Without Applying
- Saving Output to a New File
- Creating a Backup of Patched File
- Displaying Usage Help
Applying Patch to a File
To understand how the patch command works, first create an original file.
echo "Hello, Linux!" > file.txt
Now, create a modified file −
echo "Hello, Tutorialspoint!" > mod_file.txt
Next, generate a patch file using the diff command −
diff -u file.txt mod_file.txt > file.patch

Finally, apply the patch using the patch command −
patch file.txt < file.patch
Here is the content of the file.txt after patching, shown in the image below −

Applying Patch with Specific Directory Strip Level
First, create a directory structure with original and modified files −
sudo mkdir -p myproject/original sudo mkdir -p myproject/modified

Now, create files in the respective folders −
echo "Hello, Linux!" | sudo tee myproject/original/file.txt echo "Hello, Tutorialspoint!" | sudo tee myproject/modified/file.txt

Create the patch file −
diff -u myproject/original/file.txt myproject/modified/file.txt > file.patch

Apply the patch using the patch command −
patch < file.patch
The above command throws an error, as shown in the image below −

This fails because the patch command expects the full path (project/original/file.txt).
To fix it, change the directory to the original file folder. Use the -p2 option to strip the first level of directories −
cd myproject/original sudo patch -p2 < ../../file.patch

In the above command, the patch -p2 < ../../file.patch removes two leading components (myproject/original), leaving file.txt. This indicates that file.txt is located directly in ~/myproject/original/.
Reversing the Patch
To reverse the patch or undo the changes, use the -R option −
patch -R file.txt < file.patch

The file reverts to its original content.
Checking a Patch Without Applying
To check if the patch can be applied without modifying the file, use the --dry-run option −
patch --dry-run file.txt < file.patch
Saving Output to a New File
To save the output to a new file, use the -o or --output option with the filename −
patch -o output.txt file.txt < file.patch

In the above command, instead of modifying file.txt, the patched version is saved as output.txt.
Creating a Backup of Patched File
To create a backup of the patched file, use the -b or --backup option −
patch -b file.txt < file.patch

Displaying Usage Help
To display the usage help for the patch command, use the --help option −
patch --help
Conclusion
The patch command in Linux is a useful tool for applying changes from a patch file to one or more original files. It works with patch files generated by the diff command and is commonly used for updating source code, configuration files, or text documents.
Various options allow control over how patches are applied, including handling directory paths, reversing changes, checking a patch before applying it, and creating backups.