how to apply a patch to a file in linux
Patch files are usually text files that are used to easily transfer changes or modifications to files, usually source code files. These patch files or patch as it is also called, contain only the differences between the original and updated files and is much smaller in size than the original file. The file differences are stored in a pre-determined format such that these differences can easily be merged into the requisite files to produce the final updated file.
The patches are the easy and most effective way of pushing out quick updates, such as security fixes. Updating the target file using these patch files are termed as patching or applying the patch.
Patches are usually associated with source codes and software programming, but can technically be applied to any file. And even though most patches refer to text patches, it is quite possible to have binary patch files that can be applied to binary files as well. Patches can include additions, deletions and modifications of the file contents.
When to Apply a Patch
Patches can be quite handy when you want to update files without having to download the entire updated file. In addition to the savings in download time, this can also aid in quickly figuring out the exact piece of content that has changed without having to sift through entire file content.
There are several different scenarios when you want to apply a patch. If you manually compile your own software and programs, as in Gentoo distro then you might want to patch files to update the versions or to apply security fixes. Various source control system uses diff and patch routinely to update files quickly. Another use of it is in gaming software, where it allows the configuration and cheat files to be updated without a complete download and re-install.
How to Apply a Patch
The command or utility in Linux that is used to apply the patch is called patch. The patch program takes a patch file and applies the differences into one or more of the original file producing a final patched version of the file. Patch files usually have an extension of .patch although it is not necessary. You can use any file extension as long as the file contents are in the proper diff format that patch understands.
The simplest form of patch command is shown below. Change your current working directory to the directory where the file exists and then apply the patch as shown below
bash$ patch < patchfile.patch
If for some reason, the file name differs from what is in the patch file, then specify the file that needs to be patched.
bash$ patch mynewfile.txt < patchfile.patch
If you are applying patches to multiple files in the directory, ie. you are patching the entire directory then you have set the p level correctly so that the files can be identified correctly. The p level merely refers to how much of the specified file path in the patch file needs to be ignored.
Usually the absolute file path to the directory that needs to be patched will defer from the file path on the machine where the patch file was generated. This means that you will need to execute the patch command from the appropriate directory using the correct p level.
When in doubt check the patch file to see what the path to the file is. Assume that the patch file specifies the following path to the file …
/opt/utility/web/src/common/util.c
and your files exist at /home/tom/sources/utility/web/src/common/util.c. The p level is calculated from the current directory and the path in the patch file. Change your current working directory as below.
bash$ cd /home/tom/sources/
Now, the file is at utility/web/src/common/util.c. You will need now to ignore opt in the path, which gives a p level of 2. Count the number of path separators in the path that needs to ignored. That is, /opt /contains two path separators.
bash$ patch -p2 < patchfile.patch
Just in case, you decide to be in home/tom/sources/utility/web/src folder to execute the patch. You will need to ignore opt/utility/web/src from the original path, which gives us a p level of 5.
bash$ patch -p5 < /path/to/patchfile.patch
How to Reverse Patches
Once in a while, you might want to reverse the patch after you have applied it, especially if you applied the wrong patch. The patch command provides a command line option –reverse or -R to do that. Again, you can compute the p level as detailed above.
bash$ patch -p5 -R < /path/to/patchfile.patch
Patch arguments
The patch command comes with a whole lot of arguments that could be helpful. But unless you are a programmer who needs to patch files on a regular basis you need not be concerned about them. However, some of the other useful arguments are:
-b or –backup : This option will backup the original file instead of removing them. By default, the backup files are created only if there is some issue and/or the files do not match correctly.
–dry-run: Use this option, usually along with –verbose, if you like to see what exactly will be done by the patch. This will not update the file or actually apply a patch. This is useful when you are not sure about the patch or the p level.
-N or –forward: This will ignore patches that have already been applied or has been reversed.
It is always good practice to verify visually that the patch has been applied correctly by opening the modified file. You can compare the contents in the patch file with the updated content in the file. This might be possible where the patch is applied to only a few number of files. If a lot of files have been updated because of a patch, you can still verify a handful of them choosing them randomly.