功能:比较两个文件的差异,并把不同地方的信息显示出来。默认diff格式的信息。

diff比较两个文件或文件集合的差异,并记录下来,生成一个diff文件,这也是我们常说的补丁文件。也使用patch命令对相应的文件打补丁。differential [ˌdɪfəˈrenʃəl]

语法:diff    [options]   FILES
FILES的格式:

FILE1 FILE2 :源是一个文件,目标也是文件。这两个文件必须是文本文件。以逐行的方式,比较文本文件的异同处。
DIR1 DIR2   :源是一个目录,目标是目录。diff 命令会比较两个目录下名字相同的文本文件,依照字母次序排序,列出不同的二进制文件,列出公共子目录,列出只在一个目录出现的文件。
FILE DIR     :源是一个文件,目标是目录。diff命令把源文件与目标目录下的同名文件比较。
DIR FILE     :源是一个目录,目标是文件(不是目录)。源目录下所有文件中与目标文件同名的文件,将用来与目标文件比较。
FILE可以是“-”,代表由标准输入设备读入的文本。DIR不能是"-"。
被指定的文件不能是标准的输入。

短选项 长选项 含义
-i --ignore-case 忽略文件内容大小写的区别
  --ignore-file-name-case 忽略文件名大小写的区别
  --no-ignore-file-name-case 不忽略文件名大小写的区别
-E --ignore-tab-expansion 忽略由制表符宽度造成的差异
-b --ignore-space-change 忽略由空格数不同造成的差异
-w --ignore-all-space 忽略所有空格
-B --ignore-blank-lines 忽略任何因空行而造成的差异
-I --ignore-matching-lines=RE 如果某行匹配正则表达式,则忽略由该行造成的差异
-a --text 所有文件都以文本方式处理
  --strip-trailing-cr 去除输入内容每行末端的 carriage return 字符
-c 或 或 -C 行数 --context[=行数] 显示指定<行数>(默认 3 行)copied 格式的上下文
-u  或 -U 行数 --unified[=行数] 显示指定<行数>(默认 3 行)unified 格式的上下文(合并的方式)
  --label 标识 使用<标识>代替文件名称
-p --show-c-function 显示和每个差异有关的 C 函数名称
-F  --show-function-line=RE 显示最接近而符合<正则表示式>的一行
-q --brief 只显示文件是否不同
-e --ed 以 ed script 方式输出
  --normal 以正常的 diff 方式输出
-n --rcs 以 RCS diff 格式输出
-y --side-by-side 以两列并排的方式显示
-W --width=NUM 每行显示最多 NUM (默认 130) 个字符
  --left-column 当有两行相同时只显示左边的一行
  --suppress-common-lines 当有两行相同时不会显示
-D --ifdef=NAME 输出的内容以‘#ifdef NAME’方式标明差异
  --GTYPE-group-format=GFMT 效果类似,但会以 GFMT 格式处理 GTYPE 输入的行
可以是LTYPE的选择或是‘changed’
  --line-format=LFMT 效果类似,但会以 LFMT 格式处理每一行资料
  --LTYPE-line-format=LFMT 效果类似,但会以 LFMT 格式处理 LTYPE 输入的行
LTYPE 可以是‘old’、‘new’或‘unchanged’。
-l --paginate 将输出送至‘pr’指令来分页
-t --expand-tabs 将输出中的 tab 转换成空格
-T --initial-tab 每行先加上 tab 字符,使 tab 字符可以对齐
  --tabsize=NUM 定位字符 (tab) 的宽度,默认为 8 个空格宽
  --suppress-blank-empty suppress space or tab before empty output lines
-r --recursive 递归比较子目录中的文件
-N --new-file 不存在的文件以空文件方式处理
  --unidirectional-new-file 若第一文件案不存在,以空文件处理
-s --report-identical-files 文件相同则报告,否则无任何提示
-x --exclude=PAT 排除匹配 PAT 的文件
-X --exclude-from=FILE 排除所有匹配在 FILE 中列出的模式的文件
-S --starting-file=FILE 当比较目录時,由 FILE 开始比较
  --from-file=FILE1 将 FILE1 和操作数中的所有文件/目录作比较。FILE1 可以是目录
  --to-file=FILE2 将操作数中的所有文件/目录和 FILE2 作比较。FILE2 可以是目录
  --horizon-lines=NUM keep NUM lines of the common prefix and suffix
