日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

diff 比较两个文件的差异

發(fā)布時(shí)間:2023/12/18 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 diff 比较两个文件的差异 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

功能:比較兩個(gè)文件的差異,并把不同地方的信息顯示出來(lái)。默認(rèn)diff格式的信息。

diff比較兩個(gè)文件或文件集合的差異,并記錄下來(lái),生成一個(gè)diff文件,這也是我們常說(shuō)的補(bǔ)丁文件。也使用patch命令對(duì)相應(yīng)的文件打補(bǔ)丁。differential [?d?f??ren??l]

語(yǔ)法:diff ? [options] ? FILES

FILES的格式:

  • FILE1 FILE2 :源是一個(gè)文件,目標(biāo)也是文件。這兩個(gè)文件必須是文本文件。以逐行的方式,比較文本文件的異同處。
  • DIR1 DIR2 ? :源是一個(gè)目錄,目標(biāo)是目錄。diff 命令會(huì)比較兩個(gè)目錄下名字相同的文本文件,依照字母次序排序,列出不同的二進(jìn)制文件,列出公共子目錄,列出只在一個(gè)目錄出現(xiàn)的文件。
  • FILE DIR ? ? :源是一個(gè)文件,目標(biāo)是目錄。diff命令把源文件與目標(biāo)目錄下的同名文件比較。
  • DIR FILE ? ? :源是一個(gè)目錄,目標(biāo)是文件(不是目錄)。源目錄下所有文件中與目標(biāo)文件同名的文件,將用來(lái)與目標(biāo)文件比較。

FILE可以是“-”,代表由標(biāo)準(zhǔn)輸入設(shè)備讀入的文本。DIR不能是"-"。
被指定的文件不能是標(biāo)準(zhǔn)的輸入。

?

短選項(xiàng)長(zhǎng)選項(xiàng)含義
-i--ignore-case忽略文件內(nèi)容大小寫(xiě)的區(qū)別
?--ignore-file-name-case忽略文件名大小寫(xiě)的區(qū)別
?--no-ignore-file-name-case不忽略文件名大小寫(xiě)的區(qū)別
-E--ignore-tab-expansion忽略由制表符寬度造成的差異
-b--ignore-space-change忽略由空格數(shù)不同造成的差異
-w--ignore-all-space忽略所有空格
-B--ignore-blank-lines忽略任何因空行而造成的差異
-I--ignore-matching-lines=RE如果某行匹配正則表達(dá)式,則忽略由該行造成的差異
-a--text所有文件都以文本方式處理
?--strip-trailing-cr去除輸入內(nèi)容每行末端的 carriage return 字符
-c 或 或 -C 行數(shù)--context[=行數(shù)]顯示指定<行數(shù)>(默認(rèn) 3 行)copied 格式的上下文
-u? 或 -U 行數(shù)--unified[=行數(shù)]顯示指定<行數(shù)>(默認(rèn) 3 行)unified 格式的上下文(合并的方式)
?--label 標(biāo)識(shí)使用<標(biāo)識(shí)>代替文件名稱(chēng)
-p--show-c-function顯示和每個(gè)差異有關(guān)的 C 函數(shù)名稱(chēng)
-F?--show-function-line=RE顯示最接近而符合<正則表示式>的一行
-q--brief只顯示文件是否不同
-e--ed以 ed script 方式輸出
?--normal以正常的 diff 方式輸出
-n--rcs以 RCS diff 格式輸出
-y--side-by-side以?xún)闪胁⑴诺姆绞斤@示
-W--width=NUM每行顯示最多 NUM (默認(rèn) 130) 個(gè)字符
?--left-column當(dāng)有兩行相同時(shí)只顯示左邊的一行
?--suppress-common-lines當(dāng)有兩行相同時(shí)不會(huì)顯示
-D--ifdef=NAME輸出的內(nèi)容以‘#ifdef NAME’方式標(biāo)明差異
?--GTYPE-group-format=GFMT效果類(lèi)似,但會(huì)以 GFMT 格式處理 GTYPE 輸入的行
可以是LTYPE的選擇或是‘changed’
?--line-format=LFMT效果類(lèi)似,但會(huì)以 LFMT 格式處理每一行資料
?--LTYPE-line-format=LFMT效果類(lèi)似,但會(huì)以 LFMT 格式處理 LTYPE 輸入的行
LTYPE 可以是‘old’、‘new’或‘unchanged’。
-l--paginate將輸出送至‘pr’指令來(lái)分頁(yè)
-t--expand-tabs將輸出中的 tab 轉(zhuǎn)換成空格
-T--initial-tab每行先加上 tab 字符,使 tab 字符可以對(duì)齊
?--tabsize=NUM定位字符 (tab) 的寬度,默認(rèn)為 8 個(gè)空格寬
?--suppress-blank-emptysuppress space or tab before empty output lines
-r--recursive遞歸比較子目錄中的文件
-N--new-file不存在的文件以空文件方式處理
?--unidirectional-new-file若第一文件案不存在,以空文件處理
-s--report-identical-files文件相同則報(bào)告,否則無(wú)任何提示
-x--exclude=PAT排除匹配 PAT 的文件
-X--exclude-from=FILE排除所有匹配在 FILE 中列出的模式的文件
-S--starting-file=FILE當(dāng)比較目錄時(shí),由 FILE 開(kāi)始比較
?--from-file=FILE1將 FILE1 和操作數(shù)中的所有文件/目錄作比較。FILE1 可以是目錄
?--to-file=FILE2將操作數(shù)中的所有文件/目錄和 FILE2 作比較。FILE2 可以是目錄
?--horizon-lines=NUMkeep NUM lines of the common prefix and suffix
-d--minimal盡可能找出最小的差異
?--speed-large-files假設(shè)文件十分大而且其中含有許多微小的差異

