Create a Patch From a Source Tree
The previous example was very simple, involving only one file. However, when working with larger projects, you may need to create and apply patches for an entire source code directory or “source tree.”
Let’s use the OpenVPN source code as an example. Imagine you have downloaded two different versions: openvpn-2.3.2 and openvpn-2.3.4.
First, extract both versions :
tar -xvzf openvpn-2.6.9.tar.gz
tar -xvzf openvpn-2.3.10.tar.gz
Now, to create a patch file containing the differences between these two versions, use the following command.
Command :
diff -Naur openvpn-2.6.9 openvpn-2.6.10 > openvpn.patch
Output :
This command will compare the two source code directories recursively and save all the differences in a file called “openvpn.patch.” The diff command will look at all the files and directories inside openvpn-2.3.2 and openvpn-2.3.4, find the changes, and put those changes into the openvpn.patch file.
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