diff命令詳解

來源:互聯網
上載者:User

標籤:linux命令

diff命令參數:
diff - 找出兩個檔案的不同點
總覽
diff [選項] 源檔案 目標檔案
下面是 GNU所接受的 diff 的所有選項的概要. 大多數的選項有兩個相同的名字,一個是單個的跟在 - 後面字母, 另一個是由 – 引出的長名字. 多個單字母選項(除非它們產生歧義)能夠組合為單行的命令列文法 -ac 是等同於 -a -c. 長命名的選項能被縮短到他們的名字的任何唯一的首碼. 用 ([ 和 ]) 括起來顯示選項產生歧義的選項
-行數(一個整數)
顯示上下文 行數 (一個整數). 這個選項自身沒有指定輸出格式,這是沒有效果的,除非和 -c 或者 -u 組合使用. 這是已廢置的選項,對於正確的操作, 上下文至少要有兩行。
-a
所有的檔案都視為文字檔來逐行比較,甚至他們似乎不是文字檔.
-b
忽略空格引起的變化.
-B
忽略插入刪除空行引起的變化.
–brief
僅報告檔案是否相異,在乎差別的細節.
-c
使用上下文輸出格式.
-C 行數(一個整數)

–context[=lines]
使用上下文輸出格式,顯示以指定 行數 (一個整數), 或者是三行(當 行數 沒有給出時. 對於正確的操作, 上下文至少要有兩行.
–changed-group-format=format
使用 format 輸出一組包含兩個檔案的不同處的行,其格式是 if-then-else .
-d
改變演算法也許發現變化的一個更小的集合.這會使 diff 變慢 (有時更慢).
-D name
合并 if-then-else 格式輸出, 預先處理宏(由name參數提供)條件.
-e

–ed
輸出為一個有效 ed 指令碼.
–exclude=pattern
比較目錄的時候,忽略和目錄中與 pattern(樣式) 相配的.
–exclude-from=file
比較目錄的時候,忽略和目錄中與任何包含在 file(檔案) 的樣式相配的檔案和目錄.
–expand-tabs
在輸出時擴充tab為空白格,保護輸入檔案的tab對齊
-f
產生一個很象 ed 指令碼的輸出,但是但是在他們在檔案出現的順序有改變
-F regexp
在上下文和統一格式中,對於每一大塊的不同,顯示出匹配 regexp. 的一些前面的行.
–forward-ed
產生象 ed 指令碼的輸出,但是它們在檔案出現的順序有改變。
-h
這選項現在已沒作用,它呈現Unix的相容性.
-H
使用啟發規則加速操作那些有許多離散的小差異的大檔案.
–horizon-lines=lines
比較給定行數的有共同首碼的最後行,和有共同或綴的最前行.
-i
忽略大小寫.
-I regexp
忽略由插入,刪除行(由regexp 參數提供參考)帶來的改變.
–ifdef=name
合并 if-then-else 格式輸出, 預先處理宏(由name參數提供)條件.
–ignore-all-space
在比較行的時候忽略空白.
–ignore-blank-lines
忽略插入和刪除空行
–ignore-case
忽略大小寫.
–ignore-matching-lines=regexp
忽略插入刪除行(由regexp 參數提供參考).
–ignore-space-change
忽略空白的數量.
–initial-tab
在文本行(無論是常規的或者格式化的前後文關係)前輸出tab代替空格. 引起的原因是tab對齊看上去象是常規的一樣.
-l
產生通過 pr 編碼的輸出.
-L label

–label=label
使用 label 給出的字元在檔案頭代替檔案名稱輸出.
–left-column
以並列方式印出兩公用行的左邊
–line-format=format
使用 format 輸出所有的行,在 if-then-else 格式中.
–minimal
改變演算法也許發現變化的一個更小的集合.這會使 diff 變慢 (有時更慢).
-n
輸出 RC-格式 diffs; 除了每條指令指定的行數受影響外 象 -f 一樣。
-N

–new-file
在目錄比較中,如果那個檔案只在其中的一個目錄中找到,那麼它被視為在另一個目錄中是一個空檔案.
–new-group-format=format
使用 format 以if-then-else 格式輸出只在第二個檔案中取出的一個行組
–new-line-format=format
使用 format 以if-then-else 格式輸出只在第二個檔案中取出的一行
–old-group-format=format
使用 format 以if-then-else 格式輸出只在第一個檔案中取出的一個行組
–old-line-format=format
使用 format 使用 format 以if-then-else 格式輸出只在第一個檔案中取出的一行
-p
顯示帶有c函數的改變.
-P
在目錄比較中,如果那個檔案只在其中的一個目錄中找到,那麼它被視為在另一個目錄中是一個空檔案.
–paginate
產生通過 pr 編碼的輸出.
-q
僅報告檔案是否相異,不報告詳細的差異.
-r
當比較目錄時,遞迴比較任何找到的子目錄.
–rcs
輸出 RC-格式 diffs; 除了每條指令指定的行數受影響外 象 -f 一樣。
–recursive
當比較目錄時,遞迴比較任何找到的子目錄.
–report-identical-files

-s
報告兩個檔案相同.
-S file
當比較目錄時,由 file 開始. 這用於繼續中斷了的比較.
–sdiff-merge-assist
列印附加的資訊去協助 sdiff. sdiff 在運行 diff 時使用這些選項. 這些選項不是特意為使用者直接使用而準備的。
–show-c-function
顯示帶有c函數的改變.
–show-function-line=regexp
在上下文和統一的格式,對於每一大塊的差別,顯示出匹配 regexp. 的一些前面的行
–side-by-side
使用並列的輸出格式.
–speed-large-files
使用啟發規則加速操作那些有許多離散的小差異的大檔案.
–starting-file=file
當比較目錄時,由 file 開始. 這用於繼續中斷了的比較.
–suppress-common-lines
在並列格式中不印出公用行。
-t
在輸出時擴充tab為空白格,保護輸入檔案的tab對齊
-T
在文本行(無論是常規的或者格式化的前後文關係)前輸出tab代替空格.引起的原因是tab對齊看上去象是常規的一樣.
–text
所有的檔案都視為文字檔來逐行比較,甚至他們似乎不是文字檔.
-u
使用統一的輸出格式.
–unchanged-group-format=format
使用 format 輸出兩個檔案的公用行組,其格式是if-then-else.
–unchanged-line-format=format
使用 format 輸出兩個檔案的公用行,其格式是if-then-else.
–unidirectional-new-file
在目錄比較中,如果那個檔案只在其中的一個目錄中找到,那麼它被視為在另一個目錄中是一個空檔案.
-U lines

–unified[=lines]
使用前後關係格式輸出,顯示以指定 行數 (一個整數), 或者是三行(當 行數 沒有給出時. 對於正確的操作, 上下文至少要有兩行.
-v

–version
輸出 diff 版本號碼.
-w
在比較行時忽略空格
-W columns

–width=columns
在並列格式輸出時,使用指定的列寬.
-x pattern
比較目錄的時候,忽略和目錄中與 pattern(樣式) 相配的.
-X file
比較目錄的時候,忽略和目錄中與任何包含在 file(檔案) 的樣式相配的檔案和目錄.
-y
使用並列格式輸出

執行個體:

有這樣兩個檔案:
程式清單1 :hello.c

include

int main(void)
{
char msg[] = “Hello world!”;

puts(msg);printf("Welcome to use diff commond.\n");return 0;    

}
程式清單2:hello_diff.c

includeinclude

int main(void)
{
char msg[] = “Hello world,fome hello_diff.c”;

puts(msg);printf("hello_diff.c says,‘Here you are,using diff.‘\n");return 0;    

}
我們使用diff命令來查看這兩個檔案的不同之處,有一下幾種方便的方法:
1、普通格式輸出:
[[email protected] diff]# diff hello.c hello_diff.c
1a2

include

5c6
char msg[] = “Hello world,fome hello_diff.c”;
8c9
printf(“hello_diff.c says,’Here you are,using diff.’\n”);
[[email protected] diff]#
上面的“1a2”表示後面的一個檔案”hello_diff.c”比前面的一個檔案”hello.c”多了一行
“5c6”表示第一個檔案的第5行與第二個檔案的第6行有區別
2、並排格式輸出
[[email protected] diff]# diff hello.c hello_diff.c -y -W 130

include #include
                                                          > #include 

int main(void) int main(void)
{ {
char msg[] = “Hello world!”; | char msg[] = “Hello world,fome hello_diff.c”;
puts(msg); puts(msg);
printf(“Welcome to use diff commond.\n”); | printf(“hello_diff.c says,’Here you are,using diff.’\
return 0; return 0;
} }
[[email protected] diff]#
這種並排格式的對比一目瞭然,可以快速找到不同的地方。
-W選擇可以指定輸出資料行的寬度,這裡指定輸出資料行寬為130
3、上下文輸出格式
[[email protected] diff]# diff hello.c hello_diff.c -c
* hello.c 2007-09-25 17:54:51.000000000 +0800
— hello_diff.c 2007-09-25 17:56:00.000000000 +0800

* 1,11 **
#include

int main(void)
{
! char msg[] = “Hello world!”;

    puts(msg);

! printf(“Welcome to use diff commond.\n”);

    return 0;

}
— 1,12 —-
#include
+ #include

int main(void)
{
! char msg[] = “Hello world,fome hello_diff.c”;

    puts(msg);

! printf(“hello_diff.c says,’Here you are,using diff.’\n”);

    return 0;

}
[[email protected] diff]#
這種方式在開頭兩行作了比較檔案的說明,這裡有三中特殊字元:
+ 比較的檔案的後者比前著多一行
- 比較的檔案的後者比前著少一行
! 比較的檔案兩者有差別的行
4、統一輸出格式
[[email protected] diff]# diff hello.c hello_diff.c -u
— hello.c 2007-09-25 17:54:51.000000000 +0800
+++ hello_diff.c 2007-09-25 17:56:00.000000000 +0800
@@ -1,11 +1,12 @@

include

+#include

int main(void)
{
- char msg[] = “Hello world!”;
+ char msg[] = “Hello world,fome hello_diff.c”;

    puts(msg);

- printf(“Welcome to use diff commond.\n”);
+ printf(“hello_diff.c says,’Here you are,using diff.’\n”);

    return 0;

}
[[email protected] diff]#
正如看到的那樣,統一格式的輸出更加緊湊,所以更易於理解,更易於修改。
5、其他
假如你想查看兩個檔案是否不同又不想顯示差異之處的話,可以加上-q選項:
[[email protected] diff]# diff hello.c hello_diff.c -q
Files hello.c and hello_diff.c differ
[[email protected] diff]# 另外你還可以提供一些匹配規則來忽略某中差別,可以用 -I regexp
[[email protected] diff]# diff hello.c hello_diff.c -c -I include
* hello.c 2007-09-25 17:54:51.000000000 +0800
— hello_diff.c 2007-09-25 17:56:00.000000000 +0800

* 2,11 **

int main(void)
{
! char msg[] = “Hello world!”;

    puts(msg);

! printf(“Welcome to use diff commond.\n”);

    return 0;

}
— 3,12 —-

int main(void)
{
! char msg[] = “Hello world,fome hello_diff.c”;

    puts(msg);

! printf(“hello_diff.c says,’Here you are,using diff.’\n”);

    return 0;

}
[[email protected] diff]#
這裡通過“ -I include”選項來忽略帶有“ include”字樣的行

部分內容來自:http://blog.chinaunix.net/u1/48325/showart_433936.html
http://hi.baidu.com/ilotus_y/blog/item/b94231d6f55a1e2a07088b91.html

著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

diff命令詳解

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.