diff命令在最简单的情况下,比较给定的两个文件的不同。如果使用“-”代替“文件”参数,则要比较的内容将来自标准输入。diff命令是以逐行的方式,比较文本文件的异同处。如果该命令指定进行目录的比较,则将会比较该目录中具有相同文件名的文件,而不会对其子目录文件进行任何比较操作。

    (1)用法:

用法:  diff  [选项参数]  [文件1或目录1] [文件2或目录2]

  (2)功能:

功能:  diff命令能比较单个文件或者目录内容。如果指定比较的是文件,则只有当输入为文本文件时才有效。以逐行的方式,比较文本文件的异同处。如果指定比较的是目录的的时候,diff 命令会比较两个目录下名字相同的文本文件。列出不同的二进制文件、公共子目录和只在一个目录出现的文件。

    (3)选项参数:

1) -y  --side-by-side            以并列的方式显示文件的异同之处。

2) -W --width                在使用-y参数时,指定栏宽。

3) -c                    显示全部内文,并标出不同之处。

4) -u -U --unified              以合并的方式来显示文件内容的不同。

5) -r --recursive              比较子目录中的文件

6) -n --rcs                 将比较结果以RCS的格式来显示。

    (4)实例:

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
[]+ 已停止 cat > t2.txt //按ctrl+z键停止
[root@localhost Document]# diff ../Document/t1.txt ../Document/t2.txt diff后的两个文件参数可以跟相对路径也可以跟绝对路径
,5c3
< Name is t1.txt!
< The extra content!
< I am MenAngel!
---
> Name is t2.txt!
[root@localhost Document]# diff t1.txt t2.txt
,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  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  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 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 -- ::25.949100752 -
--- t2.txt -- ::54.287100555 -
***************
*** , **** //从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行
this is a text! ! Name is t2.txt! //第三行不同 

  说明:

  这种方式在开头两行作了比较文件的说明,这里有三中特殊字符:

    (“+” 比较的文件的后者比前着多一行

    “-” 比较的文件的后者比前着少一行)  //-u参数用的

    “!” 比较的文件两者有差别的行

6)[root@localhost Document]# diff -u t1.txt t2.txt        以合并的方式显示文本的不同      

[root@localhost Document]# diff -u t1.txt t2.txt                        //它的第一部分,也是文件的基本信息
--- t1.txt -- ::25.949100752 -0700 //-号表示第一个文件
+++ t2.txt -- ::54.287100555 -0700 //+号表示第二个文件
@@ -, +, @@
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 !
> 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 !
> 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,
< Total !
---
>
> Total !
只在 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,
< Total !
---
>
> Total !
只在 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? [n] y //还不知道是为什么?
[root@localhost Document]# cat t2.txt
this is a text! Name is t1.txt!
The extra content!
I am MenAngel!

    (5)其他:

扩展知识:

diff 命令是 linux上非常重要的工具,用于比较文件的内容,特别是比较两个版本不同的文件以找到改动的地方。diff在命令行中打印每一个行的改动。最新版本的diff还支持二进制文件。diff程序的输出被称为补丁 (patch),因为Linux系统中还有一个patch程序,可以根据diff的输出将a.c的文件内容更新为b.c。diff是svn、cvs、git等版本控制工具不可或缺的一部分。

