Linux命令——diff、patch


簡介

diff以行為單位比較不同ASCII文件差異,可以輸出一組指令,用於指導如何更改一個文件使其與第二個文件相同。diff在軟件開發時多用於比較新舊版本代碼,和patch連用可以將文件間區別做成補丁。

參考:Beginner's Guide to Installing from Source Patching一節

diff

用法

diff [-bBi] from-file to-file

選項與參數

from-file :源文件
to-file :目標文件

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

patch

制作補丁

diff -Naur passwd.old passwd.new > passwd.patch

補丁文件一般都以.patch結尾

升級

patch -pN < patch_file

還原

patch -R -pN < patch_file

選項與參數:

-p :后面可以接『取消幾層目錄』的意思。

-R :代表還原,將新的文件還原成原來舊的版本


使用舉例

下面這些內容,基本上是教你如何看diff對比輸出,意義不大,既然有beyondcompare,誰還費這麻煩。

准備實驗數據

[root@localhost ~]# cat file1.txt 
I need to buy apples.
I need to run the laundry.
I need to wash the dog.
I need to get the car detailed.
[root@localhost ~]# cat file2.txt 
I need to buy apples.
I need to do the laundry.
I need to wash the car.
I need to get the dog detailed.

使用diff自動為我們顯示兩個文件之間哪些行不同:

[root@localhost ~]# diff file1.txt file2.txt
2,4c2,4
< I need to run the laundry.
< I need to wash the dog.
< I need to get the car detailed.
---
> I need to do the laundry.
> I need to wash the car.
> I need to get the dog detailed.

我們來看看這個輸出是什么意思。 需要記住的重要一點是,當diff向你描述這些差異時,他的目的是:告訴你如何改變第一個文件使其與第二個文件相匹配。

diff輸出的第一行將包含:

對應於第一個文件的行號,
一個字母(a用於添加,c用於改變,d用於刪除)
對應於第二個文件的行號。

“2,4c2,4”表示:“第一個文件中的第2行到第4行需要更改以匹配第二個文件中的第2行到第4行。” 然后它會告訴我們每個文件中的內容。前面有<的行是第一個文件的行;前面有>的行是第二個文件的行;

"---"僅僅是用於分割file1.txt和file2.txt 這兩個文件

另一個例子

[root@localhost ~]# cat file1.txt 
I need to go to the store.
I need to buy some apples.
When I get home, I'll wash the dog.
[root@localhost ~]# cat file2.txt 
I need to go to the store.
I need to buy some apples.
Oh yeah, I also need to buy grated cheese.
When I get home, I'll wash the dog.
[root@localhost ~]# diff file1.txt file2.txt 
2a3
> Oh yeah, I also need to buy grated cheese.

輸出告訴我們“在第一個文件的第二行之后,需要添加一行:從第二個文件開始的第三行。” 然后它向我們展示了要在第一個文件添加的這一行是什么。

另一個例子

[root@localhost ~]# cat file1.txt 
I need to go to the store.
I need to buy some apples.
When I get home, I'll wash the dog.
I promise.
[root@localhost ~]# cat file2.txt 
I need to go to the store.
I need to buy some apples.
When I get home, I'll wash the dog.
[root@localhost ~]# diff file1.txt file2.txt 
4d3
< I promise.

輸出告訴我們“您需要刪除第一個文件中的第4行,以便兩行文件在第3行同步。” 然后它向我們顯示需要刪除的行的內容。

在上下文中查看差異輸出

上面的例子顯示了diff的默認輸出。 它的目的是被計算機閱讀,而不是人類閱讀,因此對於人類目的而言,有時候可以看到更改的上下文。

GNU diff是大多數Linux用戶將使用的版本,它提供了兩種不同的方法來實現這一點:“上下文模式”和“統一模式”。

要在上下文模式下查看差異,請使用-c選項。 例如,假設file1.txt和file2.txt包含以下內容:

[root@localhost ~]# cat file1.txt 
apples
oranges
kiwis
carrots
[root@localhost ~]# cat file2.txt 
apples
kiwis
carrots
grapefruits
[root@localhost ~]# diff -c file1.txt file2.txt
*** file1.txt    2018-06-15 03:42:16.841974345 -0400
--- file2.txt    2018-06-15 03:42:45.919587320 -0400
***************
*** 1,4 ****
  apples
- oranges
  kiwis
  carrots
--- 1,4 ----
  apples
  kiwis
  carrots
+ grapefruits

前兩行:***標識源文件,---標識目標文件,然后又分別跟文件名,修改日期時間

"***************" 僅僅是分隔符方便閱讀

***第一個文件行的范圍****

第一個文件每行的內容。如果這行沒變化,則以2個開頭。如果這一行變化了,則一個標識字符+開頭

標識字符如下:

---第二個文件行的范圍----

Unified Mode

[root@localhost ~]# diff -u file1.txt file2.txt 
--- file1.txt    2018-06-15 03:42:16.841974345 -0400
+++ file2.txt    2018-06-15 03:42:45.919587320 -0400
@@ -1,4 +1,4 @@
 apples
-oranges
 kiwis
 carrots
+grapefruits

這里差異顯示我們一個文本,而不是兩個單獨的文本。

@@ -1,4 +1,4 @@

 -1,4表示第一個文件1-4行,+1,4表示第二個文件1-4行

生成腳本

-e選項通過diff來輸出一個腳本,該腳本可以被編輯程序ed或ex使用,該腳本包含一系列命令。 這些命令是c(change),a(add)和d(delete)的組合。

[root@localhost ~]# cat file1.txt 
Once upon a time, there was a girl named Persephone.
She had black hair.
She loved her mother more than anything.
She liked to sit outside in the sunshine with her cat, Daisy.
She dreamed of being a painter when she grew up.

[root@localhost ~]# cat file2.txt 
Once upon a time, there was a girl named Persephone.
She had red hair.
She loved chocolate chip cookies more than anything.
She liked to sit outside in the sunshine with her cat, Daisy.
She would look up into the clouds and dream of being a world-famous baker.
[root@localhost ~]# diff -e file1.txt file2.txt
5,6c
She would look up into the clouds and dream of being a world-famous baker.
.
2,3c
She had red hair.
She loved chocolate chip cookies more than anything.
.

也可以將差異內容重定向到文件,方便ed使用

[root@localhost ~]# diff -e file1.txt file2.txt > my-ed-script.txt
[root@localhost ~]# cat my-ed-script.txt
5,6c
She would look up into the clouds and dream of being a world-famous baker.
.
2,3c
She had red hair.
She loved chocolate chip cookies more than anything.
.

還差一步,需要在腳本中追加w,表示 編輯寫入實際文件

[root@localhost ~]# echo "w" >> my-ed-script.txt

下面命令就可以修改原始文件

ed - file1.txt < my-ed-script.txt

-指示ed從標准輸入讀取,<將my-ed-script.txt重定向為標准輸入。這條命令執行完后,源文件file1.txt就被修改了。

 

 並排顯示差異

[root@localhost ~]# diff -y file1.txt file2.txt
Once upon a time, there was a girl named Persephone.        Once upon a time, there was a girl named Persephone.
She had black hair.                          |    She had red hair.
She loved her mother more than anything.              |    She loved chocolate chip cookies more than anything.
She liked to sit outside in the sunshine with her cat, Daisy.    She liked to sit outside in the sunshine with her cat, Daisy.
She dreamed of being a painter when she grew up.          |    She would look up into the clouds and dream of being a world-
                                  <

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM