歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux技術 >> 每天一個Linux命令(31)diff命令

每天一個Linux命令(31)diff命令

日期:2017/3/3 12:19:18   编辑:Linux技術

diff命令在最簡單的情況下,比較給定的兩個文件的不同。如果使用“-”代替“文件”參數,則要比較的內容將來自標准輸入。diff命令是以逐行的方式,比較文本文件的異同處。如果該命令指定進行目錄的比較,則將會比較該目錄中具有相同文件名的文件,而不會對其子目錄文件進行任何比較操作。[b] (1)用法:[/b]

[b] 用法: diff [選項參數] [文件1或目錄1] [文件2或目錄2][/b] [b] (2)功能:[/b]

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

1) -y --side-by-side            以並列的方式顯示文件的異同之處。 2) -W --width                在使用-y參數時,指定欄寬。

3) -c                    顯示全部內文,並標出不同之處。 4) -u -U --unified             以合並的方式來顯示文件內容的不同。

5) -r --recursive              比較子目錄中的文件 6) -n --rcs                 將比較結果以RCS的格式來顯示。

[b] (4)實例:[/b] 1)[root@localhost Document]# diff t1.txt t2.txt        比較兩個文檔的區別

[root@localhost Document]# cat >t1.txt <<EOF   //第一種新建文檔的方式
> this is a text!
> 
> Name is t1.txt!
> The extra content!
> I am MenAngel!
> EOF
[root@localhost Document]# cat >t2.txt        //第二種新建文檔的方式
this is a text!

Name is t2.txt!
^Z
[2]+  已停止               cat > t2.txt       //按ctrl+z鍵停止
[root@localhost Document]# diff ../Document/t1.txt ../Document/t2.txt    diff後的兩個文件參數可以跟相對路徑也可以跟絕對路徑
3,5c3                                  
< Name is t1.txt!
< The extra content!
< I am MenAngel!
---
> Name is t2.txt!
[root@localhost Document]# diff t1.txt t2.txt
3,5c3
< Name is t1.txt!
< The extra content!
< I am MenAngel!
---
> Name is t2.txt!
第一個3表示兩個文檔第3行不同,第二個5c3表示第一個文檔有5行,而第2個文檔有三行。

2)[root@localhost Document]# diff -y t1.txt t2.txt        以並排顯示比較兩個文檔的區別

[root@localhost Document]# diff -y t1.txt t2.txt
this is a text!                            this is a text!

Name is t1.txt!                              |    Name is t2.txt!
The extra content!                          <
I am MenAngel!                              <

3)[root@localhost Document]# diff -y -W 40 t1.txt t2.txt    在(2)的基礎上自定義顯示的寬度

[root@localhost Document]# diff -y -W 40 t1.txt t2.txt
this is a text!        this is a text!

Name is t1.txt!       |    Name is t2.txt!
The extra conten   <
I am MenAngel!     <

4)[root@localhost Document]# diff -y -W 40 t1.txt t2.txt 或者 t2.txt t1.txt    文檔順序對結果的影響

[root@localhost Document]# diff -y -W 40 t1.txt t2.txt
this is a text!        this is a text!

Name is t1.txt!       |    Name is t2.txt!
The extra conten   <
I am MenAngel!       <
[root@localhost Document]# diff -y -W 40 t2.txt t1.txt
this is a text!        this is a text!

Name is t2.txt!       |    Name is t1.txt!
           >    The extra conten
           >    I am MenAngel!

  說明:  “|”表示前後2個文件內容有不同

  “<”表示後面文件比前面文件少了1行內容  “>”表示後面文件比前面文件多了1行內容

5)[root@localhost Document]# diff -c t1.txt t2.txt            將進行比較的兩個文檔的內容全部顯示出來標明行數,標出不同點

[root@localhost Document]# diff -c t1.txt t2.txt
*** t1.txt    2016-05-27 23:31:25.949100752 -0700
--- t2.txt    2016-05-27 23:31:54.287100555 -0700
***************
*** 1,5 ****           //從1到5行
  this is a text!
  
