linux下diff命令的使用

作爲開發人員,經常會遇到需要比較文件差異的情況,如果可以將文件下載到本地的話,通常可以使用meld這樣的圖形化工具方便地在本地進行文件比較;否則只能在服務器的ssh shell中使用diff了。

Linux的man手冊中詳細列出了diff命令的參數和使用方式。這裏摘出部分常用選項,之後再進行詳細用法說明。

短選項名 長選項名 說明
  --normal 標準輸出(默認)
-q --brief 文件存在差異時僅報告而不輸出具體差異
-s --report-identical-files 文件相同時報告
-c, -C NUM --context[=NUM] 上下文輸出模式,在各差異處輸出NUM(默認3)行上下文
-u, -U NUM --unified[=NUM] 合併輸出模式,在各差異處輸出NUM(默認3)行上下文
-e --ed 輸出ed腳本,指明FIEL1編輯爲FILE2的操作
-n --rcs RCS輸出模式
-y --side-by-side 並排輸出模式,直觀的顯示文本差異
-W NUM --width=NUM 輸出寬度,輸出第行最多NUM(默認130)個字符
  --left-column 僅輸出左列。僅並排輸出模式時有效,行無差異時僅顯示左側文件行
  --suppress-common-lines 忽略無差異行。僅並排輸出模式時有效,不顯示無差異行
-t --expand-tabs 輸出時將tab展開成空格
-T --initial-tab 輸出時行首插入tab鍵
  --tabsize=NUM 設置輸出tab寬度爲NUM(默認8)個空格。需要配合使用-t,--expand-tabs選項
  --suppress-blank-empty 忽略輸出中空行的空白間隔
-l --paginate 分頁輸出
  --ignore-file-name-case 忽略文件名大小寫
  --no-ignore-file-name-case 不忽略文件名大小寫
-i --ignore-case 忽略文件內容大小寫
-E --ignore-tab-expansion 忽略tab縮進差異
-Z --ignore-trailing-space 忽略行尾空白
-b --ignore-space-change 忽略空格數量差異
-w --ignore-all-space 忽略全部空白差異
-B --ignore-blank-lines 忽略空白字符行

如果你仔細查看上表,你會發現,diff有多種輸出結果:標準輸出、上下文輸出、合併輸出、ed腳本輸出、RCS輸出和並排輸出。

現結合實例對這幾種輸出分別說明。假設你本地有兩個文件:

a.txt

vim edit
diff is a tool to compare text files in Unix.
diff tells differences of two files.
usually,tow files diffrent from each other has some same parts.
the diff command helps find out the diffrent parts.
	line.
line below.
line last.
b.txt

diff is a tool to compare text files in Unix.
diff tells differences of two text files.
usually,tow files diffrent from each other has some same parts.
many cvs software use the diff command.
 line.
line below.
line added.
line last.
注意a.txt末尾有一空行。

標準輸出

命令:

diff a.txt b.txt

diff --normal a.txt b.txt
輸出結果

1d0
< vim edit
3c2
< diff tells differences of two files.
---
> diff tells differences of two text files.
5,6c4,5
< the diff command helps find out the diffrent parts.
< 	line.
---
> many cvs software use the diff command.
>  line.
7a7
> line added.
9d8
< 

結果表示了從a.txt到b.txt發生的多處變化。每一處變化以/^\d+[adc]\d+$/行開始,a/d/c表示了變化的形式,d表示從a.txt到b.txt發生了刪除(delete),a表示新增,c代表了變更。具體發行的變化在之後的行中明確表述。這些行以“<”或“>”開頭,分別表示了刪除和新增,c類型的差異中還會出現一行“---”,分隔刪除和新增(也有說法是分隔了兩個文件),並共同表述變更。

上下文輸出

命令

diff -c a.txt b.txt
diff -C 3 a.txt b.txt
diff --context a.txt b.txt
diff --context=3 a.txt b.txt
輸出結果

*** a.txt	2014-06-14 14:38:04.979753227 +0800
--- b.txt	2014-06-14 14:37:16.355754277 +0800
***************
*** 1,9 ****
- vim edit
  diff is a tool to compare text files in Unix.
! diff tells differences of two files.
  usually,tow files diffrent from each other has some same parts.
! the diff command helps find out the diffrent parts.
! 	line.
  line below.
  line last.
- 
--- 1,8 ----
  diff is a tool to compare text files in Unix.
