Linux下文件比较工具diff
文件比较工作,win下有很多选择,神器beyond compare是一个不二选择,对于简单文本比较可以使用文本编辑器的插件,如notepad++的插件。
linux工具也有很多,版本控制工具多是使用diff原理来进行的。
对于现在的开发人员是幸福的,因为有各种版本控制工具可以使用,对于没有版本控制工具编写的代码,想知道代码的不同,diff是我知道的工具。
比如没有版本控制时的linux内核技术使用diff和patc来维护的。对于diff的输出结果经常表示看不懂,本文对这个详细说明。
如下三列,第一列为原文件old.txt,第二列示新文件new.txt,第三列示两个文件diff old.txx new.txt结果
1 This part of the 2 document has stayed the 3 same from version to 4 version. It shouldn't 5 be shown if it doesn't 6 change. Otherwise, that 7 would not be helping to 8 compress the size of the 9 changes. 10 11 This paragraph contains 12 text that is outdated. 13 It will be deleted in the 14 near future. 15 16 It is important to spell 17 check this dokument. On 18 the other hand, a 19 misspelled word isn't 20 the end of the world. 21 Nothing in the rest of 22 this paragraph needs to 23 be changed. Things can 24 be added after it. |
1 This is an important 2 notice! It should 3 therefore be located at 4 the beginning of this 5 document! 6 7 This part of the 8 document has stayed the 9 same from version to 10 version. It shouldn't 11 be shown if it doesn't 12 change. Otherwise, that 13 would not be helping to 14 compress anything. 15 16 It is important to spell 17 check this document. On 18 the other hand, a 19 misspelled word isn't 20 the end of the world. 21 Nothing in the rest of 22 this paragraph needs to 23 be changed. Things can 24 be added after it. 25 26 This paragraph contains 27 important new additions 28 to this document. |
1,14c1,14 < 1 This part of the < 2 document has stayed the < 3 same from version to < 4 version. It shouldn't < 5 be shown if it doesn't < 6 change. Otherwise, that < 7 would not be helping to < 8 compress the size of the < 9 changes. < 10 < 11 This paragraph contains < 12 text that is outdated. < 13 It will be deleted in the < 14 near future. --- > 1 This is an important > 2 notice! It should > 3 therefore be located at > 4 the beginning of this > 5 document! > 6 > 7 This part of the > 8 document has stayed the > 9 same from version to > 10 version. It shouldn't > 11 be shown if it doesn't > 12 change. Otherwise, that > 13 would not be helping to > 14 compress anything. 17c17 < 17 check this dokument. On --- > 17 check this document. On 24a25,28 > 25 > 26 This paragraph contains > 27 important new additions > 28 to this document. |
<后的内容是左边文件内容
>后的内容是右边文件内容
尖头指向哪边就是哪边文件的内容
0a1,6 左边的文件0行后面插入右边文件的1-6行
8,14c14 左边文件的8-14行修改为右边文件的14行
常见输出结果还有d,表示删除
通过patch命令打补丁,把就文件更新为新文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
diff -uN old.txt new.txt --- old.txt 2015-06-26 22:20:30.000000000 +0800 +++ new.txt 2015-06-26 22:21:07.000000000 +0800 @@ -1,20 +1,20 @@ - 1 This part of the - 2 document has stayed the - 3 same from version to - 4 version. It shouldn't - 5 be shown if it doesn't - 6 change. Otherwise, that - 7 would not be helping to - 8 compress the size of the - 9 changes. -10 -11 This paragraph contains -12 text that is outdated. -13 It will be deleted in the -14 near future. + 1 This is an important + 2 notice! It should + 3 therefore be located at + 4 the beginning of this + 5 document! + 6 + 7 This part of the + 8 document has stayed the + 9 same from version to +10 version. It shouldn't +11 be shown if it doesn't +12 change. Otherwise, that +13 would not be helping to +14 compress anything. 15 16 It is important to spell -17 check this dokument. On +17 check this document. On 18 the other hand, a 19 misspelled word isn't 20 the end of the world. @@ -22,3 +22,7 @@ 22 this paragraph needs to 23 be changed. Things can 24 be added after it. +25 +26 This paragraph contains +27 important new additions +28 to this document. |
--- old.txt 2015-06-26 22:20:30.000000000 +0800
旧文件
+++ new.txt 2015-06-26 22:21:07.000000000 +0800
新文件
@@ -1,20 +1,20 @@
旧文件-1,20,旧文件变化区域1-20行
新文件+1,20,新文件变化区域1-20行
- 1 This part of the
删除这行内容
+ 1 This is an important
增加这行内容
1 2 |
diff -uN old.txt new.txt >old.patch patch -p0 < old.patch |
-p后面的参数表示去掉补丁文件中文件名前的‘/’个数,0表示补丁文件和要打补丁的文件在同一个目录。