天天看點

linux指令之diff指令詳解(比較檔案内容)



diff 指令是 linux上非常重要的工具,用于比較檔案的内容,特别是比較兩個版本不同的檔案以找到改動的地方。diff在指令行中列印每一個行的改動。最新版本的diff還支援二進制檔案。diff程式的輸出被稱為更新檔 (patch),因為Linux系統中還有一個patch程式,可以根據diff的輸出将a.c的檔案内容更新為b.c。diff是svn、cvs、git等版本控制工具不可或缺的一部分。

1.指令格式:

diff[參數][檔案1或目錄1][檔案2或目錄2]

2.指令功能:

diff指令能比較單個檔案或者目錄内容。如果指定比較的是檔案,則隻有當輸入為文本檔案時才有效。以逐行的方式,比較文本檔案的異同處。如果指定比較的是目錄的的時候,diff 指令會比較兩個目錄下名字相同的文本檔案。列出不同的二進制檔案、公共子目錄和隻在一個目錄出現的檔案。

3.指令參數:

- 指定要顯示多少行的文本。此參數必須與-c或-u參數一并使用。

-a或--text  diff預設隻會逐行比較文本檔案。

-b或--ignore-space-change  不檢查空格字元的不同。

-B或--ignore-blank-lines  不檢查空白行。

-c  顯示全部内文,并标出不同之處。

-C或--context  與執行"-c-"指令相同。

-d或--minimal  使用不同的演算法,以較小的機關來做比較。

-D或ifdef  此參數的輸出格式可用于前置處理器巨集。

-e或--ed  此參數的輸出格式可用于ed的script檔案。

-f或-forward-ed  輸出的格式類似ed的script檔案,但按照原來檔案的順序來顯示不同處。

-H或--speed-large-files  比較大檔案時,可加快速度。

-l或--ignore-matching-lines  若兩個檔案在某幾行有所不同,而這幾行同時都包含了選項中指定的字元或字元串,則不顯示這兩個檔案的差異。

-i或--ignore-case  不檢查大小寫的不同。

-l或--paginate  将結果交由pr程式來分頁。

-n或--rcs  将比較結果以RCS的格式來顯示。

-N或--new-file  在比較目錄時,若檔案A僅出現在某個目錄中,預設會顯示:Only in目錄:檔案A若使用-N參數,則diff會将檔案A與一個空白的檔案比較。

-p  若比較的檔案為C語言的程式碼檔案時,顯示差異所在的函數名稱。

-P或--unidirectional-new-file  與-N類似,但隻有當第二個目錄包含了一個第一個目錄所沒有的檔案時,才會将這個檔案與空白的檔案做比較。

-q或--brief  僅顯示有無差異,不顯示詳細的資訊。

-r或--recursive  比較子目錄中的檔案。

-s或--report-identical-files  若沒有發現任何差異,仍然顯示資訊。

-S或--starting-file  在比較目錄時,從指定的檔案開始比較。

-t或--expand-tabs  在輸出時,将tab字元展開。

-T或--initial-tab  在每行前面加上tab字元以便對齊。

-u,-U或--unified=  以合并的方式來顯示檔案内容的不同。

-v或--version  顯示版本資訊。

-w或--ignore-all-space  忽略全部的空格字元。

-W或--width  在使用-y參數時,指定欄寬。

-x或--exclude  不比較選項中所指定的檔案或目錄。

-X或--exclude-from  您可以将檔案或目錄類型存成文本檔案,然後在=中指定此文本檔案。

-y或--side-by-side  以并列的方式顯示檔案的異同之處。

--help  顯示幫助。

--left-column  在使用-y參數時,若兩個檔案某一行内容相同,則僅在左側的欄位顯示該行内容。

--suppress-common-lines  在使用-y參數時,僅顯示不同之處。

4.使用執行個體:

執行個體1:比較兩個檔案

複制代碼 代碼如下:

[[email protected] test3]# diff log2014.log log2013.log

3c3

< 2014-03

---

> 2013-03

8c8

< 2013-07

---

> 2013-08

11,12d10

< 2013-11

< 2013-12

說明:上面的“3c3”和“8c8”表示log2014.log和log20143log檔案在3行和第8行内容有所不同;"11,12d10"表示第一個檔案比第二個檔案多了第11和12行。

diff 的normal 顯示格式有三種提示:

複制代碼 代碼如下:

a - add

c - change

d - delete

執行個體2:并排格式輸出

指令:diff log2013.log log2014.log  -y -W 50

輸出:

複制代碼 代碼如下:

[[email protected] test3]# diff log2014.log log2013.log -y -W 50

2013-01 2013-01

2013-02 2013-02

2014-03 | 2013-03

2013-04 2013-04

2013-05 2013-05

2013-06 2013-06

2013-07 2013-07

2013-07 | 2013-08

2013-09 2013-09

2013-10 2013-10

2013-11 <

2013-12 <

[[email protected] test3]# diff log2013.log log2014.log -y -W 50

2013-01 2013-01

2013-02 2013-02

2013-03 | 2014-03

2013-04 2013-04

2013-05 2013-05

2013-06 2013-06

2013-07 2013-07

2013-08 | 2013-07

2013-09 2013-09

2013-10 2013-10

> 2013-11

> 2013-12

說明:

“|”表示前後2個檔案内容有不同

“<”表示後面檔案比前面檔案少了1行内容

“>”表示後面檔案比前面檔案多了1行内容

