《LINUX教程:Linux下打補(bǔ)丁patch 和 diff 命令的使用》要點(diǎn):
本文介紹了LINUX教程:Linux下打補(bǔ)丁patch 和 diff 命令的使用,希望對(duì)您有用。如果有疑問,可以聯(lián)系我們。
Q:為什么要找分歧,為什么要打補(bǔ)丁?
A:
在Linux應(yīng)用中,作為DBA,我們知道MySQL跑在Linux系統(tǒng)之上,數(shù)據(jù)庫最重要的追求便是性能,“穩(wěn)”是重中之重,所以不能動(dòng)不動(dòng)便是換系統(tǒng)或是換這換那的,這個(gè)時(shí)候除非是萬不得已,要不然都是在原有基礎(chǔ)上改改就行了,也便是給內(nèi)核及下載的一些源碼打補(bǔ)丁或者說是升級(jí),那么在Linux下使用diff制作補(bǔ)丁以及如何使用patch打補(bǔ)丁顯得尤為重要.
一、找分歧:diff命令(differences)
-- compare?files?line?by?line
一行一行的比擬文本文件
作用:
比擬兩個(gè)文件之間的差異,輸出結(jié)果為兩個(gè)文件的不同之處.
使用diff敕令制作補(bǔ)丁.
格局:
diff ?[OPTION]... ?FILES
選項(xiàng):
-u:會(huì)將分歧的地方放在一起,緊湊易讀
diff?-u?linuxidc.com1?test2?>?test.patch? ?(應(yīng)用diff命令生成補(bǔ)丁patch)
-r:遞歸比擬目錄下的所有文件(比擬文件夾時(shí)候一定要接-r)
1、diff命令:找分歧
shell> cp fruit.txt shuiguo.txt
shell> diff fruit.txt shuiguo.txt
//因?yàn)槭菑?fù)制的文件,所以文件內(nèi)容沒有差異,也就沒有輸出結(jié)果
shell> echo "banana" >>fruit.txt
shell> diff fruit.txt shuiguo.txt
9d8
< banana
//diff命令后面,第一個(gè)文件有9行,第二個(gè)文件有8行,<表現(xiàn)右邊文件內(nèi)容缺失
shell> echo "cherry" >>shuiguo.txt
shell> diff fruit.txt shuiguo.txt
9c9
< banana
---
> cherry
//diff命令后面,兩個(gè)文件都是9行,<右邊文件缺失banana,>左邊文件缺失cherry
2、diff命令:制作補(bǔ)丁文件
shell> cat ni.txt
jinan
changqing
linux
chinaitsoft
shell> cp ni.txt wo.txt
shell> diff ni.txt wo.txt
shell> diff -u ni.txt wo.txt
//copy文件沒有內(nèi)容差別
shell> echo "zhangjiacai" >>wo.txt
shell> diff -u ni.txt wo.txt
--- ni.txt 2016-11-02 16:11:35.253976996 +0800
+++ wo.txt 2016-11-02 16:13:50.037971397 +0800
@@ -2,3 +2,4 @@
changqing
linux
chinaitsoft
+zhangjiacai
shell> vim ni.txt
shell> cat ni.txt
jinan
linux
chinaitsoft
shell> diff -u ni.txt wo.txt
--- ni.txt 2016-11-02 16:16:32.930978061 +0800
+++ wo.txt 2016-11-02 16:13:50.037971397 +0800
@@ -1,3 +1,5 @@
jinan
+changqing
linux
chinaitsoft
+zhangjiacai
解析:
@@?代表一段規(guī)模
-?代表ni.txt
+?代表wo.txt
?使用?> 輸出重定向天生補(bǔ)丁文件ni-to-wo.patch
shell> diff -u ni.txt wo.txt > ni-to-wo.patch
shell> cat ni-to-wo.patch
--- ni.txt 2016-11-02 16:16:32.930978061 +0800
+++ wo.txt 2016-11-02 16:13:50.037971397 +0800
@@ -1,3 +1,5 @@
jinan
+changqing
linux
chinaitsoft
+zhangjiacai
如斯,我們就做好了一個(gè)補(bǔ)丁文件.?
二、打補(bǔ)丁:patch敕令
---?apply?a?diff?file?to?an?original.
用途:
用來打補(bǔ)丁---修補(bǔ)文件
格局:
patch? [選項(xiàng)]? 原始文件?<?補(bǔ)丁文件
-pN:N表現(xiàn)忽略N層路徑
-R:?還原到老版本
注意事變:
①如果打多個(gè)補(bǔ)丁,注意先后次序;
②打補(bǔ)丁前不要改動(dòng)源文件;
1、文件和文件的比擬
shell> diff ni.txt wo.txt
1a2
> changqing
3a5
> zhangjiacai
shell> diff ni.txt wo.txt >ni-to-wo.patch //生成補(bǔ)丁文件
shell> patch ni.txt <ni-to-wo.patch //打補(bǔ)丁
patching file ni.txt
shell> diff ni.txt wo.txt //打補(bǔ)丁成功
shell> patch -R ni.txt <ni-to-wo.patch //還原到本來的版本(撤銷打補(bǔ)丁)
patching file ni.txt
shell> diff ni.txt wo.txt
1a2
> changqing
3a5
> zhangjiacai
2、目錄和目錄的比較
[root@localhost linuxidc.com]# tree qq-v1
qq-v1
├── hosts
└── image
└── 1.txt
[root@localhost linuxidc.com]# tree qq-v2
qq-v2
├── hosts
├── image
│ └── 1.txt
├── passwd
└── sound
└── 3.txt
[root@localhost linuxidc.com]# diff -ur qq-v1 qq-v2
Only in qq-v2: passwd
Only in qq-v2/sound: 3.txt
[root@localhost linuxidc.com]# diff -Nur qq-v1 qq-v2
diff -Nru qq-v1/passwd qq-v2/passwd
--- qq-v1/passwd 1970-01-01 08:00:00.000000000 +0800
+++ qq-v2/passwd 2016-11-02 17:07:47.664980339 +0800
@@ -0,0 +1,31 @@
+root:x:0:0:root:/root:/bin/bash
+bin:x:1:1:bin:/bin:/sbin/nologin
解析:
-N?--new-file(Treat?absent?files?as?empty)如果沒有文件,就拿一個(gè)空文件和別的目錄里的文件比擬
制作補(bǔ)丁文件進(jìn)行對(duì)目次的打補(bǔ)丁
[root@localhost linuxidc.com]#?diff?-Nur?qq-v1 qq-v2?>patch-v2.txt #比擬文件夾生成補(bǔ)丁文件--備用:補(bǔ)丁文件patch-v2.txt在linuxidc.com目錄下
-pnum? or? --strip=num
? ? ?Strip?the?smallest?prefix?containing?num?leading?slashes?from?each?file?name?
found?in?the?patch?file.?
例如:/a/b/c/d/e/f/g
-p3?的效果便是去掉第3個(gè)/前面的內(nèi)容,效果:c/d/e/f/g
-p4?的效果便是去掉第4個(gè)/前面的內(nèi)容,效果:d/e/f/g
1> 內(nèi)層打補(bǔ)丁
[root@localhost linuxidc.com]# cd qq-v1 #進(jìn)入qq目錄,進(jìn)去里面進(jìn)行打補(bǔ)丁
[root@localhost qq-v1]# patch -p1 <../patch-v2.txt
patching file passwd
patching file sound/3.txt
[root@localhost qq-v1]# cd ..
[root@localhost linuxidc.com]# diff -Nru qq-v1 qq-v2
//沒有輸出結(jié)果闡明打補(bǔ)丁成功
[root@localhost linuxidc.com]# cd qq-v1
[root@localhost qq-v1]# patch -R -p1 <../patch-v2.txt //撤銷補(bǔ)丁
patching file passwd
patching file sound/3.txt
[root@localhost qq-v1]# cd ..
[root@localhost linuxidc.com]# diff -Nru qq-v1 qq-v2
diff -Nru qq-v1/passwd qq-v2/passwd
--- qq-v1/passwd 1970-01-01 08:00:00.000000000 +0800
+++ qq-v2/passwd 2016-11-02 17:07:47.664980339 +0800
@@ -0,0 +1,31 @@
+root:x:0:0:root:/root:/bin/bash
+bin:x:1:1:bin:/bin:/sbin/nologin
2> 外層打補(bǔ)丁
//如果qq-v1和qq-v2在相同目錄下,就不必要去掉一層路徑
[root@localhost linuxidc.com]# patch -p0 <patch-v2.txt
patching file qq-v1/passwd
patching file qq-v1/sound/3.txt
墻裂建議:
任何操作前,記得對(duì)文件、目錄做好備份,防止操作失敗導(dǎo)致數(shù)據(jù)喪失.
本文永遠(yuǎn)更新鏈接地址:
學(xué)習(xí)更多LINUX教程,請(qǐng)查看站內(nèi)專欄,如果有LINUX疑問,可以加QQ交流《LINUX教程:Linux下打補(bǔ)丁patch 和 diff 命令的使用》。
轉(zhuǎn)載請(qǐng)注明本頁網(wǎng)址:
http://www.snjht.com/jiaocheng/12100.html