-d --minimal 尽可能找出最小的差异
  --speed-large-files 假设文件十分大而且其中含有许多微小的差异
diff输出格式(默认):
n1 a n3,n4 表示在文件1的n1行后面添加n3到n4行
n1,n2 d n3 表示在n1到n2行之间删除n3行
n1,n2 c n3,n4 表示把n1,n2行用n3,n4行替换掉
字母a:表示附加(add)
字符c:表示修改(change)
字符d:表示删除(delete)
字母前的是源文件,字母后是目标文件。Nx表示行号。
以”<”打头的行属于第一个文件,以”>”打头的行属于第二个文件。

比较文件

log2014.log内容
2013-01
2013-02
2014-03
2013-04
2013-05
2013-06
2013-07
2013-07
2013-09
2013-10
2013-11
2013-12

log2013.log内容
2013-01
2013-02
2013-03
2013-04
2013-05
2013-06
2013-07
2013-08
2013-09
2013-10

比较两个文件
[root@localhost test3]# diff log2014.log log2013.log   
3c3
< 2014-03
---
> 2013-03
8c8
< 2013-07
---
> 2013-08
11,12d10
< 2013-11
< 2013-12
上面的“3c3”和“8c8”表示log2014.log和log20143log文件在3行和第8行内容有所不同;"11,12d10"表示第一个文件比第二个文件多了第11和12行。

以”<”打头的行属于第一个文件,以”>”打头的行属于第二个文件。
 
 
 
并排格式输出
[root@localhost test3]# diff log2014.log log2013.log  -y -W 50
2013-01                 2013-01
2013-02                 2013-02
2014-03               | 2013-03
2013-04                 2013-04
2013-05                 2013-05
2013-06                 2013-06
2013-07                 2013-07
2013-07               | 2013-08
2013-09                 2013-09
2013-10                 2013-10
2013-11               <
2013-12               <
 
[root@localhost test3]# diff log2013.log log2014.log  -y -W 50
2013-01                 2013-01
2013-02                 2013-02
2013-03               | 2014-03
2013-04                 2013-04
2013-05                 2013-05
2013-06                 2013-06
2013-07                 2013-07
2013-08               | 2013-07
2013-09                 2013-09
2013-10                 2013-10
                      > 2013-11
                      > 2013-12
说明:

“|”表示前后2个文件内容有不同

“<”表示后面文件比前面文件少了1行内容

“>”表示后面文件比前面文件多了1行内容

上下文输出格式
[root@localhost test3]# diff log2013.log log2014.log  -c
*** log2013.log 2012-12-07 16:36:26.000000000 +0800
--- log2014.log 2012-12-07 18:01:54.000000000 +0800
***************
*** 1,10 ****
  2013-01
  2013-02
! 2013-03
  2013-04
  2013-05
  2013-06
  2013-07
! 2013-08
  2013-09
  2013-10
--- 1,12 ----
  2013-01
  2013-02
! 2014-03
  2013-04
  2013-05
  2013-06
  2013-07
! 2013-07
  2013-09
  2013-10
+ 2013-11
+ 2013-12
 
[root@localhost test3]# diff log2014.log log2013.log  -c
*** log2014.log 2012-12-07 18:01:54.000000000 +0800
--- log2013.log 2012-12-07 16:36:26.000000000 +0800
***************
*** 1,12 ****
  2013-01
  2013-02
! 2014-03
  2013-04
  2013-05
  2013-06
  2013-07
! 2013-07
  2013-09
  2013-10
- 2013-11
- 2013-12
--- 1,10 ----
  2013-01
  2013-02
! 2013-03
  2013-04
  2013-05
  2013-06
  2013-07
! 2013-08
  2013-09
  2013-10
[root@localhost test3]#
这种方式在开头两行作了比较文件的说明,这里有三中特殊字符:

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

“-” 比较的文件的后者比前着少一行

“!” 比较的文件两者有差别的行
 
 
 
统一格式输出
[root@localhost test3]# diff log2014.log log2013.log  -u
--- log2014.log 2012-12-07 18:01:54.000000000 +0800
+++ log2013.log 2012-12-07 16:36:26.000000000 +0800
@@ -1,12 +1,10 @@
 2013-01
 2013-02
