diff 的使用

- <b>—ignore-tab-expansion or -E </b> 表示认为 4 个空格 和 1 个 TAB 字符是相同的。

- <b>—ignore-space-change or -b </b> 表示一个空格字符或 者 TAB 字符和多个空格字符和 TAB 字符是一样的。

- <b>—ignore-all-space or -w</b> 根本就当空格字符和TAB 字符不存在。

例子:

$cat a.txt
There are some blanks space at the end of line.
This is a common line
Space or Tab between words.
This is a common line
Here you are.
$cat b.txt
There are some blanks space at the end of line.
This is a common line
Space or Tab between    words.
This is a common line
Here youa    re.
$xxd a.txt
0000000: 5468 6572 6520 6172 6520 736f 6d65 2062  There are some b
0000010: 6c61 6e6b 7320 7370 6163 6520 6174 2074  lanks space at t
0000020: 6865 2065 6e64 206f 6620 6c69 6e65 2e0a  he end of line..
0000030: 5468 6973 2069 7320 6120 636f 6d6d 6f6e  This is a common
0000040: 206c 696e 650a 5370 6163 6520 6f72 2054   line.Space or T
0000050: 6162 2062 6574 7765 656e 2077 6f72 6473  ab between words
0000060: 2e0a 5468 6973 2069 7320 6120 636f 6d6d  ..This is a comm
0000070: 6f6e 206c 696e 650a 4865 7265 2079 6f75  on line.Here you
0000080: 2061 7265 2e0a                            are..
$xxd b.txt
0000000: 5468 6572 6520 6172 6520 736f 6d65 2062  There are some b
0000010: 6c61 6e6b 7320 7370 6163 6520 6174 2074  lanks space at t
0000020: 6865 2065 6e64 206f 6620 6c69 6e65 2e20  he end of line.
0000030: 2020 2020 0a54 6869 7320 6973 2061 2063      .This is a c
0000040: 6f6d 6d6f 6e20 6c69 6e65 0a53 7061 6365  ommon line.Space
0000050: 206f 7220 5461 6220 6265 7477 6565 6e09   or Tab between.
$diff a.txt b.txt
1c1
< There are some blanks space at the end of line.
---
> There are some blanks space at the end of line.
3c3
< Space or Tab between words.
---
> Space or Tab between  words.
5c5
< Here you are.
---
> Here youa    re.
$diff -b a.txt b.txt
5c5
< Here you are.
---
> Here youa    re.
$diff -w a.txt b.txt
<b>-B or —ignore-blank-lines</b> 表示忽略空行。 例如:
$cat x.txt
1. A point is that which has no part.
2. A line is breadthless length.
-- Euclid,The Elements
$cat y.txt
1. A point is that which has no part.



2. A line is breadthless length.

-- Euclid,The Elements
$diff x.txt y.txt
1a2,4
>
>
>
2a6
>
$diff -B x.txt y.txt
<example>

<b> -i or --ignore-case </b> 表示忽略大小写。例如:
<example>
$echo "Hello World" > m.txt
$echo "hello world" > n.txt
$diff m.txt n.txt
1c1
< Hello World
---
> hello world
$diff -i m.txt n.txt
<b>-I <i>regexp</i> or —ignore-matching-lines=<i>regexp</i> </b> 表示忽略 一些包含和某些正则表达式匹配的行。例如:
$cat a.sh
#!/bin/bash
#this is comment
echo "Hello World"
$cat b.sh
#!/bin/bash
#THIS IS COMMENT
echo "Hello World"
$diff a.sh b.sh
2c2
< #this is comment
---
> #THIS IS COMMENT
$diff -I '^#' a.sh b.sh
<example>
但是要注意,注释还不能和空行一样。例如:
<example>
$cat a.sh
#!/bin/bash

echo "Hello World"
$cat b.sh
#!/bin/bash
#THIS IS COMMENT
echo "Hello World"
$diff -I '^#' a.sh b.sh
2c2
<
---
> #THIS IS COMMENT
可以认为是先删除两个文件中所有和 <i>regexp</i> 匹配的行,然 后再进行比较。 <b>—brief or -q </b> 表示只显示文件相同或者不相同,不用显示 具体哪里不一样。

如果要比较的两个文件,只要有一个文件是二进制文件,就假定 <b>—brief or -q </b> 已经指定了,因为没有办法一行一行的比 较二进制文件。

diff 程序看文件开头的几千个字符,如果他们都不是 null-character (ascii 0) 的话,就认为是文本文件,否则认为是 二进制文件。 如果一定要强制用文本文件的方式,一行一行的比较 的话,就是用 <b> -a or —text </b> 。

DOS 或者 Window 下的文本文件是以回车换行连个字符表示一 行的结束,<b>—strip-trailing-cr </b>可以去掉回车字符,统一 用换行表示一行的结束,这个办法对于比较 DOS 和 Unix 下的文件 很有用。

输出格式。一般的输出格式如下。

change-command
< from-file-line
< from-file-line...
---
> to-file-line
> to-file-line...
<i>change-command</i> 有三个部分组成,原地址,操作类型,目的 地址。 操作类型可以是 a , d , c ,分别表示添加,删除,修改。 例如 : 8a12,15 表示用第二个文件的 8 到 15 行添加到第一个 文件的第8行后面。12,20c50,100 表示用第二个文件的第50到100行 替换掉第一个文件的第12行到20行。 5,7d3 表示删除第一个文件的 5 到 7 行,如果不删除这些行,而是把他们添加在第二个文件的第3 行的后 面,两个文件就相同了。

- <b>-r</b> 表示递归比较目录

- <b>-u</b> 采用标准格式输入.

- <b>-N</b>

常用法

diff -ba