diff和patch更新补丁

diff和patch更新补丁

  • diff用来对比文件差异和生成补丁文件
  • patch为旧版本打补丁

diff命令

命令说明
diff -u输出统一内容的头部信息(打补丁使用),计算机知道是哪个文件需要修改
diff -r递归对比目录中的所有资源(可以对比目录)
diff -a所有文件视为文本(包括二进制程序)
diff -N无文件视为空文件
  • 说明-N作用
mkdir A 
mkdir B   #先创建两个空目录 A B
echo i am not full >  A/1.txt   #在A目录下创建1.txt
diff A B #对比A B目录
only in A: 1.txt     #警告只有A目录下有1.txt 无法比较

diff -N A B #加上N参数    将无文件视为空文件
diff -u -N A/1.txt B/1.txt
--- A/1.txt     2021-01-12 11:16:25.467376174 -0500
+++ B/1.txt     1969-12-31 19:00:00.000000000 -0500
@@ -1 +0,0 @@
-i am not full

#加上N 就能比较了    因为有和无文件无法比较   只能有和空文件比较  大概这个意思

diff测试

vim test1.sh
=================
#!/bin/bash
echo "i am v1 version"
=======================

vim test2.sh
======================
#!/bin/bash
echo "I am the second version"
================================= 
#准备两个不同的文件

diff test1.sh test2.sh #查看文件差异
2c2
< echo "i am v1 version"
---
> echo "I'm the second verison"

diff -u test1.sh test2.sh #查看差异 包含头部信息
--- test1.sh    2021-01-12 10:39:12.837272903 -0500
+++ test2.sh    2021-01-12 10:39:41.782274242 -0500
@@ -1,2 +1,2 @@
 #!/bin/bash
-echo "i am v1 version"
+echo "I'm the second verison"

#diff告诉怎么从第一个文件修改成第二个文件  不需要全部替换

diff -u test1.sh test2.sh > test1-2.patch   #生成test1.sh升级成test2.sh的补丁
cat test1-2.patch   #查看下里面是什么东西
--- test1.sh    2021-01-12 10:39:12.837272903 -0500
+++ test2.sh    2021-01-12 10:39:41.782274242 -0500
@@ -1,2 +1,2 @@
 #!/bin/bash
-echo "i am v1 version"
+echo "I'm the second verison"
#。。。。。好像就是diff -u 输出的东西。。。。 我自己重定向进行的。。。。

patch打补丁

patch 命令
参数说明
patch -pnum-pnum(num为0~n数字,指定删除补丁文件中多少层路径前缀) 剥离层级数
patch -R反向修复
patch -E修复后如果文件为空,则删除该文件
patch -a对比所有文件 不单单对比文本文件了 包括二进制文件
patch -r递归对比
  • patch -pnum参数说明

比较难理解的参数 首先写该参数的数字时(就是-pnum的num) 需要判断两个参数 分别是当前路径和patch补丁文件里面的路径名

patch命令执行根据是.patch文件里面的路径名去找对应文件进行修改,所以需要人为的用-pnum来层级剥离路径来对应想要打补丁的旧文件和当前路径的关系

举个例子

当前路径为/root/demo/

旧文件是/root/demo/old.txt

补丁文件是/root/old.patch 补丁文件里面的路径为/root/pro/test/old.txt

所以你在/root/demo执行patch命令 那么就需要剥离4层 剥离/root/pro/test/ 得到old.txt 所以patch命令就能找到当前目录下的old.txt文件了

最终执行命令为patch -p4 < …/old.patch

yum -y install patch 
patch -p0 < test1-2.patch  #根据补丁升级
patching file test1.sh    #test1.sh被更新了

cat test1.sh  #查看下
#!/bin/bash
echo "I'm the second verison"  #升级变成test2.sh的内容了

patch -RE < test1-0.patch  #还原旧版本,反向修复
patching file test1.sh

cat test1.sh #查看下
#!/bin/bash
echo "i am v1 version"

目录补丁

案例测试
mkdir demo  #创建实验目录
cd demo
mkdir source{1,2}    #创建两个不同的目录

echo "hello world" > source1/test.sh  
cp /bin/find source1/     
tree source1/
source1
├── find
└── test.sh

echo "hello the world" > source2/test.sh
echo "test" > source2/tmp.txt
cp /bin/find source2/
echo "1" >> source2/find
tree source2
source2
├── find
├── test.sh
└── tmp.txt
#source1 source2 都有find和test.sh文件 但是内容不同  source2额外有tmp.txt


diff -u source1/ source2/
Binary files source1/find and source2/find differ    #没有对比find二进制文件
diff -u source1/test.sh source2/test.sh               #对比了test.sh
--- source1/test.sh     2021-01-12 11:45:20.005456405 -0500
+++ source2/test.sh     2021-01-12 11:47:55.275463587 -0500
@@ -1 +1 @@
-hello world
+hello the world
Only in source2/: tmp.txt                  #tmp.txt没有对应文件对比
#总结  仅仅对比了文本文件test.sh  二进制文件 tmp 都没有对比
#-a解决二进制文件find    -N解决tmp.txt文件

diff -Nuar source1/ source2/ > source.patch  #生成目录source1到目录source2的补丁

cd source1
patch -p1 < ../source.patch       #当前在/root/demo/source1  所以执行patch -p1   将source1/test.sh前面的source1/这段去掉  就可以在当前目录下找到test.sh进行更新了     说白了就是路径问题 怎么讲patch补丁文件中的原本路径对应到当前路径去执行文件的修改操作
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值