歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux綜合 >> Linux命令 >> Linux常用命令(二十八)-diff

Linux常用命令(二十八)-diff

日期:2017/2/28 9:58:40   编辑:Linux命令

diff 命令是 linux上非常重要的工具,用於比較文件的內容,特別是比較兩個版本不同的文件以找到改動的地方。diff在命令行中打印每一個行的改動。最新版本的diff還支持二進制文件。diff程序的輸出被稱為補丁 (patch),因為Linux系統中還有一個patch程序,可以根據diff的輸出將a.c的文件內容更新為b.c。diff是svn、cvs、git等版本控制工具不可或缺的一部分。

1.命令格式:

diff[參數][文件1或目錄1][文件2或目錄2]

2.命令功能:

diff命令能比較單個文件或者目錄內容。如果指定比較的是文件,則只有當輸入為文本文件時才有效。以逐行的方式,比較文本文件的異同處。如果指定比較的是目錄的的時候,diff 命令會比較兩個目錄下名字相同的文本文件。列出不同的二進制文件、公共子目錄和只在一個目錄出現的文件。

3.命令參數:

-  指定要顯示多少行的文本。此參數必須與-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參數時,僅顯示不同之處。

4.命令實例:

實例一:比較兩個文件

命令: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-11
< 2013-12
上面的“3c3”和“8c8”表示log2014.log和log20143log文件在3行和第8行內容有所不同;"11,12d10"表示第一個文件比第二個文件多了第11和12行。

diff 的normal 顯示格式有三種提示:

a - add

c - change

d - delete

實例二:並排格式輸出

命令: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-10                 2013-10
2013-11               <
2013-12               <
[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-10                 2013-10
                      > 2013-11
                      > 2013-12

實例三:上下文輸出格式

命令:diff log2013.log log2014.log -c

說明:

這種方式在開頭兩行作了比較文件的說明,這裡有三中特殊字符:

“+” 比較的文件的後者比前著多一行

“-” 比較的文件的後者比前著少一行

“!” 比較的文件兩者有差別的行

[root@localhost test3]# diff log2013.log log2014.log  -c
*** log2013.log 2012-12-07 16:36:26.000000000 +0800
--- log2014.log 2012-12-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-10
--- 1,12 ----
  2013-01
  2013-02
! 2014-03
  2013-04
  2013-05
  2013-06
  2013-07
! 2013-07
  2013-09
  2013-10
+ 2013-11
+ 2013-12[root@localhost test3]# diff log2014.log log2013.log  -c
*** log2014.log 2012-12-07 18:01:54.000000000 +0800
--- log2013.log 2012-12-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-10
- 2013-11
- 2013-12
--- 1,10 ----
  2013-01
  2013-02
! 2013-03
  2013-04
  2013-05
  2013-06
  2013-07
! 2013-08
  2013-09
  2013-10[root@localhost test3]#

實例四:統一格式輸出

命令:diff log2014.log log2013.log -u

說明:

它的第一部分,也是文件的基本信息:

--- log2014.log 2012-12-07 18:01:54.000000000 +0800

+++ log2013.log 2012-12-07 16:36:26.000000000 +0800

"---"表示變動前的文件,"+++"表示變動後的文件。

第二部分,變動的位置用兩個@作為起首和結束。

@@ -1,12 +1,10 @@

前面的"-1,12"分成三個部分:減號表示第一個文件(即log2014.log),"1"表示第1行,"12"表示連續12行。合在一起,就表示下面是第一個文件從第1行開始的連續12行。同樣的,"+1,10"表示變動後,成為第二個文件從第1行開始的連續10行。

[root@localhost test3]# diff log2014.log log2013.log  -u
--- log2014.log 2012-12-07 18:01:54.000000000 +0800
+++ log2013.log 2012-12-07 16:36:26.000000000 +0800
@@ -1,12 +1,10 @@
 2013-01
 2013-02
-2014-03
+2013-03
 2013-04
 2013-05
 2013-06
 2013-07
-2013-07
+2013-08
 2013-09
 2013-10
-2013-11
-2013-12

實例五:比較文件夾不同命令:

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-10
---
> 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-10
< 2013-11
< 2013-12
Only in test6: log2015.log
Only in test6: log2016.log
Only in test6: log2017.log
[root@localhost test]# 

實例六:比較兩個文件不同,並生產補丁

命令: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-12-07 16:36:26.000000000 +0800
+++ log2014.log 2012-12-07 18:01:54.000000000 +0800
@@ -1,10 +1,12 @@
 2013-01
 2013-02
-2013-03
+2014-03
 2013-04
 2013-05
 2013-06
 2013-07
-2013-08
+2013-07
 2013-09
 2013-10
+2013-11
+2013-12[root@localhost test3]#

實例七:打補丁

命令: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-10[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-10
2013-11
2013-12[root@localhost test3]#

Copyright © Linux教程網 All Rights Reserved