June 01, 2012

Create and Apply Patch file in Linux using Diff command

Hi friends,

When I was updating one file, I have taken backup as filename_old.

After updating (adding and removing line) the current file, I thought let me see what are all the updates I have done to the current file. Then I thought, there is a concept call patch file.

Then I decided to create the patch file.

I have searched and surprised that "diff" command is able to do that.

Procedure:

Files: 1.txt (Original File), 2.txt (Modified File), diff.patch (Patch File)


1.txt 2.txt
a a
b b
c c
d e
e f
f g
1


Create Patch


Syntax: diff -Naur Original_File Modified_File > Patch_File

In terminal window, execute the following command

$ diff -Naur 1.txt 2.txt >> diff.patch


Output:

diff.patch


--- 1.txt 2012-06-01 23:25:02.713727335 +0530
+++ 2.txt 2012-06-01 23:25:09.833727317 +0530
@@ -1,7 +1,6 @@
 a
 b
 c
-d
 e
 f
-1
+g




Apply Patch

Syntax: patch -u Original_File Patch_File


$ patch -u 1.txt diff.patch


Output:



1.txt
a
b
c
e
f
g


Now, we have successfully applied the patch to the old (Original) file.

Please send your feedback to psrdotcom@gmail.com or write comments

Post a Comment

Featured Post

Java Introdcution

Please send your review and feedback to psrdotcom@gmail.com