Create a Patch File using diff
Let’s start with a small C program called hello.c:
#include <stdio.h>
int main(int argc, char *argv[]) {
printf("Hello World\n");
}
First, make a copy of hello.c called hello_new.c using the following command.
Command :
cp hello.c hello_new.c
Output :
Next, make some changes to hello_new.c:
#include <stdio.h>
int main(int argc, char *argv[]) {
printf("Hello World\n");
return 0;
}
Now, use the diff command to create a patch file called hello.patch:
Command :
diff -u hello.c hello_new.c > hello.patch
Output :
The patch file hello.patch will contain the differences between the original hello.c and the modified hello_new.c.
How to Run Patch Command in Linux?
Sometimes, you install software by compiling it from source code instead of using package managers like yum or apt-get. When a security fix is available for such software, you can’t just upgrade it like you normally would. Instead, you must download the security patch, apply it to the source code, and then recompile the software.
This article explains how to create and apply the diff and patch commands. A patch file contains the differences between two versions of the same file or source code. It is made using the diff command and applied using the patch command.
Run Patch Command in Linux
- Syntax of running patch command in Linux
- Application of the Patch File
- Options and descriptions for patch command
- Create a Patch File using diff
- Apply Patch File using Patch Command
- Create a Patch From a Source Tree
- Apply Patch File to a Source Code Tree
- Take a Backup before Applying the Patch using -b
- Validate the Patch without Applying (Dry-run Patch File)
- How to Undo/Reverse a Patch