diffutils's diff

  比較文件的差別
vim

  diff,用來查看兩個文件的差別之處,或者兩個目錄之中的對應文件。假若比較的不是文本文件,而是二進制文件,只會報告二者不一樣。輸出文本文件的異同時,能夠按照多個格式輸出,根據使用的選項決定。bash

  cmd,能夠比較兩個文件在字節、行數上的差別。less

  diff3,能夠比較三個文件的差別。當兩個副原本自同一個原始文件時,能同時獲取這兩份較之原始文件的差別,並且能保留二者的修改生成一個新的文件、並提出有衝突的警告。ide

  sdiff,交互式合併兩個文件。測試

  可使用diff來發布源碼補丁,能夠考慮使用patch命令來打上這個補丁。diff命令能夠理解爲「減法結果」的生成,patch是把「減法結果」加到文件生成新文件。spa


  忽略掉「製表符」與「空格」的差別,使用選項「--ignore-tailing-apace」(-E)regexp


1.比較方法orm


  對於文件的差別有多種狀況,出於不一樣的目得有選擇地忽略某些狀況。好比因爲空白符數量形成差別,或者存在一個僅僅包含空白符的空白行。又或者某些文件不會由於字母大小寫而存在差別,可是比較時卻認爲存在差別。blog

  默認使用「diff file1 file2」時,顯示差別的行信息。可使用選項「-q」,僅僅顯示異同結果,不一樣時「Files file1 and file2 differ」。遞歸


1.1.差別組

  倆文件的內容在忽略行順序時,是同樣的時候,比較時會由於參考的行不同獲得不一樣的輸出結果。

# file1
i
love
you
# file2
you
love
i

1.2.空白符(忽略)

1.2.1.-E

  --ignore-tab-expansion (-E)

    忽略tab與空格,認爲tab是「多個」空格。(「多個」-與數量無關、與長度有關係,這裏成功測試是7個)

[view@file diffutils]$ diff -q blank blank2
Files blank and blank2 differ
[view@file diffutils]$ diff -q -E blank blank2
[view@file diffutils]$

1.2.2.-Z

  --ignore-trailing-space (-Z)

    忽略行尾的空格(認爲行尾多個空格與沒有空格等效)

[view@file diffutils]$ diff -q blank blank2
Files blank and blank2 differ
[view@file diffutils]$ diff -q -Z blank blank2
[view@file diffutils]$

1.2.3.-b

  --ignore-space-change (-b)

    效果就像上邊兩項的集合,忽略行尾、行中的「空格與多個空格」的差別。這裏就就直接認爲tab與空格是一回事,並且不限制「等長」。

[view@file diffutils]$ diff -q blank blank2
Files blank and blank2 differ
[view@file diffutils]$ diff -q -E blank blank2
Files blank and blank2 differ
[view@file diffutils]$ diff -q -Z blank blank2
Files blank and blank2 differ
[view@file diffutils]$ diff -q -b blank blank2
[view@file diffutils]$

1.2.4.-w

  --ignore-all-space (-w)

    忽略行首存在空白符時的差別(一個有、另外一個沒有)。空白符包括:水平製表符\t、垂直製表符\v、換頁\f、回車\r、空格、以及其餘定義的空白符。

[view@file diffutils]$ diff -q -b blank blank2
Files blank and blank2 differ
[view@file diffutils]$ diff -q -w blank blank2
[view@file diffutils]$


1.3.空白行(忽略)

  --ignore-blank-lines (-B)

    空白行,就是「^?」。當這個空白行位於文件的不一樣「行位置」時,但願忽略這個狀況,須要使用該選項。

[view@file diffutils]$ diff -q blank blank2
Files blank and blank2 differ
[view@file diffutils]$ diff -q -B blank blank2
[view@file diffutils]$


    當空白行是個「^[:blank:]?」,該選項就沒法忽略差別了。

[view@file diffutils]$ diff -q -B blank blank2
Files blank and blank2 differ


1.4.匹配行(忽略)

  --ignore-matching-lines=regexp (-Iregexp)

    好比,寫了一個腳本,在不一樣的環境下分別「#/bin/sh」、「#/bin/bash」爲首行。

[view@file diffutils]$ diff -q blank blank2
Files blank and blank2 differ
[view@file diffutils]$ diff -q blank blank2 -I "^#"
[view@file diffutils]$


1.5.字母大小寫

  -i or --ignore-case

    忽略英文字母大小寫。



2.輸出格式


  例子文件

# file t:
The Nameless is the origin of Heaven and Earth;
The named is the mother of all things.

Therefore let there always be non-being,
  so we may see their subtlety,
And let there always be being,
  so we may see their outcome.
The two are the same,
But after they are produced,
  they have different names.
They both may be called deep and profound.
Deeper and more profound,
The door of all subtleties!

# file l
The Way that can be told of is not the eternal Way;
The name that can be named is not the eternal name.
The Nameless is the origin of Heaven and Earth;
The Named is the mother of all things.
Therefore let there always be non-being,
  so we may see their subtlety,
And let there always be being,
  so we may see their outcome.
The two are the same,
But after they are produced,
  they have different names.


2.1.上下文顯示

  上下文格式顯示

  把上下文顯示的格式保存爲c,並在vim中同時打開來對比效果。

選項:--context[=lines] (-Clines) or -c
默認:3行
輸出上下文顯示格式到文件
[view@file diffutils]$ diff -c l t > c
在vim左側打開上下部分分別打開l、t;右側打開c
[view@file diffutils]$ vi -o l t
:vsplit c
Ctrl+W L
Ctrl+w w
12 Ctrl+w Ctrl+_
Ctrl+w w
Ctrl+w w
28 Ctrl+w Ctrl+_

  下邊以純本本格式顯示打開後的效果(單擊圖片可放大):

