Open-Source Internship opportunity by OpenGenus for programmers. Apply now.
The patch command reads a patch file and modifies the original file/directory as described by the instructions in the patch file which were obtained by the diff command. In this article we demonstrate how to use the patch and diff commands to update source files/directories.
Table of contents.
- Introduction.
- Patching files.
- Patching directories.
- Summary.
- References.
Introduction.
A patch file is a file that will contain differences between two files that were acquired using the diff command and modifies the original file to match the updated file.
The patch command reads the patch file and modifies the original file as described in the patch file.
Patching files.
The process of patching is as follows, we have to sets of code, an original and a modified version. Assuming the original has been approved then all changes to be made must follow a well-defined process.
We place these changes in a patch file and using the diff command in Linux we get the differences between the two files and place them in a patch file then using the patch command we modify the original code with the patch file.
An example
Execute the command to create a file v1.c,
$ cat > v1.c
Paste the following, and Ctrl+C.
#include<stdio.h>
int main(){
printf("Version 1 \n");
}
Execute the command to create another file v2.c
$ cat > v2.c
Paste the following, and Ctrl+C.
#include<stdio.h>
int main(){
printf("Version 2 \n");
}
Create a patch file using the diff command.
diff -u v1.c v2.c > file.patch
The command will list differences between the two files, we use the -u(unified mode) option with diff to avoid of redundant lines.
Lines obtained are referred to as context lines and they are used by the patch command to locate areas where changes should be made in the original file.
Now we patch the original file v1.c with changes from the patch file test.patch
$ patch < file.patch
$ cat v1.c
#include<stdio.h>
int main(){
printf("Version 2 \n");
}
We could also patch as follows,
patch -u v1.c -i file.patch
Where -i specifies the input patch file.
While patching we may want to preserve the original file contents, to do so we use the -b option as follows,
patch -u -b v1.c -i file.patch
After this operation, we should see a file v1.c.orig which holds the original contents of v1.c file.
To revert back to the original version, we use the -R option,
$ patch -R < file.patch
$ cat v1.c
#include<stdio.h>
int main(){
printf("Version 1 \n");
}
Patching directories.
diff -urN DIR1/ DIR2/ > dir.patch
The -r option is for recursively looking into a directories subdirectories and files.
-N option handles files in the newer directory that are not in the working directory, diff places instructions in the patch file such that the patch command when reading the patch file will create files present in the newer directory that are missing in the current directory.
While patching large directories we may want to dry run the process to avoid mistakes, in this case we can write,
patch -urN --dry-run -d DIR1/ < dir.patch
-d option tells patch command which directory we are working on.
Since in this case we are working from DIR1 we use the < to redirect patch file into the patch command.
After this step if we are ok with the patch we can perform an actual patch as follows,
patch -urN -d DIR1 < dir.patch
Summary.
Today version control systems such as git perform this task effectively in terms of patching programs, however one can still find use cases for patch and diff commands that git cannot be able to perform. For example if we have multiple html pages of a similar structure but different content and need to make a change to all of them simultaneously for example changing the header section, patch and diff will work like a charm.
References.
- diff command
- piping and redirection in Linux
- Execute man diff or man patch for the commands' manual page.
- Execute diff --help or patch --help for help.