HowTo Apply a Patch File To My Linux / UNIX Source Code

by on April 24, 2007 · 8 comments· LAST UPDATED April 29, 2014

in , ,

I am a new Linux and Unix system user. I also know that I can patch binary package using up2date or yum command in Linux. I was wondering is if there's a way to apply a patch file to downloaded source code on a Linux / UNIX like operating system source tree?

Linux and UNIX source software often comes with security and other patches. You can download them from Internet or project home page. There is a command called patch that apply a diff file or patch to an original source tree.
Tutorial details
DifficultyEasy (rss)
Root privilegesNo
Requirementspatch and diff commands
Estimated completion time5m

The patch command takes a patch file patchfile containing a difference listing produced by the diff program and applies those differences to one or more original files, producing patched versions. Normally the patched versions are put in place of the originals.

patch command syntax

The basic syntax is as follows:
$ patch < patch.file
$ patch source.code.file < patch.file
$ patch -p LEVEL < {/path/to/patch/file}

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

In this example, patch foo.c with patch.diff file:
$ patch foo.c < patch.diff

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

How do I create a patch?

To create a patch, one could run the following diff command:
$ diff -u oldfile-name-here newfile-name-here > patch.diff

Example: Creating and applying the patch for hello.c sample program on a Linux or Unix like system

Create a hello.c as follows:

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

Make a copy of the hello.c (or file you wish to modify); both files must be in the same directory, though it may be any directory using cp command:
$ cp hello.c hello-new.c
Edit the file hello-new.c to make it as you want it. In this example, I am fixing a few compiler warning by adding return value from main():

#include<stdio.h>
 
int main(void){
	printf("Hello, world!\n");
	return 0;
}

Next, use command diff to create a unified diff patch file called hello.patch:
$ diff -u hello.c hello-new.c > hello.patch
To see patch use cat command as follows:
$ cat hello.patch
Sample outputs:

--- hello.c	2014-04-29 17:59:49.000000000 +0530
+++ hello-new.c	2014-04-29 18:00:43.000000000 +0530
@@ -1,5 +1,6 @@
 #include<stdio.h>
 
-main(){
+int main(void){
 	printf("Hello, world!\n");
+	return 0;
 }

To apply the patch from hello.patch, enter:

### The hello.patch patchfile knows the name of the file to be patched ##
patch < hello.patch
 

Sample outputs:

patching file hello.c

Finally, here is my updated and patched hello.c:
$ cat hello.c
Sample outputs:

#include<stdio.h>
 
int main(void){
	printf("Hello, world!\n");
	return 0;
}

You can now compile program:
$ cc hello.c -o hello
Run it as follows:
$ ./hello
Sample outputs:

Hello, world!

A note about working on an entire source tree

First, make a copy of the source tree:
## Original source code is in lighttpd-1.4.35/ directory ##
$ cp -R lighttpd-1.4.35/ lighttpd-1.4.35-new/

Cd to lighttpd-1.4.35-new directory and make changes as per your requirements:
$ cd lighttpd-1.4.35-new/
$ vi geoip-mod.c
$ vi Makefile

Finally, create a patch with the following command:
$ cd ..
$ diff -rupN lighttpd-1.4.35/ lighttpd-1.4.35-new/ > my.patch

You can use my.patch file to patch lighttpd-1.4.35 source code on a different computer/server using patch command as discussed above:
patch -p1 < my.patch
See the man page of patch and other command for more information and usage - patch(1), diff(1), bash(1)

TwitterFacebookGoogle+PDF versionFound an error/typo on this page? Help us!

{ 8 comments… read them below or add one }

1 naresh March 29, 2008 at 11:19 am

Linux and UNIX source software often comes with security and other patches. You can download them from Internet or project home page. There is a command called patch that apply a diff file or patch to an original source tree.

patch takes a patch file patchfile containing a difference listing produced by the diff program and applies those differences to one or more original files, producing patched versions.

Reply

2 naresh March 29, 2008 at 11:20 am

guess what I need is a link to the .patch file syntax so I know what it means and can apply the changes manually (unless there’s some other way to do it).

Reply

3 Manish May 22, 2011 at 5:08 am

Hi
I want to merge two patch file into one then how I can get that done without any complications and breaking the source.If I apply both separately then it fails.

Reply

4 Tony Browning July 16, 2011 at 1:24 pm

I have yet to see Wine Internet Explorer , wine-1.3.6 I can click on the white loading bar while new Adobe FlashPlayer 11beta for Internet Explorer (only) is trying to load it in my terminal thru (Wine Windows Program Loader) and Wine Internet Explorer will power-up, but its missing a few stuff. Adobe FP 11b not right one for it (because the accept license agreement tab not there) and wish someone could find right FP, maybe you or someday adobe will produce one? Check-it out and you will see! But the new Adobe Air 11b (IE only) loaded great.

Reply

5 Yogesh October 9, 2012 at 10:07 am

Just For Information:

-p0 in patch command –> entire file name
-p1 in patch command –> file name preceding first forward slash
-p2 in patch command –> file name preceding second forward slash
etc.
Example:
-p0 = /path/to/patch/file
-p1 = path/to/patch/file
-p2 = to/patch/file
-p3 = patch/file
-p4 = file

Reply

6 Francis February 18, 2013 at 11:01 am

Spent some time scratching my head over what -p0, -p1 is supposed to mean, and working out how to make patch < /path/to/file work (is /path/to/file supposed to be the file to patch, or the patch file? and how about the other file? You need 2 files to make a patch, why only one?).
Read the man file and finally encountered the simple syntax, which is what I would have guessed, for example:
patch fileTopatch.txt < myPatchFile.patch
or, an example with directories
patch foo/bar/fileTopatch.txt < patches/myPatchFile.patch
Just hopes this helps someone coming in on this page like I did half an hour ago.

Reply

7 Rob B April 29, 2014 at 11:43 am

Thanks for this. It is helpful.

Reply

8 Prasad June 26, 2014 at 4:37 am

Hi,

Great document for beginner. Thanks for sharing.

Reply

Leave a Comment

Tagged as:

Previous Faq:

Next Faq: