补丁:对原版本进行错误修改和添加功能
diff:创建补丁 (原则是告诉我们怎么修改第一个文件得到第二个文件)
-u | 输出统一内容的头部信息(打补丁使用) |
-r | 递归对比目录中的所有资源(可以对比目录) |
-a | 所有文件视为文本(包括二进制程序) |
-N | 无文件视为空文件(空文件怎么变成文件) |
对单个文件创建补丁
[root@security ~]# cat test1.sh
#!/bin/bash
echo "hello world"
[root@security ~]# cat test2.sh
#!/bin/bash
echo "hello the world"
echo "welcome"
[root@security ~]# diff test1.sh test2.sh //比较不同地方
2c2,3
< echo "hello world"
---
> echo "hello the world"
> echo "welcome"
[root@security ~]# diff -u test1.sh test2.sh > buding.patch 创建补丁文件.patch
[root@security ~]# cat buding.patch
--- test1.sh 2019-01-03 16:42:03.522624804 +0800
+++ test2.sh 2019-01-03 16:42:52.315624804 +0800
@@ -1,2 +1,3 @@
#!/bin/bash
-echo "hello world" // -代表删除
+echo "hello the world" //+ 代表添加
+echo "welcome"
给目录创建补丁
[root@security ~]# mkdir -p /demo/source{1,2}
[root@security ~]# cd /demo/
[root@security demo]# echo "hello world" > source1/test.sh
[root@security demo]# echo "hello the world" > source2/test.sh
[root@security demo]# cat source1/test.sh
hello world
[root@security demo]# cat source2/test.sh
hello the world
[root@security demo]# cp /bin/find source1
[root@security demo]# cp /bin/find source2
[root@security demo]# echo "1" >> source2/find
[root@security demo]# echo "test" > source2/tmp.txt
[root@security demo]# yum -y install tree
[root@security demo]# tree
.
├── source1
│ ├── find
│ └── test.sh
└── source2
├── find
├── test.sh
└── tmp.txt
2 directories, 5 files
[root@security demo]# diff -uraN source1 source2 > source.patch
[root@security demo]# cat source.patch
diff -uraN source1/find source2/find
...
diff -uraN source1/test.sh source2/test.sh
--- source1/test.sh 2019-01-03 17:21:58.772624804 +0800
+++ source2/test.sh 2019-01-03 17:22:11.124624804 +0800
@@ -1 +1 @@
-hello world
+hello the world
diff -uraN source1/tmp.txt source2/tmp.txt
--- source1/tmp.txt 1970-01-01 08:00:00.000000000 +0800
+++ source2/tmp.txt 2019-01-03 17:26:40.461624804 +0800
@@ -0,0 +1 @@
+test
patch 打补丁
选项 -p 目录层级(补丁文件和要打补丁文件路径如果一样,则是0) -RE 还原旧版本
对单个文件打补丁
[root@security ~]# yum -y install patch
[root@security ~]# which patch
/usr/bin/patch
[root@security ~]# patch -p0 < buding.patch //打补丁
patching file test1.sh
[root@security ~]# cat test1.sh
#!/bin/bash
echo "hello the world"
echo "welcome"
[root@security ~]# patch -RE < buding.patch //还原旧版本
patching file test1.sh
[root@security ~]# cat test1.sh
#!/bin/bash
echo "hello world"
给目录打补丁
[root@security demo]# cd source1 //一般打补丁会进入文件少的目录
[root@security source1]# patch -p1 < /demo/source.patch //补丁与文件不在一个目录
patching file find
patching file test.sh
patching file tmp.txt
[root@security source1]# ls
find test.sh tmp.txt
[root@security source1]# patch -RE < /demo/source.patch
patching file find
patching file test.sh
patching file tmp.txt
[root@security source1]# ls
find test.sh