diff和patch工具打补丁
原创
©著作权归作者所有:来自51CTO博客作者行走的皮卡丘的原创作品,请联系作者获取转载授权,否则将追究法律责任
diff和patch工具打补丁
1 对比单个文件差异
1.1 编写两个版本的脚本,一个为v1版本,一个为v2版本。
vim test1.sh #v1版本脚本
#!/bin/bash
echo "hello wrld"
vim test2.sh #v2版本脚本
#!/bin/bash
echo "hello the world"
echo "test file"
2.2 使用diff命令语法
使用diff命令查看不同版本文件的差异。
diff test1.sh test2.sh #查看文件差异
2c2,3
< echo "hello wrld"
---
> echo "hello the world"
> echo "test file"
diff -u test1.sh test2.sh #查看差异,包含头部信息
--- test1.sh 2020-08-13 14:59:39.161420021 +0800
+++ test2.sh 2020-08-13 14:59:57.121420407 +0800
@@ -1,2 +1,3 @@
#!/bin/bash
-echo "hello wrld"
+echo "hello the world"
+echo "test file"
diff制作补丁文件的原理:告诉我们怎么修改第一个文件后能得到第二个文件。
这样如果第一个版本的脚本有漏洞,我们不需要将整个脚本都替换,仅需要修改有问题的一小部分代码即可,diff刚好可以满足这个需求!
像Linux内核这样的大块头,一旦发现有一个小漏洞,我们不可能把整个内核都重新下载,全部替换一遍,而仅需要更新有问题的那一小部分代码即可!
- -
u
输出统一内容的头部信息(打补丁使用),计算机知道是哪个文件需要修改 - -
r
递归对比目录中的所有资源(可以对比目录) - -
a
所有文件视为文本(包括二进制程序) - -
N
无文件视为空文件(空文件怎么变成第二个文件)
- -
N
选项备注说明: -
A
目录下没有txt文件,B目录下有txt文件 -
diff
比较两个目录时,默认会提示txt仅在B目录有(无法对比差异,修复文件) -
diff
比较时使用N选项,则diff会拿B下的txt与A下的空文件对比,补丁信息会明确说明如何从空文件修改后变成txt文件,打补丁即可成功!
2 使用patch命令对单文件代码打补丁
2.1 生成补丁文件
diff -u test1.sh test2.sh >
2.2 使用patch命令打补丁
在代码相同目录下为代码打补丁
yum -y install patch
patch -p0 < test.patch #打补丁
patching file test1.sh
#patch -pnum(其中num为数字,指定删除补丁文件中多少层路径前缀)
#如原始路径为/u/howard/src/blurfl/blurfl.c
#-p0则整个路径不变
#-p1则修改路径为u/howard/src/blurfl/blurfl.c
#-p4则修改路径为blurfl/blurfl.c
#-R(reverse)反向修复,-E修复后如果文件为空,则删除该文件
cat test1.sh
#!/bin/bash
echo "hello the world"
echo "test file"
patch -RE < test.patch #还原旧版本,反向修复
cat test1.sh
#!/bin/bash
echo "hello wrld"
3 对比目录中所有文件的差异
3.1 准备实验环境
mkdir demo
cd demo
mkdir {source1,source2}
echo "hello world" > source1/test.sh
cp /bin/find source1/
tree source1/ #source1目录下2个文件
source1/
├── find
└── test.sh
0 directories, 2 files
[root@proxy demo]# echo "hello the world" > source2/test.sh
[root@proxy demo]# echo "test" > source2/tmp.txt
[root@proxy demo]# cp /bin/find source2/
[root@proxy demo]# echo "1" >> source2/find
[root@proxy demo]# tree source2/ #source1目录下3个文件
source2/
├── find
├── test.sh
└── tmp.txt
0 directories, 3 files
#注意:两个目录下find和test.sh文件内容不同,source2有tmp.txt而source1没有该文件
3.2 对比差异
diff -u source1/ source2/
#仅对比了文本文件test.sh;二进制文件、tmp都没有对比差异,仅提示,因为没有-a和-N选项
Binary files source1/find and source2/find differ
diff -u source1/test.sh source2/test.sh
--- source1/test.sh 2020-08-13 15:12:51.233437049 +0800
+++ source2/test.sh 2020-08-13 15:14:20.849438976 +0800
@@ -1 +1 @@
-hello world
+hello the world
只在 source2/ 存在:tmp.txt
diff -Nu source1/ source2/
#对比了test.sh,并且使用source2目录的tmp.txt与source1的空文件对比差异。
Binary files source1/find and source2/find differ
diff -Nu source1/test.sh source2/test.sh
--- source1/test.sh 2020-08-13 15:12:51.233437049 +0800
+++ source2/test.sh 2020-08-13 15:14:20.849438976 +0800
@@ -1 +1 @@
-hello world
+hello the world
diff -Nu source1/tmp.txt source2/tmp.txt
--- source1/tmp.txt 1970-01-01 08:00:00.000000000 +0800
+++ source2/tmp.txt 2020-08-13 15:14:25.569439078 +0800
@@ -0,0 +1 @@
+test
diff -Nua source1/ source2/
#对比了test.sh、tmp.txt、find(程序)。
4 patch命令对目录下的所有代码打补丁
4.1 前面创建的source1和source2目录下的代码为素材,生成补丁文件
diff -Nuar source1/ source2/ >
4.2 使用patch命令为代码打补丁
ls
source1 source2 source.patch
cat source.patch #对比的文件有路径信息
cd source1
patch -p1 < ../source.patch