Linux的man手册中详细列出了diff命令的参数和使用方式。这里摘出部分常用选项,之后再进行详细用法说明。
短选项名 | 长选项名 | 说明 |
--normal | 标准输出(默认) | |
-q | --brief | 文件存在差异时仅报告而不输出具体差异 |
-s | --report-identical-files | 文件相同时报告 |
-c, -C NUM | --context[=NUM] | 上下文输出模式,在各差异处输出NUM(默认3)行上下文 |
-u, -U NUM | --unified[=NUM] | 合并输出模式,在各差异处输出NUM(默认3)行上下文 |
-e | --ed | 输出ed脚本,指明FIEL1编辑为FILE2的操作 |
-n | --rcs | RCS输出模式 |
-y | --side-by-side | 并排输出模式,直观的显示文本差异 |
-W NUM | --width=NUM | 输出宽度,输出第行最多NUM(默认130)个字符 |
--left-column | 仅输出左列。仅并排输出模式时有效,行无差异时仅显示左侧文件行 | |
--suppress-common-lines | 忽略无差异行。仅并排输出模式时有效,不显示无差异行 | |
-t | --expand-tabs | 输出时将tab展开成空格 |
-T | --initial-tab | 输出时行首插入tab键 |
--tabsize=NUM | 设置输出tab宽度为NUM(默认8)个空格。需要配合使用-t,--expand-tabs选项 | |
--suppress-blank-empty | 忽略输出中空行的空白间隔 | |
-l | --paginate | 分页输出 |
--ignore-file-name-case | 忽略文件名大小写 | |
--no-ignore-file-name-case | 不忽略文件名大小写 | |
-i | --ignore-case | 忽略文件内容大小写 |
-E | --ignore-tab-expansion | 忽略tab缩进差异 |
-Z | --ignore-trailing-space | 忽略行尾空白 |
-b | --ignore-space-change | 忽略空格数量差异 |
-w | --ignore-all-space | 忽略全部空白差异 |
-B | --ignore-blank-lines | 忽略空白字符行 |
现结合实例对这几种输出分别说明。假设你本地有两个文件:
a.txt
vim edit diff is a tool to compare text files in Unix. diff tells differences of two files. usually,tow files diffrent from each other has some same parts. the diff command helps find out the diffrent parts. line. line below. line last.b.txt
diff is a tool to compare text files in Unix. diff tells differences of two text files. usually,tow files diffrent from each other has some same parts. many cvs software use the diff command. line. line below. line added. line last.注意a.txt末尾有一空行。
标准输出
命令:
diff a.txt b.txt或
diff --normal a.txt b.txt输出结果
1d0 < vim edit 3c2 < diff tells differences of two files. --- > diff tells differences of two text files. 5,6c4,5 < the diff command helps find out the diffrent parts. < line. --- > many cvs software use the diff command. > line. 7a7 > line added. 9d8 <
结果表示了从a.txt到b.txt发生的多处变化。每一处变化以/^\d+[adc]\d+$/行开始,a/d/c表示了变化的形式,d表示从a.txt到b.txt发生了删除(delete),a表示新增,c代表了变更。具体发行的变化在之后的行中明确表述。这些行以“<”或“>”开头,分别表示了删除和新增,c类型的差异中还会出现一行“---”,分隔删除和新增(也有说法是分隔了两个文件),并共同表述变更。
上下文输出
命令
diff -c a.txt b.txt或
diff -C 3 a.txt b.txt或
diff --context a.txt b.txt或
diff --context=3 a.txt b.txt输出结果
*** a.txt 2014-06-14 14:38:04.979753227 +0800 --- b.txt 2014-06-14 14:37:16.355754277 +0800 *************** *** 1,9 **** - vim edit diff is a tool to compare text files in Unix. ! diff tells differences of two files. usually,tow files diffrent from each other has some same parts. ! the diff command helps find out the diffrent parts. ! line. line below. line last. - --- 1,8 ---- diff is a tool to compare text files in Unix. ! diff tells differences of two text files. usually,tow files diffrent from each other has some same parts. ! many cvs software use the diff command. ! line. line below. + line added. line last.输出结果的前2行反应了文件的基本信息。
***表示源文件(变动前文件)
---表示目标文件(变动后文件)。第三行的
***************将文件基本信息与变化内容分隔开。
文件的变化部分分别表述了两个文件的变化,源文件中
*** 1,9 ****
表示了源文件1到7行发生了变化,接下来是变化的具体情况。变化的行行首有一个标记:!、-、+或空白。如果为空,表示该行无变化;如果是感叹号(!),表示该行有改动;如果是减号(-),表示该行被删除;如果是加号(+),表示该行为新增。
之后
--- 1,8 ---- diff is a tool to compare text files in Unix. ! diff tells differences of two text files. usually,tow files diffrent from each other has some same parts. ! many cvs software use the diff command. ! line. line below. + line added. line last.同理表述了目标文件的变化
合并输出
命令
diff -u a.txt b.txt或
diff -U 3 a.txt b.txt或
diff --unified a.txt b.txt或
diff --unified=3 a.txt b.txt
输出结果
--- a.txt 2014-06-14 14:38:04.979753227 +0800 +++ b.txt 2014-06-14 14:37:16.355754277 +0800 @@ -1,9 +1,8 @@ -vim edit diff is a tool to compare text files in Unix. -diff tells differences of two files. +diff tells differences of two text files. usually,tow files diffrent from each other has some same parts. -the diff command helps find out the diffrent parts. - line. +many cvs software use the diff command. + line. line below. +line added. line last. -类似上下文输出,前两行也是文件基本信息。
第三行表述了变化范围
@@ -1,9 +1,8 @@
源文件(“-”表示)第1行开始之后7行,目标文件(“+”表示)第1行开始之后5行
具体变更部分也有行首的标识,空白无变化,“-”表示删除行,“+”表示增加行。
版本控制系统git的diff输出是此输出格式的变种
ed脚本输出
命令
diff -e a.txt b.txt或
diff --ed a.txt b.txt输出结果
9d 7a line added. . 5,6c many cvs software use the diff command. line. . 3c diff tells differences of two text files. . 1d
</pre><p>此输出表述了从源文件到目标文件一步一步的操作,主要供gnu ed程序调用。</p><p>每一步操作由操作范围类型、操作详情、结束标识构成。第一行表明变化发生的行号和变化类型。</p><p><pre name="code" class="plain">5,6c
表示5到6行变更
7a
表示第7行新增
1d表示第1行删除
操作类型有adc,意义与标准输出相同
ac操作类型具有操作体,表明操作后行号范围变化结果。
操作体以“.”作为结束标识。
RCS输出
命令
diff -n a.txt b.txt或
diff --rcs a.txt b.txt结果输出
d1 1 d3 1 a3 1 diff tells differences of two text files. d5 2 a6 2 many cvs software use the diff command. line. a7 1 line added. d9 1此输出主要供gnu rcs程序调用。操作类型仅ad两种,操作类型后表述了操作的开始行号和操作行数。操作类型a表示新增,d表示删除。此输出与ed不同,每一处变更表述不需要先执行先前的变更,其直接表述了当前变更源文件和目标文件当前处发生的变化。发生内容替换变更时,在同一行处由一条删除表述与一条新增表述共同表示。a操作头后是新增的具体内容,行数与a操作头中表述的操作行数相同。
并排输出
命令
diff -y a.txt b.txt
diff --side-by-side a.txt b.txt
vim edit < diff is a tool to compare text files in Unix. diff is a tool to compare text files in Unix. diff tells differences of two files. | diff tells differences of two text files. usually,tow files diffrent from each other has some same part usually,tow files diffrent from each other has some same part the diff command helps find out the diffrent parts. | many cvs software use the diff command. line. | line. line below. line below. > line added. line last. line last. <