inside the mind of a linux admin

Linux Protip: How to make a patch file

I’ve been doing Linux system administration for well over ten years, and I’ve used patch files often. I’ve never actually had the need to create one until today. To my surprise, I discovered how blatantly simple and easy it is. I’ve always assumed it was some sort of black magic involving unicorns and rainbows. Sure, there are more complex ways to do this but for most needs this will work for you.

Whats a patch?

A patch is the best and easiest way to submit changes back to an open source project. It’s a summary of changes you made to file or files formatted in a way that can easily be used by the excellent program, named, unsurprisingly, ‘patch’. Now because patch was written by the inestimable Larry Wall, patches can come in a wide range of shapes, sizes, and formats, and they will apply with a high degree of “do what I mean”-ness. However, there are some tips to produce high quality patches.

Creating a patch file

diff -Naur old new > patch_file

Yeah, seriously. That’s it.

Now you’ve got a working .patch file that you can use to apply the changes between ‘old’ and ‘new’ anytime, on any machine, anywhere. It automatically determines the difference and the lines that changed. After it builds the patch file, it must be applied to identical code it was built from.

This is very useful when you need to change working code uniformly.

Applying a patch file

The following usage is most commonly used method to apply a patch:

$ patch -p1 < {/path/to/patch/file}

To apply a patch, one could run the following command in a shell:
$ patch < /path/to/file

Patches can be undone, or reversed, with the '-R' option:
$ patch -R < /path/to/file

Above 3 are basic usage read the man page of patch command for more information and usage:
% man patch


Related Posts

Touchpad stops working after sleep + resume (Fedora 26 on Dell XPS)

After recently upgrading my Dell XPS 13 w/Touchscreen to Fedora 26, the touchpad suddenly stopped working upon resuming from sleep mode. It was reproduced 100% of the time, and required a complete restart in order to get the touchpad working again. There have been several forum posts and bug reports regarding others experiencing these exact […]

Read More

Fun with fsck on Fedora – avoiding mounted partitions and handling LUKS encrypted ones

I run Fedora on a few of my machines, and inevitably over the course of time the filesystem will need to be repaired because of orphaned inodes, wrong free byte counts, etc. Typically when EXT4 errors are detected during boot, Fedora will detect this and offer to drop you into “Emergency mode” or continue. example: […]

Read More

1 Comment

  • ein on Sunday, October 14, 2012


Leave a Reply

Your email address will not be published. Required fields are marked *

Twitter: kireguy

Tweeter button Facebook button Myspace button