How to Run “patch” Command in Linux?

23

The “patch” is a command for adding patch files to source code or text files. It takes input as a patch file and applies differences to original files. We use the “diff” tool to get the difference.

The “diff” is abbreviated as “differences” and is used to compare the content of two files and list the changes in standard output.

A set of source codes makes up a piece of software. Developers build the source code that evolves over time. Getting a new file for each update is unrealistic or time-consuming. Therefore, the safest method is to distribute improvements only. The modifications are made to the old file, and then a new or patched file is created for the new software version.

This guide shows you how to use the “diff” command to generate a patch file and then apply it with the “patch” command.

Syntax:

The syntax of the “patch” command is as follows:

$ patch [options] [originalfile [patchfile]]

$ patch -pnum patchfile>

Creating a Patch File Using “diff”:

Source Code File 1:

Firstly, two different versions of a source code are required to create a patch file. The source code file I have created is named as “myfile.c”:s

#include

int main() {

printf(“Hello LinuxHintn);

}

Source Code File 2:

Now, copy the content of myfile.c in the new_myfile.c, using:

$ cp myfile.c new_myfile.c

Make some changes in the newly-created file:

#include

void main(){  

printf(” Hello Linux hint”);  

printf(“Welcome to linuxhint”);

}

Checking Difference:

Let’s create a patch file named as myfile.patch:

$ diff -u myfile.c new_myfile.c myfile.patch

You can print the patch file by executing the command below:

Applying the Patch File:

To apply patch, use:

Ensure that the patch file is in the directory where the source code file is placed.

Take a Backup Before Applying Patch:

Use “-b” option to build a backup of the patch file:

$ patch -b myfile.patch

Setting Backup File Version

If you need multiple backups of a single backup file, then use the “-V” option. It sets the version number of each backup file. Execute the command given below:

$ patch -b -V numbered myfile.patch

Validate Patch Files

If you want to verify or observe the outcome of patching, then use “–dry-run” option. It does not make any modification to the original file:

$ patch –dry-run myfile.patch

Reverse/Undo a Patch

The “-R” option is used to reverse or undo a patch that has already been applied.

$ patch file.patch

$ ls –l myfile.c

$ patch –R myfile.patch

$ ls –l myfile.c

Conclusion:

In Linux operating system, “patch” is a command that allows us to apply patch files to the source codes or configuration files. The patch file is used for software updating purposes. The difference between the original and new files is held in patch files and the “diff” command is used to get the difference or patch. We discussed the usage of the “diff” and the “patch” commands with a number of options such as making backups, dry-running, and reversing the applied patch.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More