wKiom1mK0ROQ9PlgAADbsrgbVNQ405.png


  第一行,前三列定義文件名稱表示符號,以深綠色顯示,分割表明文件名對應的文件內容。深×××星號分割對比的區域。區域內,白色文字屬於「上下文」。歎號開頭的紫紅色,表示差別部分。加號開頭淺藍色表示多出的差別部分。(表明文件名的星號第8行、與短橫槓的第7行相同,不在c文件中顯示。)


  百尺杆頭,更近一步,把上下文設定爲1行:

[view@file diffutils]$ diff -C 1 l t > c1
[view@file diffutils]$ vi -o l t

wKioL1mK272wbOMLAADKJ6mfLhg815.png


2.2.統一顯示格式

  使用選項:--unified[=lines] (-Ulines), or -u;默認3行。

[view@file diffutils]$ diff -u l t
--- l    2017-08-09 15:38:18.861252990 +0800
+++ t    2017-08-09 15:38:30.759311361 +0800
@@ -1,7 +1,6 @@
-The Way that can be told of is not the eternal Way;
-The name that can be named is not the eternal name.
-The Nameless is the origin of Heaven and Earth;
-The Named is the mother of all things.
+meless is the origin of Heaven and Earth;
+The named is the mother of all things.
+
 Therefore let there always be non-being,
   so we may see their subtlety,
 And let there always be being,
@@ -9,3 +8,6 @@
 The two are the same,
 But after they are produced,
   they have different names.
+They both may be called deep and profound.
+Deeper and more profound,
+The door of all subtleties!

  前兩行定義了怎麼理解加、鍵號。-:l文件。+:t文件。縮進:相同內容。@開頭,內容彙總信息,加減號於前邊兩行對應;9,3表示第9行日後3行。


2.3.並排顯示

  兩個文件的內容並排顯示出來,使用選項:--side-by-side (-y)。但是使用--width=columns (-Wcolumns)指定顯示寬度。

[view@file diffutils]$ diff -y l t
The Way that can be told of is n   <
The name that can be named is no   <
The Nameless is the origin of He        The Nameless is the origin of He
The Named is the mother of all t   |    The named is the mother of all t
                                   >
Therefore let there always be no        Therefore let there always be no
  so we may see their subtlety,           so we may see their subtlety,
And let there always be being,          And let there always be being,
  so we may see their outcome.            so we may see their outcome.
The two are the same,                   The two are the same,
But after they are produced,            But after they are produced,
  they have different names.              they have different names.
                                   >    They both may be called deep and
                                   >    Deeper and more profound,
                                   >    The door of all subtleties!


2.4.默認輸出

  默認輸出,也稱爲通常輸出,選項「--normal」爲默認選項。

[view@file diffutils]$ diff l t
1,4c1,3
< The Way that can be told of is not the eternal Way;
< The name that can be named is not the eternal name.
< The Nameless is the origin of Heaven and Earth;
< The Named is the mother of all things.
---
> meless is the origin of Heaven and Earth;
> The named is the mother of all things.
>
11a11,13
> They both may be called deep and profound.
> Deeper and more profound,
> The door of all subtleties!



3.比較目錄

[view@file diffutils]$ ll
total 8
drwxrwxr-x 2 work work 4096 Aug 10 18:04 dir1
drwxrwxr-x 2 work work 4096 Aug 10 18:05 dir2
[view@file diffutils]$ tree
.
├── dir1
│   ├── l
│   └── t
└── dir2
    ├── l
    └── t

2 directories, 4 files
文件是一致的時候,沒有任何顯示
[view@file diffutils]$ diff dir1 dir2
[view@file diffutils]$ rm -f dir1/l
[view@file diffutils]$ diff dir1 dir2
Only in dir2: l
[view@file diffutils]$ touch dir1/l
當不一樣的文件是文本文件時,就比較倆文件內容
[view@file diffutils]$ diff dir1 dir2
diff dir1/l dir2/l
0a1,11
> The Way that can be told of is not the eternal Way;
> The name that can be named is not the eternal name.
> The Nameless is the origin of Heaven and Earth;
> The Named is the mother of all things.
> Therefore let there always be non-being,
>   so we may see their subtlety,
> And let there always be being,
>   so we may see their outcome.
> The two are the same,
> But after they are produced,
>   they have different names.
把輸出簡化
[view@file diffutils]$ diff -q dir1 dir2
Files dir1/l and dir2/l differ
有差別時,不輸出
[work@file diffutils]$ diff -q dir1 dir2 1>/dev/null
[work@file diffutils]$ echo $?
1


  再看看二進制文件比較

[view@file diffutils]$ diff bin1 bin2
[view@file diffutils]$ mv bin1/ls bin1/lls
[view@file diffutils]$ diff bin1 bin2
Only in bin1: lls
Only in bin2: ls
從結果看,比較二進制文件時,僅僅比較了文件名
[view@file diffutils]$ echo $?
1


  比較兩個目錄時,若是包含子目錄,會顯示關於子目錄的信息(目錄亦即文件)。默認不遞歸比較,可使用選項顯式指定「-r or--recursive」。比較目錄時,一樣會關注其中文本文件的內容(不一樣時會顯示出來)、二進制文件的名稱(文件一致名稱不一樣會顯示不一樣文件)。



wKiom1jwZNWRan0iAABm6i-XhL0322.jpg

相關文章
相關標籤/搜索