六月丁香五月婷婷,丁香五月婷婷网,欧美激情网站,日本护士xxxx,禁止18岁天天操夜夜操,18岁禁止1000免费,国产福利无码一区色费

學(xué)習(xí)啦 > 學(xué)習(xí)電腦 > 操作系統(tǒng) > Linux教程 > linux的diff命令

linux的diff命令

時間: 佳洲1085 分享

linux的diff命令

  diff命令在linux系統(tǒng)下是非常重要的工具,它能用于比較文件的內(nèi)容。下面由學(xué)習(xí)啦小編為大家整理了linux的diff命令的相關(guān)知識,希望大家喜歡!

  Linux的diff命令詳解

  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ù)時,僅顯示不同之處。

  linux的diff命令使用實例

  實例1:比較兩個文件

  命令:

  輸出:

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

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

  a - add

  c - change

  d - delete

  實例2:并排格式輸出

  命令:

  diff log2013.log log2014.log -y -W 50

  輸出:

  [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

  說明:

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

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

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

  實例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]#

  說明:

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

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

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

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

  實例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

  輸出:

  [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]#

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

  命令:

  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]#

  實例7:打補丁

  命令:

  輸出:

  [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]#

3622878