diff輸出格式(默認(rèn)):
n1?a?n3,n4?表示在文件1的n1行后面添加n3到n4行
n1,n2?d?n3?表示在n1到n2行之間刪除n3行
n1,n2?c?n3,n4?表示把n1,n2行用n3,n4行替換掉
字母a:表示附加(add)
字符c:表示修改(change)
字符d:表示刪除(delete)
字母前的是源文件,字母后是目標(biāo)文件。Nx表示行號(hào)。?
以”<”打頭的行屬于第一個(gè)文件,以”>”打頭的行屬于第二個(gè)文件。

比較文件

log2014.log內(nèi)容 2013-01 2013-02 2014-03 2013-04 2013-05 2013-06 2013-07 2013-07 2013-09 2013-10 2013-11 2013-12

log2013.log內(nèi)容 2013-01 2013-02 2013-03 2013-04 2013-05 2013-06 2013-07 2013-08 2013-09 2013-10

比較兩個(gè)文件 [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"表示第一個(gè)文件比第二個(gè)文件多了第11和12行。
以”<”打頭的行屬于第一個(gè)文件,以”>”打頭的行屬于第二個(gè)文件。 并排格式輸出 [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 說(shuō)明:
“|”表示前后2個(gè)文件內(nèi)容有不同
“<”表示后面文件比前面文件少了1行內(nèi)容
“>”表示后面文件比前面文件多了1行內(nèi)容
上下文輸出格式 [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]# 這種方式在開(kāi)頭兩行作了比較文件的說(shuō)明,這里有三中特殊字符:
“+” 比較的文件的后者比前著多一行
“-” 比較的文件的后者比前著少一行
“!” 比較的文件兩者有差別的行 統(tǒng)一格式輸出 [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
"---"表示變動(dòng)前的文件,"+++"表示變動(dòng)后的文件。

第二部分,變動(dòng)的位置用兩個(gè)@作為起首和結(jié)束
@@ -1,12 +1,10 @@
前面的"-1,12"分成三個(gè)部分:減號(hào)表示第一個(gè)文件(即log2014.log),"1"表示第1行,"12"表示連續(xù)12行。意思:第一個(gè)文件從第1行開(kāi)始的連續(xù)12行。
同樣的,"+1,10" 表示第二個(gè)文件從第1行開(kāi)始的連續(xù)10行。

?

比較文件夾

[root@localhost test]# diff test3 test6 Only in test6: linklog.log Only in test6: log2012.log [root@localhost test]# 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 [root@localhost test]# 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]#

?

比較兩個(gè)文件不同,并生產(chǎn)補(bǔ)丁

[root@localhost test3]# diff -ruN log2013.log log2014.log >patch.log [root@localhost test3]# ll 總計(jì) 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 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]#

?

打補(bǔ)丁

[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 使用上面產(chǎn)生的patch.log文件打補(bǔ)丁 patching file log2013.log [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]#

?

實(shí)例

實(shí)例:有這樣兩個(gè)文件。程序清單1,程序清單2 程序清單1 :hello.c #include int main(void) { char msg[] = "Hello world!"; puts(msg); printf("Welcome to use diff commond.n"); return 0; } 程序清單2:hello_diff.c #include #include int main(void) { char msg[] = "Hello world,fome hello_diff.c"; puts(msg); printf("hello_diff.c says,'Here you are,using diff.'n"); return 0; }

我們使用diff命令來(lái)查看這兩個(gè)文件的不同之處,有一下幾種方便的方法:


1、普通格式輸出: [root@localhost diff]# diff hello.c hello_diff.c 1a2 > #include 5c6 char msg[] = "Hello world,fome hello_diff.c"; 8c9 printf("hello_diff.c says,'Here you are,using diff.'n"); [root@localhost diff]# 上面的“1a2”表示后面的一個(gè)文件"hello_diff.c"比前面的一個(gè)文件"hello.c"多了一行
"5c6"表示第一個(gè)文件的第5行與第二個(gè)文件的第6行有區(qū)別 2、并排格式輸出 [root@localhost diff]# diff hello.c hello_diff.c -y -W 130 -W選擇可以指定輸出列的寬度,這里指定輸出列寬為130 #include #include > #include int main(void) int main(void) { { char msg[] = "Hello world!"; | char msg[] = "Hello world,fome hello_diff.c"; puts(msg); puts(msg); printf("Welcome to use diff commond.n"); | printf("hello_diff.c says,'Here you are,using diff.' return 0; return 0; } } [root@localhost diff]# 這種并排格式的對(duì)比一目了然,可以快速找到不同的地方。 3、上下文輸出格式 [root@localhost diff]# diff hello.c hello_diff.c -c *** hello.c 2007-09-25 17:54:51.000000000 +0800 --- hello_diff.c 2007-09-25 17:56:00.000000000 +0800 *************** *** 1,11 **** #include int main(void) { ! char msg[] = "Hello world!"; puts(msg); ! printf("Welcome to use diff commond.n"); return 0; } --- 1,12 ---- #include + #include int main(void) { ! char msg[] = "Hello world,fome hello_diff.c"; puts(msg); ! printf("hello_diff.c says,'Here you are,using diff.'n"); return 0; } [root@localhost diff]# 這種方式在開(kāi)頭兩行作了比較文件的說(shuō)明,這里有三中特殊字符:
+ 比較的文件的后者比前著多一行
- 比較的文件的后者比前著少一行
! 比較的文件兩者有差別的行4、統(tǒng)一輸出格式 [root@localhost diff]# diff hello.c hello_diff.c -u --- hello.c 2007-09-25 17:54:51.000000000 +0800 +++ hello_diff.c 2007-09-25 17:56:00.000000000 +0800 @@ -1,11 +1,12 @@ #include +#include int main(void) { - char msg[] = "Hello world!"; + char msg[] = "Hello world,fome hello_diff.c"; puts(msg); - printf("Welcome to use diff commond.n"); + printf("hello_diff.c says,'Here you are,using diff.'n"); return 0; } [root@localhost diff]# 5、其他 [root@localhost diff]# diff hello.c hello_diff.c -q 假如你想查看兩個(gè)文件是否不同又不想顯示差異之處的話,可以加上-q選項(xiàng) Files hello.c and hello_diff.c differ [root@localhost diff]# [root@localhost diff]# diff hello.c hello_diff.c -c -I include 這里通過(guò)“ -I include”選項(xiàng)來(lái)忽略帶有“ include”字樣的行。另外你還可以提供一些匹配規(guī)則來(lái)忽略某中差別,可以用 -I regexp *** hello.c 2007-09-25 17:54:51.000000000 +0800 --- hello_diff.c 2007-09-25 17:56:00.000000000 +0800 *************** *** 2,11 **** int main(void) { ! char msg[] = "Hello world!"; puts(msg); ! printf("Welcome to use diff commond.n"); return 0; } --- 3,12 ---- int main(void) { ! char msg[] = "Hello world,fome hello_diff.c"; puts(msg); ! printf("hello_diff.c says,'Here you are,using diff.'n"); return 0; } [root@localhost diff]#

轉(zhuǎn)載于:https://www.cnblogs.com/duanxz/p/3994787.html

總結(jié)

以上是生活随笔為你收集整理的diff 比较两个文件的差异的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。