例:
[root@localhost test]# cat A.txt
1
2
5
[root@localhost test]# cat B.txt
1
3
6
[root@localhost test]# diff A.txt B.txt -u
--- A.txt 2012-05-19 11:09:01.000000000 +0800
+++ B.txt 2012-05-19 11:09:25.000000000 +0800
@@ -1,3 +1,3 @@
1
-2
-5
+3
+6
其中加上-u是生成一体化输出,这样易读性更好,更易于看出两个文件之间的差异。
在一体化的diff中,以+起始的行表示是新加入的行,-起始的行表示被删除的行。
将一体化的修补文件,重定向到一个新文件,得到修补文件:
[root@localhost test]# diff A.txt B.txt -u > version.patch
将修补谁的用于A.txt,会将A.txt修改为B.txt:
[root@localhost test]# patch -p1 A.txt < version.patch
missing header for unified diff at line 3 of patch
patching file A.txt
[root@localhost test]# cat A.txt
1
3
6
同样,将修被文件用于B.txt,会将B.txt修改为A.txt:
[root@localhost test]# patch -p1 B.txt < version.patch
missing header for unified diff at line 3 of patch
patching file B.txt
Reversed (or previously applied) patch detected! Assume -R? [n] y
[root@localhost test]# cat B.txt
1
2
5