-2014-03
+2013-03
 2013-04
 2013-05
 2013-06
 2013-07
-2013-07
+2013-08
 2013-09
 2013-10
-2013-11
-2013-12
 
第一部分,也是文件的基本信息:

--- log2014.log 2012-12-07 18:01:54.000000000 +0800

+++ log2013.log 2012-12-07 16:36:26.000000000 +0800

"---"表示变动前的文件,"+++"表示变动后的文件。

第二部分,变动的位置用两个@作为起首和结束

@@ -1,12  +1,10 @@

前面的"-1,12"分成三个部分:减号表示第一个文件(即log2014.log),"1"表示第1行,"12"表示连续12行。意思:第一个文件从第1行开始的连续12行。

同样的,"+1,10" 表示第二个文件从第1行开始的连续10行。

比较文件夹

[root@localhost test]# diff test3 test6
Only in test6: linklog.log
Only in test6: log2012.log
 
[root@localhost test]# diff test3/log2013.log test6/log2013.log
1,10c1,3
< 2013-01
< 2013-02
< 2013-03
< 2013-04
< 2013-05
< 2013-06
< 2013-07
< 2013-08
< 2013-09
< 2013-10
---
> hostnamebaidu=baidu.com
> hostnamesina=sina.com
> hostnames=true
 
[root@localhost test]# diff test3/log2014.log test6/log2014.log
1,12d0
< 2013-01
< 2013-02
< 2014-03
< 2013-04
< 2013-05
< 2013-06
< 2013-07
< 2013-07
< 2013-09
< 2013-10
< 2013-11
< 2013-12
Only in test6: log2015.log
Only in test6: log2016.log
Only in test6: log2017.log
[root@localhost test]#

比较两个文件不同,并生产补丁

[root@localhost test3]# diff -ruN log2013.log log2014.log >patch.log
[root@localhost test3]# ll
总计 12
-rw-r--r-- 2 root root  80 12-07 16:36 log2013.log
-rw-r--r-- 1 root root  96 12-07 18:01 log2014.log
-rw-r--r-- 1 root root 248 12-07 21:33 patch.log
 
[root@localhost test3]# cat patch.log
--- log2013.log 2012-12-07 16:36:26.000000000 +0800
+++ log2014.log 2012-12-07 18:01:54.000000000 +0800
@@ -1,10 +1,12 @@
 2013-01
 2013-02
-2013-03
+2014-03
 2013-04
 2013-05
 2013-06
 2013-07
-2013-08
+2013-07
 2013-09
 2013-10
+2013-11
+2013-12
[root@localhost test3]#

打补丁

[root@localhost test3]# cat log2013.log
2013-01
2013-02
2013-03
2013-04
2013-05
2013-06
2013-07
2013-08
2013-09
2013-10
 
[root@localhost test3]# patch log2013.log patch.log  使用上面产生的patch.log文件打补丁
patching file log2013.log
 
[root@localhost test3]# cat log2013.log
2013-01
2013-02
2014-03
2013-04
2013-05
2013-06
2013-07
2013-07
2013-09
2013-10
2013-11
2013-12
[root@localhost test3]#

实例

实例:有这样两个文件。程序清单1,程序清单2
程序清单1 :hello.c
#include
int main(void)
{
    char msg[] = "Hello world!";
   
    puts(msg);
    printf("Welcome to use diff commond.n");
   
    return 0; 
}
 
程序清单2:hello_diff.c
#include
#include 
int main(void)
{
    char msg[] = "Hello world,fome hello_diff.c";
   
    puts(msg);
    printf("hello_diff.c says,'Here you are,using diff.'n");
   
    return 0;
}
 
 
我们使用diff命令来查看这两个文件的不同之处,有一下几种方便的方法:

1、普通格式输出:
[root@localhost diff]# diff hello.c hello_diff.c
1a2
> #include
5c6
       char msg[] = "Hello world,fome hello_diff.c";
8c9
       printf("hello_diff.c says,'Here you are,using diff.'n");
[root@localhost diff]#
上面的“1a2”表示后面的一个文件"hello_diff.c"比前面的一个文件"hello.c"多了一行
"5c6"表示第一个文件的第5行与第二个文件的第6行有区别
 
 
2、并排格式输出
[root@localhost diff]# diff hello.c hello_diff.c -y -W 130  -W选择可以指定输出列的宽度,这里指定输出列宽为130
#include                                                      #include
                                                              > #include
