Linux 系统中可以使用 diff 命令对比出新旧软件的不同,并生成补丁文件。
diff 命令基本格式为:
[root@localhost ~]# diff 选项 old new
#比较old和new文件的不同
此命令中可使用如下几个选项:
-a:将任何文档当作文本文档处理;
-b:忽略空格造成的不同;
-B:忽略空白行造成的不同;
-I:忽略大小写造成的不同;
-N:当比较两个目录时,如果某个文件只在一个目录中,则在另一个目录中视作空文件;
-r:当比较目录时,递归比较子目录;
-u:使用同一输出格式;
生成补丁文件
[root@localhost ~]# mkdir test
#建立测试目录
[root@localhost ~]# cd test
#进入测试目录
[root@localhost test]# vi old.txt
our
school
is
lampbrother
#文件old.txt,为了便于比较,将每行分开
[root@localhost test]# vi new.txt
our
school
is
lampbrother
in
Beijing
#文件new.txt
[root@localhost test]# diff -Naur /root/test/old.txt /root/test/new.txt > txt. patch
#比较两个文件的不同,同时生成txt.patch补丁文件
[root@localhost test]#vi txt.patch
#查看一下这个文件
--/root/test/old.txt 2012-11-23 05:51:14.347954373 +0800
#前一个文件
+ + + /root/test/new.txt 2012-11-23 05:50:05.772988210 +0800
#后一个文件
@@-2, 3+2, 5@@
school
is
lampbrother
+in
+beijing
#后一个文件比前一个文件多两行(用+表示)
[root@localhost test]# patch -pn < 补丁文件
#按照补丁文件进行更新
[root@localhost test]# patch -p3 < txt.patch
patching file old.txt
#给old.txt文件打补丁
[root@localhost test]# cat old.txt
#查看一下dd.txt文件的内容
our
school
is
lampbrother
in
Beijing
#多出了in Beijing两行
给旧文件打补丁依赖的不是新文件,而是补丁文件,所以即使新文件被删除也没有关系。
补丁文件中记录的目录和当前所在目录需要通过 "-pn" 选项实现同步,否则更新可能失败。
原文:https://www.cnblogs.com/outsrkem/p/13416372.html