Monday 30 July 2012

How to apply PATCH in Linux to your code base?


What is Patch?

    Patch is Unix Command used to updates file according to instructions contained in a separate filet is called as Patch file.

What is Patch file?
    Patch file is a text file that consists of a list of differences and is produced by running the related "diff"command with the original and updated file as arguments.

Updating files with patch is often referred to as applying the patch or simply patching the files.

How to apply patch to directory?

If you want to apply patch(patch file contains list of differences) to directory, please refer the below steps.

Step 1
Take the diff between the <unmodified Directory> < modified directory>   using the below command

Command:  
diff  -Ncpr   (NotModifieddirectory(Original( modified directory)   > patch_file.txt

Ex:   

diff  -Ncpr   linux-26_org   linux-26   > patch_file.txt

Ex:  Use -x Option to omit some directory 
diff  -Ncpr   -x <omit> linux-26_org   linux-26   > patch_file.txt

Step 2
Open the patch_file.txt and check the differences if it proper or not.
vi  patch_file.txt 

Step 3
Before actually applying patch to the respective directory, first validate your patch file is proper or not. This is also called as "dry run".


Be in appropriate directory( where you want do apply patch). For example, If the patch is taken for XXX directory then


cd  XXX
 
Run the patch--dry-run and redirect  the output to another file.  
patch -p0 --dry-run -i   <patchfile.txt>  > Patch_output


Step 4:    
Just check the output of the Patch_output file. If there are any HUNK Failures, then patch file is having some problem. Please try to take patch file one more time.( using "diff" command).

Step 5:
If there are No HUNK failures, then go ahead and apply the patch to the respective directory.
patch -p0  -i   <patchfile.txt>  > Patch_output 
 



8 comments: