Saturday 9 November 2013

Command line diff example

I had to compare some files for changes and used the diff command, thought I would add an example for reference.

File 1:

user@pc:~$ cat test1.txt
aa
aa
aa

File 2:
 user@pc:~$ cat test2.txt
aa
aa
bb
cc
dd
user@pc:~$

 Then the compare:

user@pc:~$ diff -rupN test1.txt test2.txt
--- test1.txt    2013-01-01 22:50:09.918800341 +0000
+++ test2.txt    2013-01-01 22:52:28.214796256 +0000
@@ -1,3 +1,5 @@
 aa
 aa
-aa
+bb
+cc
+dd
user@pc:~$
 Creating a patch file that will contain the difference:

user@pc:~$ diff -rupN test1.txt test2.txt > test3.diff
user@pc:~$ cat test3.diff
--- test1.txt    2013-01-01 22:50:09.918800341 +0000
+++ test2.txt    2013-01-01 22:52:28.214796256 +0000
@@ -1,3 +1,5 @@
 aa
 aa
-aa
+bb
+cc
+dd

Then to patch file 1:
user@pc:~$ patch test1.txt < test3.diff
patching file test1.txt

user@pc:~$ cat test1.txt
aa
aa
bb
cc
dd
user@pc:~$ 

 And if you need to reverse the patch:
user@pc:~$ patch -R test1.txt < test3.diff
patching file test1.txt

user@pc:~$ cat test1.txt
aa
aa
aa
user@pc:~$


No comments:

Post a Comment