! Name is t1.txt!     //第3.4.5行不同
! The extra content!  //由於t2.txt沒有第4和第5行,所以第4和第5行表明是比t2.txt多的
! I am MenAngel!
--- 1,3 ----          //從1到3行
  this is a text!
  
! Name is t2.txt!     //第三行不同

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

    (“+” 比較的文件的後者比前著多一行    “-” 比較的文件的後者比前著少一行) //-u參數用的

    [b]“!” 比較的文件兩者有差別的行[/b] 6)[root@localhost Document]# diff -u t1.txt t2.txt        以合並的方式顯示文本的不同      

[root@localhost Document]# diff -u t1.txt t2.txt                        //它的第一部分,也是文件的基本信息
--- t1.txt    2016-05-27 23:31:25.949100752 -0700                       //-號表示第一個文件
+++ t2.txt    2016-05-27 23:31:54.287100555 -0700                       //+號表示第二個文件
@@ -1,5 +1,3 @@
 this is a text!
 
-Name is t1.txt!
-The extra content!
-I am MenAngel!
+Name is t2.txt!                                                  //每個減號對應一個加號,如果沒有對應則表明在另一個文件中沒有此行
7)[root@localhost Document]# diff dir1 dir2            比較兩個目錄

[root@localhost Document]# mkdir dir1 dir2                                 //創建兩個目錄
[root@localhost Document]# cd dir1
[root@localhost dir1]# cat >text1 <<EOF                                    //在dir1中創建text1,text2在dir2中創建text1,text3
> dir:      dir1
> name:     text1
> 
> Total 4!
> EOF
[root@localhost dir1]# cat >text2 <<EOF
> I am MenAngel!
> I am studying the order of Linux!
> EOF
[root@localhost dir1]# cd ../dir2
[root@localhost dir2]# cat >text1 <<EOF
> dir:      dir2
> name:     text1
> 
> 
> Total 5!
> EOF
[root@localhost dir2]# cat >text3 <<EOF
> Working hard makes success!
> I am MenAngel!
> EOF
[root@localhost dir2]# cd ../
[root@localhost Document]# diff dir1 dir2
只在 dir2 存在:text3
diff dir1/text1 dir2/text1       //遇到同名文件自動比較
1c1
< dir:      dir1
---
> dir:      dir2
4c4,5
< Total 4!
---
> 
> Total 5!
只在 dir1 存在:text2
8)[root@localhost Document]# diff dir1 dir2 >dir.log      產生補丁,其實就是把原本要輸出到標准輸出的內容輸出到自定義文件中

[root@localhost Document]# diff dir1 dir2 >dir.log
[root@localhost Document]# cat dir.log
只在 dir2 存在:text
diff dir1/text1 dir2/text1
1c1
< dir:      dir1
---
> dir:      dir2
4c4,5
< Total 4!
---
> 
> Total 5!
只在 dir1 存在:text2
9)[root@localhost Document]# diff t1.txt t2.txt>t12.log        產生補丁,並用此補丁更新文件

[root@localhost Document]# cat t1.txt t2.txt
this is a text!

Name is t1.txt!
The extra content!
I am MenAngel!      //到這是t1.txt的內容
this is a text!

Name is t2.txt!    //到這是t2.txt的內容
[root@localhost Document]# diff t1.txt t2.txt>t12.log       //產生補丁
[root@localhost Document]# patch t1.txt t12.log             //給t1.txt打補丁,讓它的內容變為t2.txt的內容
patching file t1.txt
[root@localhost Document]# cat t1.txt
this is a text!

Name is t2.txt!
[root@localhost Document]# patch t2.txt t12.log             //給t2.txt打補丁,不過好像有點差別
patching file t2.txt
Reversed (or previously applied) patch detected!  Assume -R? 
 y    //還不知道是為什麼?
[root@localhost Document]# cat t2.txt
this is a text!

Name is t1.txt!
The extra content!
I am MenAngel!
[b] (5)其他:[/b]

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

Copyright © Linux教程網 All Rights Reserved