Linux常用命令(二十八) - diff
來源:程序員人生 發布時間:2014-12-19 08:34:30 閱讀次數:3544次
diff 命令是 linux上非常重要的工具,用于比較文件的內容,特別是比較兩個版本不同的文件以找到改動的地方。diff在命令行中打印每個行的改動。最新版本的diff還支持2進制文件。diff程序的輸出被稱為補釘 (patch),由于Linux系統中還有1個patch程序,可以根據diff的輸出將a.c的文件內容更新為b.c。diff是svn、cvs、git等版本控制工具不可或缺的1部份。
1.命令格式:
diff[參數][文件1或目錄1][文件2或目錄2]
2.命令功能:
diff命令能比較單個文件或目錄內容。如果指定比較的是文件,則只有當輸入為文本文件時才有效。以逐行的方式,比較文本文件的異同處。如果指定比較的是目錄的的時候,diff 命令會比較兩個目錄下名字相同的文本文件。列出不同的2進制文件、公共子目錄和只在1個目錄出現的文件。
3.命令參數:
- 指定要顯示多少行的文本。此參數必須與-c或-u參數1并使用。
-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與1個空白的文件比較。
-p 若比較的文件為C語言的程序碼文件時,顯示差異所在的函數名稱。
-P或--unidirectional-new-file 與-N類似,但只有當第2個目錄包括了1個第1個目錄所沒有的文件時,才會將這個文件與空白的文件做比較。
-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參數時,若兩個文件某1行內容相同,則僅在左邊的欄位顯示該行內容。
--suppress-common-lines 在使用-y參數時,僅顯示不同的地方。
4.命令實例:
實例1:比較兩個文件
命令:diff log2014.log log2013.log
[root@localhost test3]# diff log2014.log log2013.log
3c3
< 2014-03
---
> 2013-03
8c8
< 2013-07
---
> 2013-08
11,12d10
< 2013⑴1
< 2013⑴2
上面的“3c3”和“8c8”表示log2014.log和log20143log文件在3行和第8行內容有所不同;"11,12d10"表示第1個文件比第2個文件多了第11和12行。
diff 的normal 顯示格式有3種提示:
a - add
c - change
d - delete
實例2:并排格式輸出
命令:diff log2013.log log2014.log -y -W 50
說明:
“|”表示前后2個文件內容有不同
“<”表示后面文件比前面文件少了1行內容
“>”表示后面文件比前面文件多了1行內容
[root@localhost 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⑴0 2013⑴0
2013⑴1 <
2013⑴2 <
[root@localhost 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⑴0 2013⑴0
> 2013⑴1
> 2013⑴2
實例3:上下文輸出格式
命令:diff log2013.log log2014.log -c
說明:
這類方式在開頭兩行作了比較文件的說明,這里有3中特殊字符:
“+” 比較的文件的后者比前著多1行
“-” 比較的文件的后者比前著少1行
“!” 比較的文件二者有差別的行
[root@localhost test3]# diff log2013.log log2014.log -c
*** log2013.log 2012⑴2-07 16:36:26.000000000 +0800
--- log2014.log 2012⑴2-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⑴0
--- 1,12 ----
2013-01
2013-02
! 2014-03
2013-04
2013-05
2013-06
2013-07
! 2013-07
2013-09
2013⑴0
+ 2013⑴1
+ 2013⑴2[root@localhost test3]# diff log2014.log log2013.log -c
*** log2014.log 2012⑴2-07 18:01:54.000000000 +0800
--- log2013.log 2012⑴2-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⑴0
- 2013⑴1
- 2013⑴2
--- 1,10 ----
2013-01
2013-02
! 2013-03
2013-04
2013-05
2013-06
2013-07
! 2013-08
2013-09
2013⑴0[root@localhost test3]#
實例4:統1格式輸出
命令:diff log2014.log log2013.log -u
說明:
它的第1部份,也是文件的基本信息:
--- log2014.log 2012⑴2-07 18:01:54.000000000 +0800
+++ log2013.log 2012⑴2-07 16:36:26.000000000 +0800
"---"表示變動前的文件,"+++"表示變動后的文件。
第2部份,變動的位置用兩個@作為起首和結束。
@@ ⑴,12 +1,10 @@
前面的"⑴,12"分成3個部份:減號表示第1個文件(即log2014.log),"1"表示第1行,"12"表示連續12行。合在1起,就表示下面是第1個文件從第1行開始的連續12行。一樣的,"+1,10"表示變動后,成為第2個文件從第1行開始的連續10行。
[root@localhost test3]# diff log2014.log log2013.log -u
--- log2014.log 2012⑴2-07 18:01:54.000000000 +0800
+++ log2013.log 2012⑴2-07 16:36:26.000000000 +0800
@@ ⑴,12 +1,10 @@
2013-01
2013-02
⑵014-03
+2013-03
2013-04
2013-05
2013-06
2013-07
⑵013-07
+2013-08
2013-09
2013⑴0
⑵013⑴1
⑵013⑴2
實例5:比較文件夾不同命令:
diff test3 test6
[root@localhost 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⑴0
---
> 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⑴0
< 2013⑴1
< 2013⑴2
Only in test6: log2015.log
Only in test6: log2016.log
Only in test6: log2017.log
[root@localhost test]#
實例6:比較兩個文件不同,并生產補釘
命令:diff -ruN log2013.log log2014.log >patch.log
[root@localhost test3]# diff -ruN log2013.log log2014.log >patch.log
[root@localhost 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
[root@localhost test3]# cat patc.log
cat: patc.log: 沒有那個文件或目錄
[root@localhost test3]# cat patch.log
--- log2013.log 2012⑴2-07 16:36:26.000000000 +0800
+++ log2014.log 2012⑴2-07 18:01:54.000000000 +0800
@@ ⑴,10 +1,12 @@
2013-01
2013-02
⑵013-03
+2014-03
2013-04
2013-05
2013-06
2013-07
⑵013-08
+2013-07
2013-09
2013⑴0
+2013⑴1
+2013⑴2[root@localhost test3]#
實例7:打補釘
命令:patch log2013.log patch.log
[root@localhost test3]# cat log2013.log
2013-01
2013-02
2013-03
2013-04
2013-05
2013-06
2013-07
2013-08
2013-09
2013⑴0[root@localhost test3]# patch log2013.log patch.log
patching file log2013.log
[root@localhost test3]#
[root@localhost test3]# cat log2013.log
2013-01
2013-02
2014-03
2013-04
2013-05
2013-06
2013-07
2013-07
2013-09
2013⑴0
2013⑴1
2013⑴2[root@localhost test3]#
原文:http://www.cnblogs.com/peida/archive/2012/12/12/2814048.html
生活不易,碼農辛苦
如果您覺得本網站對您的學習有所幫助,可以手機掃描二維碼進行捐贈