The Linux Patch Command allows you to transfer changes from one set of files to another set of files quickly and securely. Learn how to use the patch in a simple way.
Patch and diff commands
Imagine that you have a text file on your computer. You receive a modified version of this text file from someone else. How to quickly transfer all changes from the modified file to your original file? This is where patch and diff come into play. Patch and diff are available on Linux and others Unix-like operating systems, such as macOS.
The diff command examines two different versions of a file and lists the differences between them. The differences can be stored in a file called a patch file.
The patch command can read a patch file and use the content as a set of instructions. By following these instructions, the changes in the modified file are replicated in the original file.
Now imagine that this process occurs in a complete directory of text files. All in one. This is the power of the patch.
Sometimes modified files are not sent to you. All you receive is the patch file. Why send dozens of files when you can send a file or post a file for easy download?
What do you do with the patch file to actually fix your files? In addition to being almost a fool, it's also a good question. We will explain this in this article.
The patch command is most often used by people working with software source code files, but it works equally well with any set of text files, regardless of their purpose, source code or not.
Our example scenario
In this scenario, we are in a directory called work that contains two other directories. One calls it to work, the other one calls last. The working directory contains a set of source code files. The latest directory contains the most recent version of these source code files, some of which have been modified.
For more security, the working directory is a copy of the current version of the text files. This is not the only copy.
Find the differences between two versions of a file
The diff command finds the differences between two files. Its default action is to list the modified rows in the terminal window.
A file is called slang.c. We will compare the version of the working directory to that of the last directory.
The -u (unified) option tells diff to also list some of the unmodified text lines preceding and following each of the modified sections. These lines are called context lines. They help the patch command locate exactly where a change needs to be made in the original file.
We provide the file names so that diff knows which files to compare. The original file is listed first, followed by the modified file. That's the command we give to diff:
diff -u work / slang.c last / slang.c
diff produces an output list showing the differences between the files. If the files were identical, no output would be listed. Seeing this type of output from diff confirms that there are differences between the two versions of the file and that the original file needs to be corrected.
Make a FILE patch
To capture these differences in a patch file, use the following command. This is the same command as above, with the result of redir redirected to a file called slang.patch.
diff -u work / slang.c last / slang.c> slang.patch
The name of the patch file is arbitrary. You can call it what you want. Giving a ".patch" extension is a good idea. however, this clearly indicates the type of file.
For patch to respond to the patch file and modify the working / slang.c file, use the following command. The -u (unified) option tells the fix that the patch file contains unified context lines. In other words, we used the -u option with diff, so we used the -u option with patch.
patch -u working.slang.c -i slang.patch
If all goes well, a single output line tells you that patch corrects the file.
Make a backup of the original file
We can ask patch to make a backup copy of the corrected files before they are modified using the -b (backup) option. The -i (input) option tells patch the name of the patch file to use:
patch -u -b working.slang.c -i slang.patch
The file is corrected as before, with no visible difference in the output. However, if you look in the working folder, you will see that the file called slang.c.orig has been created. The timestamps of the files indicate that slang.c.orig is the original file and slang.c is a new file created by patch.
Using diff with directories
We can use diff to create a patch file containing all the differences between the files in two directories. We can then use this hotfix patch file to apply these differences to the working folder files with a single command.
The options we will use with diff are the -u (unified context) option we used earlier, the -r (recursive) option that allows diff to look at all subdirectories and the -N option ( new file).
The -N option tells diff how to handle the files in the last directory that are not in the working directory. It forces diff to put instructions in the patch file so that patch creates the files in the last directory but not in the working directory.
You can group the options so that they use a single hyphen (-).
Note that we only provide directory names. We do not say diff to look at specific files:
diff -ruN working / latest /> slang.patch
<img class="alignnone size-full wp-image-421203" src="http://howtodoeasily.com/wp-content/uploads/2019/06/1560067506_955_How-to-Apply-a-Patch-to-a-File-and-Create-Patches-in-Linux.png" alt="diff -ruN working / latest /> slang.patch "width =" 646 "height =" 77 "/>
Look inside the patch file
Let's look quickly at the patch file. We will use less to look at its content.
The top of the file shows the differences between the two versions of slang.c.
Going down further in the patch file, we see that it then describes the changes in another file called structs.h. This verifies that the patch file contains the differences between different versions of multiple files.
Think before acting
Correcting a large collection of files can be a little annoying, so we will use the –dry-run option to check that everything is fine before we start and commit ourselves to making the changes.
The –dry-run option tells patch to do everything except modify the files. patch will perform all its preflight checks on files and if it encounters any problems, it reports them. In any case, no file is modified.
If no problem is reported, we can repeat the command without the -dry-run option and apply confidential patches to our files.
The -d (directory) option tells the hotfix which directory to use.
Note that we do not use the -i (input) option to tell the hotfix which hotfix file contains the diff instructions. Instead, we redirect the hotfix file into hotfix with <.
patch –dry-run -ruN -d work <slang.patch
<img class = "full-size alignnone wp-image-421261" src = "https://www.howtogeek.com/wp-content/uploads/2019/05/xpatch_14.png.pagespeed.gp+jp+jw+pj + ws + js + rj + rp + rw + ri + cp + md.ic.EN3xXcFRY6.png "alt =" patch –dry-run -ruN -d works
On the entire directory, diff found two files to fix. Instructions for changes to these two files have been patched and no issues have been reported.
Pre-flight checks are acceptable. we are ready to take off.
Patch a directory
To really apply the patches to the files, we use the previous command without the –dry-run option.
patch -ruN -d work <slang.patch
<img class = "full-size alignnone wp-image-421274" src = "https://www.howtogeek.com/wp-content/uploads/2019/05/xpatch_16.png.pagespeed.gp+jp+jw+pj + ws + js + rj + rp + rw + ri + cp + md.ic.PrA8BQ3riq.png "alt =" patch -ruN -d works
This time, each line of exit does not start with "verification", it starts with "correction".
And no problem is reported. We can compile our source code and we will use the latest version of the software.
Set your differences
This is by far the easiest and safest way to use patch. Copy your target files to a folder and correct this folder. Copy them again when you are satisfied with the end of the error-free correction process.