Patch is a file that describes difference between two files (old file with new file). A special program called diff will compare both files line by line and prints the differences to stdout in a special format. Patchfile can read the output of diff function and it may apply those changes to old (original) file. Sounds strange..? Let me explain more..

1) Create two directories for usage

$ mkdir old new

2) Create files with same name at both directories

$ vim old/file





$ vim new/file


3) Use diff function now

$ diff -Nru old new

diff -Nru old/file new/file
— old/file 2013-05-15 14:11:28.000000000 +0530
+++ new/file 2013-05-15 14:11:37.000000000 +0530
@@ -1,4 +1,4 @@

Looks strange…!

diff -Nru old/file new/file – It says diff function is applied to old/file and new/file files.
— old/file 2013-05-15 14:11:28.000000000 +0530 – It says we need to remove something from old/file, it provides date and time for reference
+++ new/file 2013-05-15 14:11:37.000000000 +0530 – It says we need to add something from new/file to old/file, it provides date and time for reference
@@ -1,4 +1,4 @@ – It says that we need to remove one line from first file (old/file) which holds totally 4 lines, and need to add one from second file (new/file) which holds only 4 lines.
this – first line same for both files
is – second line same for both files
-old – remove this from first file (old/file)
+new – add this line of second file (new/file) into first file (old file)

file – last line same for both files

4) Feed output of diff function into a patch file

$ diff -Nru old new > patchfile

5) Applying patchfile into old/file

$ cd old/
$ patch -p1 < ../patchfile

patching file file.txt

6) old/file is same as new/file

$ cat file


Moral of Story : To change any old file into newer, use respective patch for that. Linux Kernel version can be upgraded from old version into a newer ones.



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s