قالب وردپرس درنا توس
Home / Tips and Tricks / Apply a patch to a file (and create patches) on Linux

Apply a patch to a file (and create patches) on Linux



  PC with a Shell Command Prompt on a Linux Desktop
Fatmawati Achmad Zaenuri / Shutterstock.com

The Linux Patch command allows you to make changes from one filegroup to another filegroup transmit quickly and safely. Learn how to use Patch in a simple way.

The Patch and Diff Commands

Imagine that you have a text file on your computer. You will receive a modified version of this text file from another person. How do you quickly transfer any changes from the modified file to your original file? Here comes Patch and Diff into play. Patch and diff are included in Linux and other Unix-like operating systems, such as macOS.

The command diff 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.

Use the command patch to read a patch file and use the contents as a command set. If you follow these instructions, the changes in the modified file will be replicated to the original file.

Now imagine that this process runs in an entire directory of text files. Everything in one go. That's the strength of Patch .

Sometimes the changed files are not sent. You only receive the patch file. Why send dozens of files when you can send a file or provide a file for easy downloading?

What do you do with the patch file to actually patch your files? Apart from being almost a tongue twister, this is also a good question. We will guide you through the command in this article.

The Patch command is most commonly used by people who work with software source code files. However, it works for all types of text files, regardless of their purpose, source code or not.

RELATED: Comparing Two Text Files in the Linux Terminal

Our Example Scenario

In this scenario, we are in a directory called work that contains two different directories. One is called work, and the other is called latest . The working directory contains a number of source code files. The latest directory contains the latest version of these source code files, some of which have been changed.

For security reasons, the working directory is a copy of the current version of the text files. This is not the only copy.

Determining differences between two file versions

The diff command detects the differences between two file versions. By default, the changed rows are listed in the terminal window.

One file is called slang.c . We will compare the version in the working directory with the one in the latest directory.

The option -u (unified) directs diff to list some of the unified directories as well. Changed text lines before and after each of the changed sections. These lines are called context lines. They help the patch command to determine exactly where a change in the original file must be made.

We specify the names of the files so that diff knows which files to compare. The original file is listed first, then the modified file. This is the command we send to diff :

  diff -u working / slang.c latest / slang.c 

  diff -u working / slang.s latest / slang.c in a terminal window

diff creates an output list with the differences between the files. If the files were identical, no output would be listed at all. If you see this way of issuing diff it is confirmed that there are differences between the two file versions and that the original file needs to be patched.

 Diff output in a terminal window

Create a patch file

Use the following command to capture these differences in a patch file. It's the same command as above, with the output of diff being redirected to a file called slang.patch.

  diff -u working / slang.c latest / slang.c> slang.patch 

  diff -u working / slang.c latest / slang.c> slang.patch in a terminal window

The Name of the patch file is arbitrary. You can name it as you like. It's a good idea to give him a ".patch" extension. However, it becomes clear which type of file it is.

Use the following command to apply patch to the patch file and modify the working / slang.c file. The option -u (unified) indicates patch indicates that the patch file contains Unified Context Lines. In other words, we used the -u option with diff, so we use the option -u with patch .

Patch -u working.slang.c - i slang.patch

 Patch -u working.slang.c -i slang.patch in a terminal window

If all goes well, there is a single line of output that tells you Patch patches the file.

Backing Up the Original File

We can tell Patch to back up the patched files before they are modified. -b (backup) option. The option -i (input) tells Patch the name of the patch file to use:

   patch -u -b working.slang.c -i slang.patch 

  patch - u working.slang.c -i slang.patch in a terminal window

The file is patched as before, with no visible difference in the output. However, if you look in the working folder, you will see that the file slang.c.orig has been created. The date and time stamps of the files show that slang.c.orig is the original file and slang.c is a new file created with patch .

 The output of ls in a terminal window

Using diff with directories

With diff you can create a patch file containing all the differences between the files in contains two directories. We can then use this patch file with patch to apply these differences to the files in the working folder with a single command.

The options we will use with diff are the option -u (unified context), which we used previously, the option -r (recursively) to see diff in all subdirectories and option -N (new file).

The option -N tells diff how to handle files in the latest directory that are not in work directory. It forces diff to insert statements into the patch file so that patch creates files that exist in the latest directory but are missing from the working directory.

You can group the options to use a single hyphen ( - ).

Note that we specify only the directory names, but not diff to view specific files: [19659022] diff -ruN working / latest /> slang.patch

  diff -ruN working / latest /> slang.patch "width =" 646 "height =" 77 "src =" /pagespeed_static/1.JiBnMqyl6S.gif "onload =" pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon (this); "onerror =" this.onerror = null ; pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon (this); "We will use <code> less </code> to view the content. </p>
<p>  <img class=

The upper part of the File shows the differences between the two versions of slang.c.

 Less slang.patch shows the top part of the file in a terminal window

Scroll down through the patch file that it then the Describes changes in another file called structs.h This ensures that the patch file definitely contains the differences between different versions of multiple files.

 less slang.patch displays the middle of the file in a terminal window

Look Before You Leap

Patching a large collection of files can be a bit annoying, so we will use the option - trial run to see if all it's alright before we take the plunge and commit to make the changes.

The - Trial option instructs Patch to do everything but change the files. patch checks all files before the flight and reports them if problems occur. In both cases, no files are changed.

If no issues are reported, we can retry the command without the option - Trial Run and patch our files reliably.

The - Option d (directory) tells the patch which directory to work on.

Note that we do not use option -i option, patch tell which patch file the instructions of Diff contains. Instead, we redirect the patch file to patch with <.

  patch -dry-run -ruN -d working <slang.patch 

<img class = "Alignnone Size-Full wp-Image-421261" data-pagespeed-lazy-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 working < slang.patch in a terminal window" width="646" height="122" src="/pagespeed_static/1.JiBnMqyl6S.gif" onload="pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);" onerror="this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);"/>

Out of the whole directory diff has found two files for patching – the instructions for the changes for these two files were reviewed by patch and no issues were reported.

Before the flight checks are fine, we're ready to start.

Patch a directory

To really apply the patches to the files , we use the previous command without the option - Trial run .

  patch -ruN -d working <slang.patch 

<img class = "Do not resize – Full wp image – 421274" data-pagespe ed-lazy-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 working < slang.patch in a terminal window" width="646" height="122" src="/pagespeed_static/1.JiBnMqyl6S.gif" onload="pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);" onerror="this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);"/>

This time, each output line does not start with "check", each line begins with "patching".

And no problems are reported. We can compile our source code and have the latest version of the software.

Clean Up Your Differences

This is by far the easiest and safest method of using Patch . Copy your target files to a folder and patch this folder. If you are satisfied that the patch has been completed successfully, please copy it back.




Source link