linux下diff命令的使用

作为开发人员,经常会遇到需要比较文件差异的情况,如果可以将文件下载到本地的话,通常可以使用meld这样的图形化工具方便地在本地进行文件比较;否则只能在服务器的ssh shell中使用diff了。

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--rcsRCS输出模式
-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忽略空白字符行

如果你仔细查看上表,你会发现,diff有多种输出结果:标准输出、上下文输出、合并输出、ed脚本输出、RCS输出和并排输出。

现结合实例对这几种输出分别说明。假设你本地有两个文件:

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.
							      <

输出结果与shell宽度、输出宽度选项等有关。此输出结果是最直观结果,主要供人员快速查阅使用。因shell显示宽度限制,且无行号提示,使用不多。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值