SlideShare a Scribd company logo
1 of 3
<diff 和 patch>
         摘錄自鳥哥的 Linux 私房菜--正規表示法 Regular Expression, RE
           http://linux.vbird.org/linux_basic/0330regularex.php#doc_compare


 diff
diff 就是用在比對兩個檔案之間的差異的,一般是用在 ASCII 純文字檔的比對上。
我們先預處理一下一個檔案好了。假設我要將 /etc/passwd 的內容,將第四行刪除,
第六行則取代成為『no six line』,新的檔案放置到 /tmp/test 裡面, 那麼應該怎麼
做?
[root@linux ~]# mkdir -p /tmp/test
[root@linux ~]# cat /etc/passwd | 
> sed -e '4d' -e '6c no six line' > /tmp/test/passwd
# 注意一下 sed 後面如果要接超過兩個以上的動作時 每個動作前面得加 -e 才
          ,                                     ,
行!
接下來討論一下關於 diff 的用法吧!
[root@linux ~]# diff [-bBi] from-file to-file
參數:
from-file :一個檔名,作為原始比對檔案的檔名;
to-file   :一個檔名,作為目的比對檔案的檔名;
注意,from-file 或 to-file 可以 - 取代,那個 - 代表『Standard input』之
意。

-b :忽略一行當中,僅有多個空白的差異(例如 "about me" 與 "about                                   me"
視為相同
-B :忽略空白行的差異。
-i :忽略大小寫的不同。
範例:

範例一:比對 /tmp/test/passwd 與 /etc/passwd 的差異:
[root@linux ~]# diff /etc/passwd /tmp/test/passwd
4d3    <==這裡是說,左邊檔案(/etc/passwd)第四行被刪除 (d)
< adm:x:3:4:adm:/var/adm:/sbin/nologin
6c5    <==這裡是說,左邊檔案的第六行被取代成右邊檔案(/tmp/test/passwd)
的第五行
< sync:x:5:0:sync:/sbin:/bin/sync
---
> no six line
# 很聰明吧!用 diff 就把我們剛剛的處理給比對完畢了!
用 diff 比對檔案真的是很簡單喔!另外, diff 也可以比對整個目錄下的差異喔!
舉例來說,我們將兩個目錄比對一下:
[root@linux ~]# diff /etc /tmp/test
......(前面省略).....
Only in /etc: paper.config
diff /etc/passwd /tmp/test/passwd
4d3
< adm:x:3:4:adm:/var/adm:/sbin/nologin
6c5
< sync:x:5:0:sync:/sbin:/bin/sync
---
> no six line
Only in /etc: passwd-
......(後面省略).....
我們的 diff 很聰明吧!還可以比對不同目錄下的相同檔名的內容, 這樣真的很方
便喔~


 patch
patch 這個指令與 diff 可是有密不可分的關係啊!我們前面提到, diff 可以用來分
辨兩個版本之間的差異,舉例來說,剛剛我們所建立的 /tmp/test/passwd 與
/etc/passwd 就是兩個不同版本之間的檔案。那麼,如果要『升級』呢? 就是『將舊
的檔案升級成為新的檔案』       時,應該要怎麼做呢? 舉例來說,我們可以這樣做測試:
[root@linux ~]# mkdir /tmp/old; cp /etc/passwd /tmp/old
[root@linux ~]# mkdir /tmp/new; cp /tmp/test/passwd /tmp/new
[root@linux ~]# cd /tmp ; diff -Naur old/ new/ > test.patch
此時,在 /tmp/test.patch 檔案之中,就記錄了新舊的檔案之間的差異, 對了!您必
須要瞭解的是,用 diff 製作這個檔案時,舊的檔案必須是在前面,亦即是 diff oldfile
newfile 才行喔!此外    ,新舊檔案的 『相對目錄位置』 最好也是一樣比較好喔! OK!
那麼如何將舊的內容 (/tmp/old/passwd) 更新到新版 (/tmp/new/passwd) 的內容呢?
簡單的說,可以用這樣:
[root@linux ~]# patch -pN < patch_file
參數:
-p :後面可以接『取消幾層目錄』的意思。
範例:

範例一:將剛剛製作出來的 patch file 用來更新舊版資料
[root@linux ~]# cd /tmp/old
[root@linux ~]# patch -p1 < /tmp/test.patch
patching file passwd
# 為什麼這裡會使用 -p1 呢?因為我們在比對新舊版的資料時 是在 /tmp 底下            ,          ,
# 而實際的資料是在 /tmp/old 裡面,因此,當我們進入到 /tmp/old 時,
# 再查閱 /tmp/test.patch 的第一行如下:
# diff -Naur old/passwd new/passwd (用 head -n 1 /tmp/test.patch)
# 發現到,我們所在的目錄其實是 old 裡面,所以,就必須要減去一層目錄。
更詳細的 patch 用法我們會在後續的第五章跟大家介紹, 這裡僅是介紹給您,呵
呵!我們可以利用 diff 來比對兩個檔案之間的差異, 更可進一步利用這個功能來
製作修補檔案 (patch file) ,讓大家更容易進行比對與升級呢! 很不賴吧! ^_^

More Related Content

Viewers also liked

Windows Phone 8 overview and the developer oportunity
Windows Phone 8 overview and the developer oportunityWindows Phone 8 overview and the developer oportunity
Windows Phone 8 overview and the developer oportunityStar Channel - Nea Tileorasi
 
Java Thread and Process Performance for Parallel Machine Learning on Multicor...
Java Thread and Process Performance for Parallel Machine Learning on Multicor...Java Thread and Process Performance for Parallel Machine Learning on Multicor...
Java Thread and Process Performance for Parallel Machine Learning on Multicor...Saliya Ekanayake
 

Viewers also liked (6)

Forthnet catalogue October - November 2012
Forthnet catalogue October - November 2012Forthnet catalogue October - November 2012
Forthnet catalogue October - November 2012
 
Windows Phone 8 overview and the developer oportunity
Windows Phone 8 overview and the developer oportunityWindows Phone 8 overview and the developer oportunity
Windows Phone 8 overview and the developer oportunity
 
BRIC Case Study
BRIC Case StudyBRIC Case Study
BRIC Case Study
 
Forthnet Δεκέμβριος 2012
Forthnet Δεκέμβριος 2012Forthnet Δεκέμβριος 2012
Forthnet Δεκέμβριος 2012
 
Java Thread and Process Performance for Parallel Machine Learning on Multicor...
Java Thread and Process Performance for Parallel Machine Learning on Multicor...Java Thread and Process Performance for Parallel Machine Learning on Multicor...
Java Thread and Process Performance for Parallel Machine Learning on Multicor...
 
J Geygan 2016 Resume
J Geygan 2016 ResumeJ Geygan 2016 Resume
J Geygan 2016 Resume
 

Similar to Diff and Patch command Manual

Sed 命令详解
Sed 命令详解Sed 命令详解
Sed 命令详解climbtop
 
linux 從無到有
linux 從無到有linux 從無到有
linux 從無到有alex867
 
sed -- A programmer's perspective
sed -- A programmer's perspectivesed -- A programmer's perspective
sed -- A programmer's perspectiveLi Ding
 
2, bash synax simplified
2, bash synax simplified2, bash synax simplified
2, bash synax simplifiedted-xu
 
Maintainable PHP Source Code
Maintainable PHP Source CodeMaintainable PHP Source Code
Maintainable PHP Source CodeBo-Yi Wu
 
5, sed
5, sed5, sed
5, sedted-xu
 
Learning python in the motion picture industry by will zhou
Learning python in the motion picture industry   by will zhouLearning python in the motion picture industry   by will zhou
Learning python in the motion picture industry by will zhouWill Zhou
 
康盛创想项目部Linux 服务器部署标准(最新版)
康盛创想项目部Linux 服务器部署标准(最新版)康盛创想项目部Linux 服务器部署标准(最新版)
康盛创想项目部Linux 服务器部署标准(最新版)Yiwei Ma
 
X64服务器 lamp服务器部署标准 new
X64服务器 lamp服务器部署标准 newX64服务器 lamp服务器部署标准 new
X64服务器 lamp服务器部署标准 newYiwei Ma
 
Hdfs raid migration to hadoop 1.x
Hdfs raid migration to hadoop 1.x Hdfs raid migration to hadoop 1.x
Hdfs raid migration to hadoop 1.x Jiang Yu
 
新北市教師工作坊 -- Bash script programming 介紹
新北市教師工作坊 -- Bash script programming 介紹新北市教師工作坊 -- Bash script programming 介紹
新北市教師工作坊 -- Bash script programming 介紹fweng322
 
Linux安全配置终极指南
Linux安全配置终极指南Linux安全配置终极指南
Linux安全配置终极指南wensheng wei
 
makefile20141121
makefile20141121makefile20141121
makefile20141121Kevin Wu
 
Puppet安装测试
Puppet安装测试Puppet安装测试
Puppet安装测试Yiwei Ma
 
Effective linux.1.(commandline)
Effective linux.1.(commandline)Effective linux.1.(commandline)
Effective linux.1.(commandline)wang hongjiang
 
HITCON CTF 2014 BambooFox 解題心得分享
HITCON CTF 2014 BambooFox 解題心得分享HITCON CTF 2014 BambooFox 解題心得分享
HITCON CTF 2014 BambooFox 解題心得分享Chong-Kuan Chen
 
Linux常用命令与工具简介
Linux常用命令与工具简介Linux常用命令与工具简介
Linux常用命令与工具简介weihe
 

Similar to Diff and Patch command Manual (20)

Sed 命令详解
Sed 命令详解Sed 命令详解
Sed 命令详解
 
linux 從無到有
linux 從無到有linux 從無到有
linux 從無到有
 
sed -- A programmer's perspective
sed -- A programmer's perspectivesed -- A programmer's perspective
sed -- A programmer's perspective
 
2, bash synax simplified
2, bash synax simplified2, bash synax simplified
2, bash synax simplified
 
Maintainable PHP Source Code
Maintainable PHP Source CodeMaintainable PHP Source Code
Maintainable PHP Source Code
 
5, sed
5, sed5, sed
5, sed
 
Learning python in the motion picture industry by will zhou
Learning python in the motion picture industry   by will zhouLearning python in the motion picture industry   by will zhou
Learning python in the motion picture industry by will zhou
 
康盛创想项目部Linux 服务器部署标准(最新版)
康盛创想项目部Linux 服务器部署标准(最新版)康盛创想项目部Linux 服务器部署标准(最新版)
康盛创想项目部Linux 服务器部署标准(最新版)
 
X64服务器 lamp服务器部署标准 new
X64服务器 lamp服务器部署标准 newX64服务器 lamp服务器部署标准 new
X64服务器 lamp服务器部署标准 new
 
Hdfs raid migration to hadoop 1.x
Hdfs raid migration to hadoop 1.x Hdfs raid migration to hadoop 1.x
Hdfs raid migration to hadoop 1.x
 
新北市教師工作坊 -- Bash script programming 介紹
新北市教師工作坊 -- Bash script programming 介紹新北市教師工作坊 -- Bash script programming 介紹
新北市教師工作坊 -- Bash script programming 介紹
 
Linux安全配置终极指南
Linux安全配置终极指南Linux安全配置终极指南
Linux安全配置终极指南
 
makefile20141121
makefile20141121makefile20141121
makefile20141121
 
Puppet安装测试
Puppet安装测试Puppet安装测试
Puppet安装测试
 
Effective linux.1.(commandline)
Effective linux.1.(commandline)Effective linux.1.(commandline)
Effective linux.1.(commandline)
 
第五章
第五章第五章
第五章
 
HITCON CTF 2014 BambooFox 解題心得分享
HITCON CTF 2014 BambooFox 解題心得分享HITCON CTF 2014 BambooFox 解題心得分享
HITCON CTF 2014 BambooFox 解題心得分享
 
Unix常用命令
Unix常用命令Unix常用命令
Unix常用命令
 
Overlayfs and VFS
Overlayfs and VFSOverlayfs and VFS
Overlayfs and VFS
 
Linux常用命令与工具简介
Linux常用命令与工具简介Linux常用命令与工具简介
Linux常用命令与工具简介
 

Diff and Patch command Manual

  • 1. <diff 和 patch> 摘錄自鳥哥的 Linux 私房菜--正規表示法 Regular Expression, RE http://linux.vbird.org/linux_basic/0330regularex.php#doc_compare  diff diff 就是用在比對兩個檔案之間的差異的,一般是用在 ASCII 純文字檔的比對上。 我們先預處理一下一個檔案好了。假設我要將 /etc/passwd 的內容,將第四行刪除, 第六行則取代成為『no six line』,新的檔案放置到 /tmp/test 裡面, 那麼應該怎麼 做? [root@linux ~]# mkdir -p /tmp/test [root@linux ~]# cat /etc/passwd | > sed -e '4d' -e '6c no six line' > /tmp/test/passwd # 注意一下 sed 後面如果要接超過兩個以上的動作時 每個動作前面得加 -e 才 , , 行! 接下來討論一下關於 diff 的用法吧! [root@linux ~]# diff [-bBi] from-file to-file 參數: from-file :一個檔名,作為原始比對檔案的檔名; to-file :一個檔名,作為目的比對檔案的檔名; 注意,from-file 或 to-file 可以 - 取代,那個 - 代表『Standard input』之 意。 -b :忽略一行當中,僅有多個空白的差異(例如 "about me" 與 "about me" 視為相同 -B :忽略空白行的差異。 -i :忽略大小寫的不同。 範例: 範例一:比對 /tmp/test/passwd 與 /etc/passwd 的差異: [root@linux ~]# diff /etc/passwd /tmp/test/passwd 4d3 <==這裡是說,左邊檔案(/etc/passwd)第四行被刪除 (d) < adm:x:3:4:adm:/var/adm:/sbin/nologin 6c5 <==這裡是說,左邊檔案的第六行被取代成右邊檔案(/tmp/test/passwd) 的第五行 < sync:x:5:0:sync:/sbin:/bin/sync --- > no six line # 很聰明吧!用 diff 就把我們剛剛的處理給比對完畢了!
  • 2. 用 diff 比對檔案真的是很簡單喔!另外, diff 也可以比對整個目錄下的差異喔! 舉例來說,我們將兩個目錄比對一下: [root@linux ~]# diff /etc /tmp/test ......(前面省略)..... Only in /etc: paper.config diff /etc/passwd /tmp/test/passwd 4d3 < adm:x:3:4:adm:/var/adm:/sbin/nologin 6c5 < sync:x:5:0:sync:/sbin:/bin/sync --- > no six line Only in /etc: passwd- ......(後面省略)..... 我們的 diff 很聰明吧!還可以比對不同目錄下的相同檔名的內容, 這樣真的很方 便喔~  patch patch 這個指令與 diff 可是有密不可分的關係啊!我們前面提到, diff 可以用來分 辨兩個版本之間的差異,舉例來說,剛剛我們所建立的 /tmp/test/passwd 與 /etc/passwd 就是兩個不同版本之間的檔案。那麼,如果要『升級』呢? 就是『將舊 的檔案升級成為新的檔案』 時,應該要怎麼做呢? 舉例來說,我們可以這樣做測試: [root@linux ~]# mkdir /tmp/old; cp /etc/passwd /tmp/old [root@linux ~]# mkdir /tmp/new; cp /tmp/test/passwd /tmp/new [root@linux ~]# cd /tmp ; diff -Naur old/ new/ > test.patch 此時,在 /tmp/test.patch 檔案之中,就記錄了新舊的檔案之間的差異, 對了!您必 須要瞭解的是,用 diff 製作這個檔案時,舊的檔案必須是在前面,亦即是 diff oldfile newfile 才行喔!此外 ,新舊檔案的 『相對目錄位置』 最好也是一樣比較好喔! OK! 那麼如何將舊的內容 (/tmp/old/passwd) 更新到新版 (/tmp/new/passwd) 的內容呢? 簡單的說,可以用這樣: [root@linux ~]# patch -pN < patch_file 參數: -p :後面可以接『取消幾層目錄』的意思。 範例: 範例一:將剛剛製作出來的 patch file 用來更新舊版資料 [root@linux ~]# cd /tmp/old [root@linux ~]# patch -p1 < /tmp/test.patch
  • 3. patching file passwd # 為什麼這裡會使用 -p1 呢?因為我們在比對新舊版的資料時 是在 /tmp 底下 , , # 而實際的資料是在 /tmp/old 裡面,因此,當我們進入到 /tmp/old 時, # 再查閱 /tmp/test.patch 的第一行如下: # diff -Naur old/passwd new/passwd (用 head -n 1 /tmp/test.patch) # 發現到,我們所在的目錄其實是 old 裡面,所以,就必須要減去一層目錄。 更詳細的 patch 用法我們會在後續的第五章跟大家介紹, 這裡僅是介紹給您,呵 呵!我們可以利用 diff 來比對兩個檔案之間的差異, 更可進一步利用這個功能來 製作修補檔案 (patch file) ,讓大家更容易進行比對與升級呢! 很不賴吧! ^_^