int main(void)                                                  int main(void)
{                                                               {
        char msg[] = "Hello world!";                          |         char msg[] = "Hello world,fome hello_diff.c";
        puts(msg);                                                      puts(msg);
        printf("Welcome to use diff commond.n");             |         printf("hello_diff.c says,'Here you are,using diff.'
        return 0;                                                       return 0;
}                                                               }
[root@localhost diff]#
这种并排格式的对比一目了然,可以快速找到不同的地方。
 
 
3、上下文输出格式
[root@localhost diff]# diff hello.c hello_diff.c -c
*** hello.c     2007-09-25 17:54:51.000000000 +0800
--- hello_diff.c        2007-09-25 17:56:00.000000000 +0800
***************
*** 1,11 ****
  #include 
 
  int main(void)
  {
!       char msg[] = "Hello world!";
 
        puts(msg);
!       printf("Welcome to use diff commond.n");
 
        return 0;
  }
--- 1,12 ----
  #include
+ #include
 
  int main(void)
  {
!       char msg[] = "Hello world,fome hello_diff.c";
 
        puts(msg);
!       printf("hello_diff.c says,'Here you are,using diff.'n");
 
        return 0;
  }
[root@localhost diff]#
这种方式在开头两行作了比较文件的说明,这里有三中特殊字符:

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

-        比较的文件的后者比前着少一行

!        比较的文件两者有差别的行
 
 
 
4、统一输出格式
[root@localhost diff]# diff hello.c hello_diff.c -u
--- hello.c     2007-09-25 17:54:51.000000000 +0800
+++ hello_diff.c        2007-09-25 17:56:00.000000000 +0800
@@ -1,11 +1,12 @@
#include
+#include
 
int main(void)
{
-       char msg[] = "Hello world!";
+       char msg[] = "Hello world,fome hello_diff.c";
 
        puts(msg);
-       printf("Welcome to use diff commond.n");
+       printf("hello_diff.c says,'Here you are,using diff.'n");
 
        return 0;
}
[root@localhost diff]#
 
 
 
5、其他 
[root@localhost diff]# diff hello.c hello_diff.c -q  假如你想查看两个文件是否不同又不想显示差异之处的话,可以加上-q选项
Files hello.c and hello_diff.c differ
[root@localhost diff]#
[root@localhost diff]# diff hello.c hello_diff.c -c -I include  这里通过“ -I include”选项来忽略带有“ include”字样的行。另外你还可以提供一些匹配规则来忽略某中差别,可以用 -I regexp
*** hello.c     2007-09-25 17:54:51.000000000 +0800
--- hello_diff.c        2007-09-25 17:56:00.000000000 +0800
***************
*** 2,11 ****
 
  int main(void)
  {
!       char msg[] = "Hello world!";
 
        puts(msg);
!       printf("Welcome to use diff commond.n");
 
        return 0;
  }
--- 3,12 ----
  
  int main(void)
  {
!       char msg[] = "Hello world,fome hello_diff.c";
 
        puts(msg);
!       printf("hello_diff.c says,'Here you are,using diff.'n");
 
        return 0;
  }
[root@localhost diff]#

diff 比较两个文件的差异的更多相关文章

  1. diff比较两个文件的差异

    1.diff -ruN a.txt b.txt>patch.txt比较第二个文件与第一个文件相比的变化,并将变化添加到patch.txt文件中,-表示删除的行,+表示添加的行 2.下面的,“&l ...

  2. diff比较两个文件 linux

    功能:比较两个文件的差异,并把不同地方的信息显示出来.默认diff格式的信息. diff比较两个文件或文件集合的差异,并记录下来,生成一个diff文件,这也是我们常说的补丁文件.也使用patch命令对 ...

  3. linux对比两个文件的差异

    在项目维护阶段,经常会对垃圾文件进行清理.比如没有在数据库中的文件进行删除,这个时候最好的选择就是使用shell命令了:废话不多说直接上代码: 1.首先准备好从数据表导出来的数据,方法随意 2.在服务 ...

  4. diff 比较两个文件的不同

    1.命令功能 diff 逐行比较文件内容,并输出文件差异. 2.语法格式 diff  option   file1    file2 diff  选项   文件1   文件2 参数说明 参数 参数说明 ...

  5. linux比较两个文件的差异

    1. vimdiff $ vimdiff in.txt out.txt 垂直打开:  vimdiff   point.c     point-a.c 水平打开:   vimdiff -o  point ...

  6. diff - 找出两个文件的不同点

    总览 diff [选项] 源文件 目标文件 描述 在最简单的情况是, diff 比较两个文件的内容 (源文件 和 目标文件). 文件名可以是 - 由标准输入设备读入的文本. 作为特别的情况是, dif ...

  7. linux cmp-比较两个文件是否有差异

    推荐:更多Linux 文件查找和比较 命令关注:linux命令大全 cmp命令用来比较两个文件是否有差异.当相互比较的两个文件完全一样时,则该指令不会显示任何信息.若发现有差异,预设会标示出第一个不通 ...

  8. linux比较两个文件的不同(6/21)

    cmp 命令:比较任意两个类型的文件,且吧结果输出到标准输出,默认文件相同不输出,不同的文件输出差异 必要参数 -c 显示不同的信息-l 列出所有的不同信息-s 错误信息不提示 选择参数 -i< ...

  9. linux diff-比较给定的两个文件的不同

    推荐:更多Linux 文件查找和比较 命令关注:linux命令大全 diff命令在最简单的情况下,比较给定的两个文件的不同.如果使用“-”代替“文件”参数,则要比较的内容将来自标准输入.diff命令是 ...

随机推荐

  1. 【转】高效Java编程工具集锦

    原文地址:http://geek.csdn.net/news/detail/57469 Java 开发者常常都会想办法如何更快地编写 Java 代码,让编程变得更加轻松.目前,市面上涌现出越来越多的高 ...

  2. 理解 Glance - 每天5分钟玩转 OpenStack(20)

    OpenStack 由 Glance 提供 Image 服务. 理解 Image 要理解 Image Service 先得搞清楚什么是 Image 以及为什么要用 Image? 在传统 IT 环境下, ...

  3. 001.mysql安装(lnmp)

    mysql官方网站:http://dev.mysql.com/downloads/ Linux环境:刚安装的32位的“最小化安装“的CentOS 6.7 mysql版本:本次实验安装的是mysql5. ...

  4. ActionBar详解

    转: 一.ActionBar介绍 在Android 3.0中除了我们重点讲解的Fragment外,Action Bar也是一个非常重要的交互元素,Action Bar取代了传统的tittle bar和 ...

  5. linux命令之tail

    tail用于输出文件末尾部分.一个比较有用的功能是tail + grep实现类似于安卓开发时调试使用的logcat,具体操作是: 一般我是用SecureCRT连接linux,然后使用SecureCRT ...

  6. 自己第一个github开源的感受

    自己在github上发布了开源<基于IOS的手机视频直播SDK>后,不到一个月,被人star了508次,fork了120次,这个成绩大大出乎了我自己的意料! github网址:https: ...

  7. MIDI信息为什么不能通过FL Studio输出

    讲解这个问题之前,小编先给大家科普一下什么是MIDI.所谓的MIDI其实只是数字信号,和我们熟知的乐谱有点像.它的作用就是能很好的传达什么时间演奏什么音,这个音要演奏多久等信息,就如同我们演唱会中指挥 ...

  8. Oracle 中的分析函数

    Oracle常用分析函数介绍(排名函数+窗口函数) 2014年11月30日 ⁄ 数据库 ⁄ 共 3903字 ⁄ 暂无评论 ⁄ 阅读 7,772 次 评级函数 常见评级函数如下: RANK():返回数据 ...

  9. C#.NET 大型企业信息化系统集成快速开发平台 4.2 版本 - 所有的基础数据都可以恢复删除

    客户的需求如下: 所有基礎信息需要記錄創建人,創建時間,更改人,更改時間,刪除人,刪除時間.有停用基礎信息功能(停用不是刪除,只是暫時停用).基礎信息可以查出已經刪除的信息(有選項可以選擇),有方法把 ...

  10. Android Monkey压力测试

    Monkey 是Android SDK提供的一个命令行工具, 可以简单,方便地运行在任何版本的Android模拟器和实体设备上. Monkey会发送伪随机的用户事件流,适合对app做压力测试. 1为什 ...