How to Apply a Patch to a File Using Linux

Use 'diff' and 'patch' to update a file

Patch of denim on denim jeans

 

Daniela White Images / Getty Images

A patch is a segment of computer code that inserts or modifies an existing codeset. Patches are commonly used during the programming process to substitute changes to source code. Linux includes a patch utility to streamline this process.

The patching process is generally used in formal change-control systems, where professional best practices for code quality and testing are in effect. Patching is less commonly used in hobby projects where the source code changes at the programmer's discretion.

The Patching Process

In a typical use case, you've got an original version of your source code, then a modified version of it. The original has been approved formally and any changes to it must follow a well-defined process. So those changes occur in a patch file.

Using a tool called diff, reduce the differences between the two file versions to a single file that lists the specific changes that the second makes to the first.

Then, the resulting difference file modifies the original code through use of the patch tool.

Applying Patches: An Example

Consider a typical "Hello World" program. The source code in C looks like this:

#include <stdio.h>
int main()
{
printf("Hello world!");
}

To change the printed message to Hello universe!, develop an alternative version of the code, as follows:

#include <stdio.h>
int main()
{
printf("Hello universe!");
}

Generate a patch file by using the diff utility:

diff -u helloworld.c helloworld2.c > file.patch

You'll then see that the patch file looks like this:

patched file in linux

The patch file lists each compared file. It offers symbols, --- and +++, identifying each filename with its last-revised date. Then it summarizes the location of the changes between a pair of @@ symbols. Finally, it lists the source code, marking the different lines using the noted plus-and-minus symbols. Generally, the minus sign indicates that the line will be removed in the patched file and the plus sign represents a replacement, if the line will be replaced.

To apply the patch file to the original file, execute:

patch < file.patch

At that point, the first file changes to match the second.

To undo the patch, use the -R option:

patch -R < file.patch

Considerations

This approach to patching source code is less common than it used to be. Modern source-control systems like Git and Subversion automatically manage versions over time, including along various development and testing branches, so that manual difference checking and file patching is now managed by the version-control tool. If you must follow a formal coding structure or conform to team-coding standards, you'll use a tool like Git instead.

However, one effective use case for the diff-and-patch system on a local machine relates to changes to an entire directory of files. For example, if you've developed a website but merely need to make a change to a single line—like a copyright line—then this system will update all of those pages simultaneously.

Consult the manpages for patch for additional context.