溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Linux如何使用diff命令比較文件找出文件之間相同的部分

發(fā)布時間:2021-08-26 16:03:35 來源:億速云 閱讀:583 作者:chen 欄目:系統(tǒng)運(yùn)維

本篇內(nèi)容主要講解“Linux如何使用diff命令比較文件找出文件之間相同的部分”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強(qiáng)。下面就讓小編來帶大家學(xué)習(xí)“Linux如何使用diff命令比較文件找出文件之間相同的部分”吧!

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

  1.命令格式:

  diff[參數(shù)][文件1或目錄1][文件2或目錄2]

  2.命令功能:

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

  3.命令參數(shù):

  - 指定要顯示多少行的文本。此參數(shù)必須與-c或-u參數(shù)一并使用。

  -a或--text  diff預(yù)設(shè)只會逐行比較文本文件。

  -b或--ignore-space-change  不檢查空格字符的不同。

  -B或--ignore-blank-lines  不檢查空白行。

  -c  顯示全部內(nèi)文,并標(biāo)出不同之處。

  -C或--context  與執(zhí)行“-c-”指令相同。

  -d或--minimal  使用不同的演算法,以較小的單位來做比較。

  -D或ifdef  此參數(shù)的輸出格式可用于前置處理器巨集。

  -e或--ed  此參數(shù)的輸出格式可用于ed的script文件。

  -f或-forward-ed  輸出的格式類似ed的script文件,但按照原來文件的順序來顯示不同處。

  -H或--speed-large-files  比較大文件時,可加快速度。

  -l或--ignore-matching-lines  若兩個文件在某幾行有所不同,而這幾行同時都包含了選項中指定的字符或字符串,則不顯示這兩個文件的差異。

  -i或--ignore-case  不檢查大小寫的不同。

  -l或--paginate  將結(jié)果交由pr程序來分頁。

  -n或--rcs  將比較結(jié)果以RCS的格式來顯示。

  -N或--new-file  在比較目錄時,若文件A僅出現(xiàn)在某個目錄中,預(yù)設(shè)會顯示:Only in目錄:文件A若使用-N參數(shù),則diff會將文件A與一個空白的文件比較。

  -p  若比較的文件為C語言的程序碼文件時,顯示差異所在的函數(shù)名稱。

  -P或--unidirectional-new-file  與-N類似,但只有當(dāng)?shù)诙€目錄包含了一個第一個目錄所沒有的文件時,才會將這個文件與空白的文件做比較。

  -q或--brief  僅顯示有無差異,不顯示詳細(xì)的信息。

  -r或--recursive  比較子目錄中的文件。

  -s或--report-identical-files  若沒有發(fā)現(xiàn)任何差異,仍然顯示信息。

  -S或--starting-file  在比較目錄時,從指定的文件開始比較。

  -t或--expand-tabs  在輸出時,將tab字符展開。

  -T或--initial-tab  在每行前面加上tab字符以便對齊。

  -u,-U或--unified=  以合并的方式來顯示文件內(nèi)容的不同。

  -v或--version  顯示版本信息。

  -w或--ignore-all-space  忽略全部的空格字符。

  -W或--width  在使用-y參數(shù)時,指定欄寬。

  -x或--exclude  不比較選項中所指定的文件或目錄。

  -X或--exclude-from  您可以將文件或目錄類型存成文本文件,然后在=中指定此文本文件。

  -y或--side-by-side  以并列的方式顯示文件的異同之處。

  --help  顯示幫助。

  --left-column  在使用-y參數(shù)時,若兩個文件某一行內(nèi)容相同,則僅在左側(cè)的欄位顯示該行內(nèi)容。

  --suppress-common-lines  在使用-y參數(shù)時,僅顯示不同之處。

  4.使用實例:

  實例1:比較兩個文件

  代碼如下:

 ?。踨oot@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行內(nèi)容有所不同;“11,12d10”表示第一個文件比第二個文件多了第11和12行。

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

  代碼如下:

  a - add

  c - change

  d - delete

  實例2:并排格式輸出

  命令:diff log2013.log log2014.log -y -W 50

  輸出:

  代碼如下:

 ?。踨oot@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

  說明:

  “|”表示前后2個文件內(nèi)容有不同

  “《”表示后面文件比前面文件少了1行內(nèi)容

  “》”表示后面文件比前面文件多了1行內(nèi)容

  實例3:上下文輸出格式

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

  輸出:

  代碼如下:

 ?。踨oot@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]# 

  說明:

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

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

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

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

  實例4:統(tǒng)一格式輸出

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

  輸出:

  代碼如下:

  [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

  說明:

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

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

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

  “---”表示變動前的文件,“+++”表示變動后的文件。

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

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

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

  實例5:比較文件夾不同

  命令:diff test3 test6

  輸出:

  代碼如下:

 ?。踨oot@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

 ?。踨oot@localhost test]#

  實例6:比較兩個文件不同,并生產(chǎn)補(bǔ)丁

  命令:diff -ruN log2013.log log2014.log 》patch.log

  輸出:

  代碼如下:

 ?。踨oot@localhost test3]# diff -ruN log2013.log log2014.log 》patch.log

 ?。踨oot@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: 沒有那個文件或目錄

 ?。踨oot@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]#

  實例7:打補(bǔ)丁

  命令: cat log2013.log

  輸出:

  代碼如下:

 ?。踨oot@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

 ?。踨oot@localhost test3]#

 ?。踨oot@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]#

到此,相信大家對“Linux如何使用diff命令比較文件找出文件之間相同的部分”有了更深的了解,不妨來實際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI