UNIX - diff 指令
【功能】
比較二個檔案的不同處
【語法】
diff [-bitw] [-c|-e|-f|-h|-n] filename1 filename2
diff [-bitw] [-C number ] filename1 filename2
diff [-bitw] [-D string ] filename1 filename2
diff [-bitw] [-c|-e|-f|-h|-n] [-l] [-r] [-s] [-S name ] directory1 directory2
【說明】
diff 指令可指出要如何修改才能使二個資料檔一致。以 ed 編輯程式的 script 中的增加、刪除或取代方式說明,如:
n1 a n3,n4
n1,n2 d n3
n1,n2 c n3,n4
可使filename1 修改成 filename2。n 是指行數,若 n1 等於 n2 或 n3 等於n4 時可只用 1 個數字表示。 在修改的結果後出現的 '>' 及 '<' 符號,分別代表 filename1 及 filename2 有變動的資料。
若是檔案 filename1 或 filename2 是 '-' 字元,則代表從標準輸入讀取資料。
若檔案 filename1(filename2)是個目錄名稱,則是指該目錄 內取出與 filename2(filename1)檔名相同的檔案。例如:
diff/usr/eric listing
/usr/eric 是個目錄,而 listing 是一個檔案,表示檔案 filename1 是指 /usr/eric/listing,而檔案 filename2 則是現行目錄下的 listing 檔案。
【選項】
選 項 | 說 明 |
-b(blank) | 對每行尾端的空白字元不予比較。 |
-i(ignore) | 將大小寫的字母視為相同。 |
-t(tab) | 輸出時會將跳格字元(tab)擴展成空白字元(space)。 |
-w(whitespace) | 忽略所有的空白字元和跳格字元。 |
下面的選項只能擇一使用: | |
-c | 輸出時會先印出 filename1、filename2 檔案的建立時間。對於 filename1 中被刪除的行會以 '-' 標示;新增到 filename2 的行會以 '+' 標示;有改變的行則是以 '!' 標示。 |
-C number | 同 -c 選項,會產生相異及相同的列表以及number 行的本文。 |
-e(ed) | 產生 ed 指令的 script 命令 a、c 及 d,允許從 filename1 產生 filename2。 |
-f | 以相反的順序,產生類似於 ed 指令上的 script 命令,但無法使用於 ed 指令上。 |
-h(half-heart) | 以快速的方式處理比較,只能處理改變不大的檔案,但可適用於無限大的檔案。此選項不可與 -e、-f 選項同時使用。 |
-n | 同 -e 選項,但會計算插入、刪除的行數。 |
-D string | 以類似 C 語言的前置處理程式的方式產生 filename1、filename2 檔案的綜合版本。若定義了 string 字串則相當於產生filename2,否則產生 filename1。 |
下面的選項是用在比較目錄時: | |
-l(long) | 以長格式輸出。 在使用 diff 指令前,文字檔應以 pr 指令處理。 |
-r(recursive) | 要求 diff 指令以遞迴方式處理在子目錄下名稱相同的檔案,並將二目錄內的檔案名稱做一比較,如那些檔案只存在 filename1 目錄及 filename2 目錄下。 |
-s(same) | 要求在二個檔案內容完全相同時產生訊息。 |
-S name(start) | 從目錄中名稱為 name 的檔案開始比較。name名稱以前的檔案不比較內容。 |
★註:選項 -i、-t、-w、-c、-C、-n、-D、-l、-r、-s、-S是在 SVR4 版本時才開始提供。
【傳回值】
- 0 - 比較的結果相同。
- 1 - 比較的結果不同。
- 2 - 有錯誤產生。
【範例】
1. 比較二個檔案的不同處:
$ cat file1 |
Before we start our tour today of the United states |
Senate,let's review how a bill is introduced to Congress. |
Some specialized vocabulary is used to describe the |
process,but I'll be defining these words as we go along. |
$ cat file2 |
Before we start our tour today of the United states |
Some specialized vocabulary is used to describe the |
process |
$ diff file1 file2 |
2d1 |
< Senate,let's review how a bill is introduced to Congress. |
4c3 |
< process,but I'll be defining these words as we go along. |
--- |
> process |
從以上比較的輸出結果中可看出,'2d1' 表示可將第一個檔案的第 2 行刪除而與第二個檔案相同,'4c3' 表示可將第一個檔案的第 4 行以第二個檔案的第 3 行取代,最後可將 file1 檔案的內容轉換成 file2 檔案。在比較的說明後還有改變的內容,'<' 符號是第一個檔案受影響的行,'>' 符號是第二個檔案受影響的行。
2. 印出較詳細的比較結果:
$ diff -c file1 file2 |
*** file1 Sun Nov 1 19:31:10 1992 |
--- file2 Sun Nov 1 19:31:12 1992 |
*************** |
*** 1,4 **** |
Before we start our tour today of the United states |
- Senate,let's review how a bill is introduced to Congress. |
Some specialized vocabulary is used to describe the |
! process,but I'll be defining these words as we go along. |
--- 1,3 ---- |
Before we start our tour today of the United states |
Some specialized vocabulary is used to describe the |
! process |
上面是增加 -c 選項後的輸出結果,且會印出二個檔案的名稱和建立時間,以及二個檔案的全部內容,並在每個檔案內有差異的行前端加上標示符號。