您的位置:首页 > 运维架构 > Linux

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

2016-05-29 08:38 531 查看
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等版本控制工具不可或缺的一部分。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: