Posted on 2008-11-14 16:12
Prayer 閱讀(569)
評論(0) 編輯 收藏 引用 所屬分類:
Shell
功能說明:比較文件的差異。
語 法:diff [-abBcdefHilnNpPqrstTuvwy][-<行數>][-C <行數>][-D <巨集名稱>][-I <字符或字符串>][-S <文件>][-W <寬度>][-x <文件或目錄>][-X <文件>][--help][--left-column][--suppress-common-line][文件或目錄1][文件或目錄2]
補充說明:diff以逐行的方式,比較文本文件的異同處。所是指定要比較目錄,則diff會比較目錄中相同文件名的文件,但不會比較其中子目錄。
參 數:
-<行數> 指定要顯示多少行的文本。此參數必須與-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參數時,僅顯示不同之處。
===========================================================================
摘要:本文詳細介紹了diff命令的基本用法
作者:zieckey (zieckey@yahoo.com.cn)
All Rights Reserved!
有這樣兩個文件:
程序清單1 :hello.c
#include <stdio.h>
int main(void)
{
char msg[] = "Hello world!";
puts(msg);
printf("Welcome to use diff commond.\n");
return 0;
}
程序清單2:hello_diff.c
#include <stdio.h>
#include <stdlib.h>
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、普通格式輸出:
[root@localhost diff]# diff hello.c hello_diff.c
1a2
> #include <stdlib.h>
5c6
< char msg[] = "Hello world!";
---
> char msg[] = "Hello world,fome hello_diff.c";
8c9
< printf("Welcome to use diff commond.\n");
---
> printf("hello_diff.c says,'Here you are,using diff.'\n");
[root@localhost diff]#
上面的“1a2”表示后面的一個文件"hello_diff.c"比前面的一個文件"hello.c"多了一行
"5c6"表示第一個文件的第5行與第二個文件的第6行有區別
2、并排格式輸出
[root@localhost diff]# diff hello.c hello_diff.c -y -W 130
#include <stdio.h> #include <stdio.h>
> #include <stdlib.h>
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;
} }
[root@localhost diff]#
這種并排格式的對比一目了然,可以快速找到不同的地方。
-W選擇可以指定輸出列的寬度,這里指定輸出列寬為130
3、上下文輸出格式
[root@localhost 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 <stdio.h>
int main(void)
{
! char msg[] = "Hello world!";
puts(msg);
! printf("Welcome to use diff commond.\n");
return 0;
}
--- 1,12 ----
#include <stdio.h>
+ #include <stdlib.h>
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;
}
[root@localhost diff]#
這種方式在開頭兩行作了比較文件的說明,這里有三中特殊字符:
+ 比較的文件的后者比前著多一行
- 比較的文件的后者比前著少一行
! 比較的文件兩者有差別的行
4、統一輸出格式
[root@localhost 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 <stdio.h>
+#include <stdlib.h>
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;
}
[root@localhost diff]#
正如看到的那樣,統一格式的輸出更加緊湊,所以更易于理解,更易于修改。
5、其他
假如你想查看兩個文件是否不同又不想顯示差異之處的話,可以加上-q選項:
[root@localhost diff]# diff hello.c hello_diff.c -q
Files hello.c and hello_diff.c differ
[root@localhost diff]# 另外你還可以提供一些匹配規則來忽略某中差別,可以用 -I regexp
[root@localhost 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;
}
[root@localhost diff]#
這里通過“ -I include”選項來忽略帶有“ include”字樣的行