每天一個linux命令(36):diff命令

來源:互聯網
上載者:User

每天一個linux命令(36):diff命令 相關連結:每天一個linux命令(1):ls命令http://www.bkjia.com/os/201210/163049.html;每天一個linux命令(2):cd命令http://www.bkjia.com/os/201210/163050.html;每天一個linux命令(3):pwd命令http://www.bkjia.com/os/201210/163462.html;每天一個linux命令(4):mkdir命令http://www.bkjia.com/os/201210/163463.html;每天一個linux命令(5):rm命令http://www.bkjia.com/os/201210/163662.html;每天一個linux命令(6):rmdir命令http://www.bkjia.com/os/201210/164017.html;每天一個linux命令(7):mv命令http://www.bkjia.com/os/201210/164247.html;每天一個linux命令(8):cp命令http://www.bkjia.com/os/201210/164254.html;每天一個linux命令(9):touch命令http://www.bkjia.com/os/201211/165699.html;每天一個linux命令(10):cat命令http://www.bkjia.com/os/201211/165989.html;每天一個linux命令(11):nl命令http://www.bkjia.com/os/201211/165990.html每天一個linux命令(12):more命令http://www.bkjia.com/os/201211/165994.html每天一個linux命令(13):less命令http://www.bkjia.com/os/201211/165998.html每天一個linux命令(14):head命令http://www.bkjia.com/os/201211/166191.html每天一個linux命令(15):tail命令http://www.bkjia.com/os/201211/168702.html每天一個linux命令(16):which命令http://www.bkjia.com/os/201211/168890.html每天一個linux命令(17):whereis命令http://www.bkjia.com/os/201211/168893.html每天一個linux命令(18):locate命令http://www.bkjia.com/os/201211/168895.html每天一個linux命令(19):find命令概覽http://www.bkjia.com/os/201211/168897.html每天一個linux命令(20):find命令之exechttp://www.bkjia.com/os/201211/168901.html每天一個linux命令(21):find命令之xargshttp://www.bkjia.com/os/201211/168903.html每天一個linux命令(22):find命令的參數詳解http://www.bkjia.com/os/201211/168912.html每天一個linux命令(23):Linux目錄結構http://www.bkjia.com/os/201211/170430.html每天一個linux命令(24):Linux檔案類型與副檔名http://www.bkjia.com/os/201211/170431.html每天一個linux命令(25):linux檔案屬性詳解http://www.bkjia.com/os/201211/170434.html每天一個linux命令(26):用SecureCRT來上傳和下載檔案http://www.bkjia.com/os/201211/172022.html每天一個linux命令(27):linux chmod命令http://www.bkjia.com/os/201211/172028.html每天一個linux命令(28):tar命令http://www.bkjia.com/os/201212/172641.html每天一個linux命令(29):chgrp命令http://www.bkjia.com/os/201212/172983.html每天一個linux命令(30):chown命令http://www.bkjia.com/os/201212/173239.html每天一個linux命令(31):/etc/group檔案詳解http://www.bkjia.com/os/201212/174429.html每天一個linux命令(32):gzip命令http://www.bkjia.com/os/201212/174431.html每天一個linux命令(33):df命令http://www.bkjia.com/os/201212/174434.html每天一個linux命令(34):du命令http://www.bkjia.com/os/201212/174701.html每天一個linux命令(35):ln命令http://www.bkjia.com/os/201212/174993.html diff 命令是 linux上非常重要的工具,用於比較檔案的內容,特別是比較兩個版本不同的檔案以找到改動的地方。diff在命令列中列印每一個行的改動。最新版本的diff還支援二進位檔案。diff程式的輸出被稱為補丁 (patch),因為Linux系統中還有一個patch程式,可以根據diff的輸出將a.c的檔案內容更新為b.c。diff是svn、cvs、git等版本控制工具不可或缺的一部分。  www.2cto.com  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  不檢查大小寫不同。  www.2cto.com  -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.使用執行個體:執行個體1:比較兩個檔案命令:輸出:[root@localhost test3]# diff log2014.log log2013.log 3c3< 2014-03---> 2013-038c8< 2013-07---> 2013-0811,12d10< 2013-11< 2013-12說明:上面的“3c3”和“8c8”表示log2014.log和log20143log檔案在3行和第8行內容有所不同;"11,12d10"表示第一個檔案比第二個檔案多了第11和12行。diff 的normal 顯示格式有三種提示:a - addc - changed - delete 執行個體2:並排格式輸出命令:diff log2013.log log2014.log  -y -W 50輸出:[root@localhost test3]# diff log2014.log log2013.log  -y -W 502013-01                 2013-012013-02                 2013-022014-03               | 2013-032013-04                 2013-042013-05                 2013-052013-06                 2013-062013-07                 2013-072013-07               | 2013-082013-09                 2013-092013-10                 2013-102013-11               <2013-12               <[root@localhost test3]# diff log2013.log log2014.log  -y -W 502013-01                 2013-012013-02                 2013-022013-03               | 2014-032013-04                 2013-042013-05                 2013-052013-06                 2013-062013-07                 2013-072013-08               | 2013-072013-09                 2013-092013-10                 2013-10                      > 2013-11                      > 2013-12說明:“|”表示前後2個檔案內容有不同“<”表示後面檔案比前面檔案少了1行內容“>”表示後面檔案比前面檔案多了1行內容執行個體3:上下文輸出格式命令: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]#說明:  www.2cto.com  這種方式在開頭兩行作了比較檔案的說明,這裡有三中特殊字元:“+” 比較的檔案的後者比前著多一行“-” 比較的檔案的後者比前著少一行“!” 比較的檔案兩者有差別的行執行個體4:統一格式輸出命令: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"---"表示變動前的檔案,"+++"表示變動後的檔案。第二部分,變動的位置用兩個@作為起首和結束。   @@ -1,12 +1,10 @@前面的"-1,12"分成三個部分:減號表示第一個檔案(即log2014.log),"1"表示第1行,"12"表示連續12行。合在一起,就表示下面是第一個檔案從第1行開始的連續12行。同樣的,"+1,10"表示變動後,成為第二個檔案從第1行開始的連續10行。執行個體5:比較檔案夾不同命令:diff  test3 test6輸出:[root@localhost test]# diff test3 test6Only in test6: linklog.logOnly in test6: log2012.logdiff test3/log2013.log test6/log2013.log1,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=truediff test3/log2014.log test6/log2014.log1,12d0< 2013-01< 2013-02< 2014-03< 2013-04< 2013-05< 2013-06< 2013-07< 2013-07< 2013-09< 2013-10< 2013-11< 2013-12Only in test6: log2015.logOnly in test6: log2016.logOnly 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.logcat: 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]#說明:執行個體7:打補丁命令:輸出:[root@localhost test3]# cat log2013.log2013-012013-022013-032013-042013-052013-062013-072013-082013-092013-10[root@localhost test3]# patch log2013.log patch.log patching file log2013.log[root@localhost test3]# [root@localhost test3]# cat log2013.log 2013-012013-022014-032013-042013-052013-062013-072013-072013-092013-102013-112013-12[root@localhost test3]#
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.