執行個體3:上下文輸出格式

指令:diff log2013.log log2014.log  -c

輸出:

複制代碼 代碼如下:

[[email protected] test3]# diff log2013.log log2014.log -c

*** log2013.log 2012-12-07 16:36:26.000000000 +0800

--- log2014.log 2012-12-07 18:01:54.000000000 +0800

***************

*** 1,10 ****

2013-01

2013-02

! 2013-03

2013-04

2013-05

2013-06

2013-07

! 2013-08

2013-09

2013-10

--- 1,12 ----

2013-01

2013-02

! 2014-03

2013-04

2013-05

2013-06

2013-07

! 2013-07

2013-09

2013-10

+ 2013-11

+ 2013-12[[email protected] test3]# diff log2014.log log2013.log -c

*** log2014.log 2012-12-07 18:01:54.000000000 +0800

--- log2013.log 2012-12-07 16:36:26.000000000 +0800

***************

*** 1,12 ****

2013-01

2013-02

! 2014-03

2013-04

2013-05

2013-06

2013-07

! 2013-07

2013-09

2013-10

- 2013-11

- 2013-12

--- 1,10 ----

2013-01

2013-02

! 2013-03

2013-04

2013-05

2013-06

2013-07

! 2013-08

2013-09

2013-10[[email protected] test3]#

說明:

這種方式在開頭兩行作了比較檔案的說明,這裡有三中特殊字元:

“+” 比較的檔案的後者比前着多一行

“-” 比較的檔案的後者比前着少一行

“!” 比較的檔案兩者有差别的行

執行個體4:統一格式輸出

指令:diff log2014.log log2013.log  -u

輸出:

複制代碼 代碼如下:

[[email protected] test3]# diff log2014.log log2013.log -u

--- log2014.log 2012-12-07 18:01:54.000000000 +0800

+++ log2013.log 2012-12-07 16:36:26.000000000 +0800

@@ -1,12 +1,10 @@

2013-01

2013-02

-2014-03

+2013-03

2013-04

2013-05

2013-06

2013-07

-2013-07

+2013-08

2013-09

2013-10

-2013-11

-2013-12

說明:

它的第一部分,也是檔案的基本資訊:

--- log2014.log 2012-12-07 18:01:54.000000000 +0800

+++ log2013.log 2012-12-07 16:36:26.000000000 +0800

"---"表示變動前的檔案,"+++"表示變動後的檔案。

第二部分,變動的位置用兩個@作為起首和結束。

   @@ -1,12 +1,10 @@

前面的"-1,12"分成三個部分:減号表示第一個檔案(即log2014.log),"1"表示第1行,"12"表示連續12行。合在一起,就表示下面是第一個檔案從第1行開始的連續12行。同樣的,"+1,10"表示變動後,成為第二個檔案從第1行開始的連續10行。

執行個體5:比較檔案夾不同

指令:diff  test3 test6

輸出:

複制代碼 代碼如下:

[[email protected] test]# diff test3 test6

Only in test6: linklog.log

Only in test6: log2012.log

diff test3/log2013.log test6/log2013.log

1,10c1,3

< 2013-01

< 2013-02

< 2013-03

< 2013-04

< 2013-05

< 2013-06

< 2013-07

< 2013-08

< 2013-09

< 2013-10

---

> hostnamebaidu=baidu.com

> hostnamesina=sina.com

> hostnames=true

diff test3/log2014.log test6/log2014.log

1,12d0

< 2013-01

< 2013-02

< 2014-03

< 2013-04

< 2013-05

< 2013-06

< 2013-07

< 2013-07

< 2013-09

< 2013-10

< 2013-11

< 2013-12

Only in test6: log2015.log

Only in test6: log2016.log

Only in test6: log2017.log

[[email protected] test]#

執行個體6:比較兩個檔案不同,并生産更新檔

指令:diff -ruN log2013.log log2014.log >patch.log

輸出:

複制代碼 代碼如下:

[[email protected] test3]# diff -ruN log2013.log log2014.log >patch.log

[[email protected] test3]# ll

總計 12

-rw-r--r-- 2 root root 80 12-07 16:36 log2013.log

-rw-r--r-- 1 root root 96 12-07 18:01 log2014.log

-rw-r--r-- 1 root root 248 12-07 21:33 patch.log

[[email protected] test3]# cat patc.log

cat: patc.log: 沒有那個檔案或目錄

[[email protected] test3]# cat patch.log

--- log2013.log 2012-12-07 16:36:26.000000000 +0800

+++ log2014.log 2012-12-07 18:01:54.000000000 +0800

@@ -1,10 +1,12 @@

2013-01

2013-02

-2013-03

+2014-03

2013-04

2013-05

2013-06

2013-07

-2013-08

+2013-07

2013-09

2013-10

+2013-11

+2013-12[[email protected] test3]#

執行個體7:打更新檔

指令: cat log2013.log

輸出:

複制代碼 代碼如下:

[[email protected] test3]# cat log2013.log

2013-01

2013-02

2013-03

2013-04

2013-05

2013-06

2013-07

2013-08

2013-09

2013-10[[email protected] test3]# patch log2013.log patch.log

patching file log2013.log

[[email protected] test3]#

[[email protected] test3]# cat log2013.log

2013-01

2013-02

2014-03

2013-04

2013-05

2013-06

2013-07

2013-07

2013-09

2013-10

2013-11

2013-12[[email protected] test3]#

繼續閱讀