diff以行爲單位比較不一樣ASCII文件差別,能夠輸出一組指令,用於指導如何更改一個文件使其與第二個文件相同。diff在軟件開發時多用於比較新舊版本代碼,和patch連用能夠將文件間區別作成補丁。linux
參考:Beginner's Guide to Installing from Source Patching一節promise
diff [-bBi] from-file to-filecookie
from-file :源文件
to-file :目標文件app
-b :忽略一行當中,僅有多個空白的差別(例如 "about me" 與 "about me" 視爲相同
-B :忽略空白行的差別。
-i :忽略大小寫的不一樣。ide
diff -Naur passwd.old passwd.new > passwd.patchui
補丁文件通常都以.patch結尾spa
patch -pN < patch_file
.net
patch -R -pN < patch_file
code
選項與參數:blog
-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個開頭。若是這一行變化了,則一個標識字符+開頭
標識字符以下:
---第二個文件行的範圍----
[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- <