! diff tells differences of two text files.
  usually,tow files diffrent from each other has some same parts.
! many cvs software use the diff command.
!  line.
  line below.
+ line added.
  line last.
輸出結果的前2行反應了文件的基本信息。
*** 
表示源文件(變動前文件)
---
表示目標文件(變動後文件)。第三行的
***************
將文件基本信息與變化內容分隔開。

文件的變化部分分別表述了兩個文件的變化,源文件中

*** 1,9 ****

表示了源文件1到7行發生了變化,接下來是變化的具體情況。變化的行行首有一個標記:!、-、+或空白。如果爲空,表示該行無變化;如果是感嘆號(!),表示該行有改動;如果是減號(-),表示該行被刪除;如果是加號(+),表示該行爲新增。

之後

--- 1,8 ----
  diff is a tool to compare text files in Unix.
! diff tells differences of two text files.
  usually,tow files diffrent from each other has some same parts.
! many cvs software use the diff command.
!  line.
  line below.
+ line added.
  line last.
同理表述了目標文件的變化

合併輸出

命令

diff -u a.txt b.txt

diff -U 3 a.txt b.txt

diff --unified a.txt b.txt

diff --unified=3 a.txt b.txt

輸出結果

--- a.txt	2014-06-14 14:38:04.979753227 +0800
+++ b.txt	2014-06-14 14:37:16.355754277 +0800
@@ -1,9 +1,8 @@
-vim edit
 diff is a tool to compare text files in Unix.
-diff tells differences of two files.
+diff tells differences of two text files.
 usually,tow files diffrent from each other has some same parts.
-the diff command helps find out the diffrent parts.
-	line.
+many cvs software use the diff command.
+ line.
 line below.
+line added.
 line last.
-
類似上下文輸出,前兩行也是文件基本信息。

第三行表述了變化範圍

@@ -1,9 +1,8 @@

源文件(“-”表示)第1行開始之後7行,目標文件(“+”表示)第1行開始之後5行

具體變更部分也有行首的標識,空白無變化,“-”表示刪除行,“+”表示增加行。

版本控制系統git的diff輸出是此輸出格式的變種

ed腳本輸出

命令

diff -e a.txt b.txt

diff --ed a.txt b.txt
輸出結果

9d
7a
line added.
.
5,6c
many cvs software use the diff command.
 line.
.
3c
diff tells differences of two text files.
.
1d
</pre><p>此輸出表述了從源文件到目標文件一步一步的操作,主要供gnu ed程序調用。</p><p>每一步操作由操作範圍類型、操作詳情、結束標識構成。第一行表明變化發生的行號和變化類型。</p><p><pre name="code" class="plain">5,6c

表示5到6行變更

7a

表示第7行新增

1d
表示第1行刪除

操作類型有adc,意義與標準輸出相同

ac操作類型具有操作體,表明操作後行號範圍變化結果。

操作體以“.”作爲結束標識。

RCS輸出

命令

diff -n a.txt b.txt

diff --rcs a.txt b.txt
結果輸出

d1 1
d3 1
a3 1
diff tells differences of two text files.
d5 2
a6 2
many cvs software use the diff command.
 line.
a7 1
line added.
d9 1
此輸出主要供gnu rcs程序調用。操作類型僅ad兩種,操作類型後表述了操作的開始行號和操作行數。操作類型a表示新增,d表示刪除。此輸出與ed不同,每一處變更表述不需要先執行先前的變更,其直接表述了當前變更源文件和目標文件當前處發生的變化。發生內容替換變更時,在同一行處由一條刪除表述與一條新增表述共同表示。a操作頭後是新增的具體內容,行數與a操作頭中表述的操作行數相同。

並排輸出

命令

diff -y a.txt b.txt

diff --side-by-side a.txt b.txt

輸出結果

vim edit						      <
diff is a tool to compare text files in Unix.			diff is a tool to compare text files in Unix.
diff tells differences of two files.			      |	diff tells differences of two text files.
usually,tow files diffrent from each other has some same part	usually,tow files diffrent from each other has some same part
the diff command helps find out the diffrent parts.	      |	many cvs software use the diff command.
	line.						      |	 line.
line below.							line below.
							      >	line added.
line last.							line last.
							      <

輸出結果與shell寬度、輸出寬度選項等有關。此輸出結果是最直觀結果,主要供人員快速查閱使用。因shell顯示寬度限制,且無行號提示,使用不多。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章