How to Apply a Patch to a File (and Create Patches) in Linux

Share

Fatmawati Achmad Zaenuri/Shutterstock.com

The Linux patch command lets you transfer the 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. You receive a modified version of that text file from someone else. How do you quickly transfer all of the changes from the modified file to your original file? That’s where patch and diff come into play. patch and diff are found in Linux and other 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 contents as a set of instructions. By following those instructions, the changes in the modified file are replicated in the original file.

Now imagine that process happening to an entire directory of text files. All in one go. That’s the power of patch.

Sometimes you don’t get sent the modified files. All you get sent is the patch file. Why send dozens of files round when you can send one file, or post one file for easy download?

What do you do with the patch file to actually patch your files? Apart from almost being a tongue-twister, that’s also a good question. We’ll walk you through it 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 whatever their purpose, source code or not.

RELATED: How to Compare Two Text Files in the Linux Terminal

Our Example Scenario

Read the remaining 47 paragraphs

Source : How to Apply a Patch to a File (and Create Patches) in Linux