How to patch a file (and create patches) en Linux

Contents

PC displaying a shell prompt on a Linux desktop

El Linux patch The command enables you to transfer changes from one set of files to another set of files quickly and safely. Learn how to use patch the simple way.

The patch and diff commands

Imagine you have a text file on your computer. Receive a modified version of that text file from someone else. How do you quickly transfer all changes from the modified file to your original file? That's where patch and diff come to play. patch and diff found on Linux and other Unix-like operating systems, like macOS.

the diff command examine two different versions of a file and list the differences between them. Differences can be stored in a file called patch file.

the patch The command can read a patch file and use the content as a set of instructions. Following those instructions, changes to the modified file are replicated in the original proceedings.

Now imagine that this procedure occurs on an entire directory of text files. All at once. That is the power of patch.

Sometimes they don't send you modified files. All that is sent to you is the patch file. Why submit dozens of files when you can submit a file or post a file for easy download?

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

the patch The command is most often used by people who work with software source code files., but it works equally well with any set of text files, whatever your goal, source code or not.

RELATED: How to Compare Two Text Files in Linux Terminal

Our example scenario

In this stage, we are in a directory called work that contains two other directories. One is called labor, and the other is called more recent. The working directory contains a set of source code files. The latest directory contains the latest version of those source code files, some of which have been modified.

To be sure, the working directory is a copy of the current version of the text files. It is not the only copy of them.

Find the differences between two versions of a file

the diff command finds differences between two files. Its default action is to list the modified lines in the terminal window.

A file is called slang.c. We will compare the version of the working directory with that of the most recent directory.

the -u the option (unified) dice diff to further list some of the unmodified lines of text before and after each of the modified sections. These lines are called context lines. They help the patch precisely locate where a change should be made in the original file.

We provide the file names so that diff know which files to compare. Original file is listed first, after the modified file. This is the command we issue to diff:

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

diff produces an output list showing the differences between the files. If the files were identical, no results would be included in the list. Seeing this kind of output from diff confirms that there are differences between the two file versions and that the original file needs a patch.

Make a patch file

To capture those differences in a patch file, use the following command. It is the same command as the previous one, with the departure of diff redirected to a file called slang.patch.

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

The name of the patch file is arbitrary. You can call it whatever you want. Darle una extensión “.patch” That's a good idea; despite this, make it clear what type of file it is.

To make patch act on the patch file and modify the working file / slang.c, use the following command. the -u option (unified) enables patch know that the patch file contains unified context lines. In other words, we use the -u option with diff, so we use the -u option with patch.

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

If everything goes fine, there is only one starting line that tells you patch is patching the file.

Make a backup of the original file

We can instruct patch to make a backup of the patched files before they are modified via the -b option (backup). the -i The option (entry) tells the patch the name of the patch file to use:

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

The file is patched as before, no visible differences in the output. Despite this, if you look at the work folder, you will see that the file named slang.c.orig has been created. The file date and time stamps show 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 files in two directories. Then we can use that patch file with patch so that those differences are applied to the files in the working folder with a single command.

The alternatives we will use with diff are the -u (unified context) that we have previously used, the -r (recursive) option to do diff search in any subdirectory and the -N (new file) option.

the -N option says diff how to handle files in newer directory that are not in working directory. Strength diff put instructions in the patch file so thatpatch creates files that are present in the latest directory but are missing from the working directory.

You can group the alternatives to use a single script (-).

Please note that we only provide the directory names, we are not saying diff to look at specific files:

diff -ruN working/ latest/ > slang.patch

slang.patch” width=”646″ height=”77″ onload=”pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);” onerror=this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);”>

Echando un vistazo dentro del archivo de parche

Echemos un vistazo rápido al archivo de parche. we will use less para mirar su contenido.

La parte de arriba del archivo muestra las diferencias entre las dos versiones de slang.c.

Desplazándonos hacia abajo por el archivo de parche, vemos que después describe los cambios en otro archivo llamado structs.h. This verifies that the patch file definitely contains the differences between the different versions of various files.

Look before you jump

Patching a large collection of files can be a bit disconcerting, therefore we are going to use the --dry-run option to check that everything is fine before taking the step and commit to making the changes.

the --dry-run option says patch to do everything, apart from modifying the files. patch perform all your pre-flight checks on files and, if you find any problem, will inform. Anyway, no file is modified.

If no issues are reported, we can repeat the command without the --dry-run option and patch our files with confidence.

the -d (directory) option say patch which directory to work in.

Please note that we are no using the -i (entry) option to count patch which patch file contains the instructions for diff. Instead, we are redirecting the patch file to patch with <.

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

From the entire directory, diff I found two files to patch. Instructions regarding modifications for those two files have been verified by patch and no problems have been reported.

Pre-flight checks are fine; we are ready to take off.

Patch a directory

To genuinely apply the patches to the files we use the above command without the --dry-run option.

patch -ruN -d working < slang.patch

This time, cada línea de salida no comienza concomprobación”, cada línea comienza conparcheo”.

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

Resolve your differences

This is, with much, the easiest and safest way to use patch. Copy your destination files to a folder and patch that folder. Copy them again when you are satisfied that the patching procedure completed without errors.

setTimeout(function(){
!function(f,b,e,v,n,t,s)
{if(f.fbq)return;n=f.fbq=function(){n.callMethod?
n.callMethod.apply(n,arguments):n.queue.push(arguments)};
if(!f._fbq)f._fbq = n;n.push=n;n.loaded=!0;n.version=’2.0′;
n.queue=[];t=b.createElement(e);t.async=!0;
t.src=v;s=b.getElementsByTagName(e)[0];
s.parentNode.insertBefore(t,s) } (window, document,’script’,
‘https://connect.facebook.net/en_US/fbevents.js’);
fbq(‘init’, ‘335401813750447’);
fbq(‘track’, ‘PageView’);
},3000);

Subscribe to our Newsletter

We will not send you SPAM mail. We hate it as much as you.