每天一个Linux命令(31)diff命令的更多相关文章

  1. 每天一个linux命令(49)--diff命令

    diff 命令是 Linux 上非常重要的工具,用于比较文件的内容,特别是比较两个版本不同的文件以找到改动的地方.diff 在命令行中打印每一个行的改动.最新版本的diff还支持二进制文件,diff ...

  2. linux常用命令:diff 命令

    diff 命令是 linux上非常重要的工具,用于比较文件的内容,特别是比较两个版本不同的文件以找到改动的地方.diff在命令行中打印每一个行的改动.最新版本的diff还支持二进制文件.diff程序的 ...

  3. Linux 文本对比 diff 命令详解(整理)

    diff 命令详解 1.概述 windows系统下面就有不错的文本对比工具可以使用,例如常用的Beyond Compare,WinMerge都是图形界面的比较工具而且使用非常方便,如果你仅仅是在win ...

  4. 【Linux常见命令】diff命令

    diff - compare files line by line diff命令用于比较文件的差异. diff以逐行的方式,比较文本文件的异同处. 如果指定要比较目录,则diff会比较目录中相同文件名 ...

  5. 每天一个linux命令31)--chown命令

    chown将 指定文件的拥有者改为指定的用户或组,用户可以是用户名或者用户ID,组可以使组名或者组ID:文件是以空格分开的要改变权限的文件列表,支持通配符.系统管理员经常使用chown命令,在将文件拷 ...

  6. linux中的diff命令

    今天在公司的代码中看到了一个用的不是很多的命令diff,一开始以为不是,后来一查发现还真有这个命令,有关它的详细资料在这个网址中查看[http://blog.chinaunix.net/uid-253 ...

  7. linux每日命令(33):diff命令

    diff 命令是 linux上非常重要的工具,用于比较文件的内容,特别是比较两个版本不同的文件以找到改动的地方.diff在命令行中打印每一个行的改动.最新版本的diff还支持二进制文件.diff程序的 ...

  8. Linux学习历程——Centos 7 diff命令

    一.命令介绍 diff命令用于比较文本差异. diff以逐行的方式,比较文本文件的异同处.如果指定要比较目录,则diff会比较目录中相同文件名的文件,但不会比较其中子目录. ------------- ...

  9. Linux命令之---diff

    命令介绍 diff命令可以酌行比较纯文本文件内的内容,并输出文件的差异. 命令格式 diff [option] [file1] [file2] 举例子 1)比较俩文本文件 [root@king ~]# ...

随机推荐

  1. 浅谈struts2标签中的2个非经常常使用的标签的使用方法(radio和select)

    1.如图所看到的我们须要在前台的页面通过radio和select将相应的数据库中的数据显示到选项其中,这也是我们做项目中常常须要做的,动态的显示,而不是静态的显示. 首先我们须要在页面中导入strut ...

  2. springboot学习(二) 第一个springboot项目:Hello World!

    1.简介 可以像使用其他java标准库那样使用spriongboot,只需简单地在你的classpath下包含正确的 spring-boot-*.jar 文件.springboot不需要集成任何特殊的 ...

  3. 图解WinHex使用入门

    一 Winhex和相关概念简单介绍 1 Winhex 是在Windows下执行的十六进制编辑软件,此软件功能很强大,有完好的分区管理功能和文件管理功能.能自己主动分析分区链和文件簇链.能对硬盘进行不同 ...

  4. Photoshop中磁力套索的一种简陋实现(基于Python)

    经常用Photoshop的人应该熟悉磁力套索(Magnetic Lasso)这个功能,就是人为引导下的抠图辅助工具.在研发领域一般不这么叫,通常管这种边缘提取的办法叫Intelligent Sciss ...

  5. Parencodings - poj 1068

      Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 22764   Accepted: 13344 Description L ...

  6. 【JMeter4.0学习(二)】之搭建openLDAP在windows8.1上的安装配置以及JMeter对LDAP服务器的性能测试脚本开发

    目录: 概述 安装测试环境 安装过程 配置启动 配置搭建OpenLDAP 给数据库添加数据 测试查询刚刚插入的数据 客户端介绍 JMeter建立一个扩展LDAP服务器的性能测试脚本开发 附:LDAP学 ...

  7. 二级导航内容均分--jquery

    这个是去年做过的一个项目中的算法,个人感觉还可以,所以拿出来分享下. 背景:头部导航二级导航有些内容太长,一列的话太过难看,就要分成两列,要做到按块尽量均分,排列顺序没有限制. 原理: 1.把各个二级 ...

  8. python获取shell输出(转)

    From:http://www.cnblogs.com/snow-backup/p/5035792.html   python中获取shell命令输出的方法: 1.  import subproces ...

  9. CentOS 没有可用软件包 libmcrypt

    [1]安装libmcrypt 提示:没有可用软件包 解决办法: 1.安装第三方yum源 1.1 wget http://www.atomicorp.com/installers/atomic 1.2 ...

  10. git add -A使用说明

    git help add -A, --all            Like -u, but match <filepattern> against files in the workin ...