How to Run “patch” Command on Linux?

Introduction

Before we begin talking about how to run “patch” command on Linux, let's briefly understand – What is patch command?

The built-in utility for patching files (whether they be text or source code files) on Linux is called patch. The patch file is provided as the input, and changes are made to the original file. We utilize the "diff" tool to identify discrepancies. The standard output contains a list of the "diff" (Differences) used to compare the contents of the two file modifications.

In this tutorial, you will run “patch” command on Linux. We will also address a few FAQs on how to run “patch” command on Linux.

What is a patch Command on Linux?

The "patch" command is a built-in tool for patching files, as was before explained. Run the following command in the terminal to check the patch command's version:

patch --version

Version 2.7.6 of the patch has been installed.

The patch command's syntax is listed below:

Syntax:

patch [Options] [Original_File] [Source_File]

The "patch" keyword, "options" for patching the files, "original file," and "source file" should all be typed. Even though the choices can be recovered using the command:

patch --help

Let's now examine how to use the patch command.

How to use the patch Command on Linux?

Let's generate two files and use the "diff" tool to apply the patch utility to them in order to use the patch file on Linux. Look carefully how the ensuing examples are implemented.

Create the Patch file through “diff”

Two files with the names "file.c" and "new_file.c" can be found in our directory. The original file (file.c) and add some C code there by using the nano editor:

include <stdio.h>
int main(){ 
printf("Welcome to vegastack\n");
}

Once the code has been added, use "Ctrl+O" to save the file and "Ctrl+X" to close the terminal.

Next, copy file.c (the original file) to new_file.c (the source file):

cp file.c new_file.c

The "new_file.c" has received a copy of the "file.c" file.

Let's update "new_file.c" with a few changes.

void main() {
 
printf("Hello world!")
printf("Welcome to vegastack\n");
}

After making changes, save the document.

Let's now use "diff" to create the patch file. Run the provided command in the terminal to achieve this.

diff -u file.c new_file.c > file.patch

The patch has already been made.

Patching File

Use the following terminal command to apply the patch:

patch < file.patch

The file has the patch applied to it:

The patch file's output is shown below:

Output 

--- file.c
+++ new_file.c
@@ -1,4 +1,5 @@
 #include <stdio.h>

-int main() {
+void main() {
 
+printf("Hello world!")
 printf("Welcome to vegastack\n");
 }

Generating Backup Before Patching the File

You can use the "b" flag in the command to create the backup file before patching:

patch -b < file.patch

The backup file will be made before the patch is applied. Let's use the "ls" command to verify it:

ls

The ".orig" extension is used to create the backup file.

Generating Backup File Version

Users can also use the backup file version-wise; to do so, use "numbered" and the "V" flag to designate the version in the backup file.

patch -b -V numbered < file.patch

When you run the aforementioned command, a backup file with the numbers (1, 2, 3) will be created. Let's use the "ls" command on the terminal to confirm it:

ls

The files shown in the figure above have three different iterations.

That's everything about the "patch" command.

FAQs to Run “patch” Command on Linux

How can I apply a patch file using the "patch" command? 

To apply a patch file, use the command: patch < patchfile. The patch file usually contains differences between the original and modified files.

Can I use the "patch" command to revert/undo patches? 

Yes, you can use the -R or --reverse option with the "patch" command to revert or undo a previously applied patch.

How do I specify the target file to patch? 

You can specify the target file by providing the file path as an argument after the patch command. For example, patch myfile.txt < patchfile.

Can I apply patches to multiple files simultaneously? 

Yes, the "patch" command supports applying patches to multiple files. Simply list the file paths as arguments after the patch command.

How can I check if a patch has been successfully applied? 

You can use the "-s" or "--silent" option to run the patch command silently. If the patch is applied successfully, there will be no output. Otherwise, an error message will be displayed.

Can I exclude certain files from being patched? 

Yes, the "patch" command provides the -N or --reject-file option to exclude specific files from being patched.

What if the file to be patched has already been modified?

If the file you are patching has already been modified, the patch may fail. You can use the -f or --force option to force the patch application, but be careful as it may result in conflicts.

Conclusion

The "patch" is the inbuilt utility for making changes to the files (both text and source code). It is used to apply the updated patch to the software. Using the "diff" tool, the patch file contains the differences between the source and original files. This tutorial has provided instructions on how to use Linux's "patch" command.