How to create and use the patch command

This very simple HOWTO will show how to use the *nix patch command

Create a patch from two files

diff -u myfile mynewfile > myfile.patch

This will give you a patch (myfile.patch) that, when applied to myfile, will make it look exactly like mynewfile. I use the -u option because I like the so called unified diff format

Apply a patch to one file

patch myfile myfile.patch

This applies the myfile.patch on the file myfile (which makes it look exactly like mynewfile from above)

Create a patch from two directories

diff -u -r dir1/ dir2/ > myfiles.patch

Creating a patch file for multiple files in different directories requires the -r option which stands för recursive i.e. diff will search through all subdirectories

Apply a patch to a directory

 patch -p0 -i myfiles.patch 

When patching multiple files in different directories we need to tell the patch command to not strip paths from patch file (which is the default behavior). This is done through the -p0 option. We also need to specify the input file with the -i option <patchfile>
NOTE! when applying a patch you should always stand one level above the topmost directory of the structure to be patched

Leave a Comment


NOTE - You can use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre lang="" line="" escaped="" cssfile="">

This site uses Akismet to reduce spam. Learn how your comment data is processed.