比较文件的差异

  diff,用来查看两个文件的差异之处,或者两个目录之中的对应文件。倘若比较的不是文本文件,而是二进制文件,只会报告两者不同。输出文本文件的异同时,可以按照多个格式输出,根据使用的选项决定。

  cmd,可以比较两个文件在字节、行数上的差异。

  diff3,可以比较三个文件的差异。当两个副本来自同一个原始文件时,能同时获取这两份较之原始文件的差异,而且能保留两者的修改生成一个新的文件、并提出有冲突的警告。

  sdiff,交互式合并两个文件。

  可以使用diff来发布源码补丁,可以考虑使用patch命令来打上这个补丁。diff命令可以理解为“减法结果”的生成,patch是把“减法结果”加到文件生成新文件。

  忽略掉“制表符”与“空格”的差异,使用选项“--ignore-tailing-apace”(-E)

1.比较方法


  对于文件的差异有多种情况,出于不同的目得有选择地忽略某些情况。比如由于空白符数量造成差异,或者存在一个仅仅包含空白符的空白行。又或者某些文件不会因为字母大小写而存在差异,但是比较时却认为存在差异。

  默认使用“diff file1 file2”时,显示差异的行信息。可以使用选项“-q”,仅仅显示异同结果,不同时“Files file1 and file2 differ”。

1.1.差异组

  俩文件的内容在忽略行顺序时,是一样的时候,比较时会因为参考的行不一样得到不同的输出结果。

  1. # file1
  2. i
  3. love
  4. you
  5. # file2
  6. you
  7. love
  8. i

1.2.空白符(忽略)

1.2.1.-E

  --ignore-tab-expansion(-E)

    忽略tab与空格,认为tab是“多个”空格。(“多个”-与数量无关、与长度有关系,这里成功测试是7个)

  1. [view@file diffutils]$ diff -q blank blank2
  2. Files blank and blank2 differ
  3. [view@file diffutils]$ diff -q -E blank blank2
  4. [view@file diffutils]$

1.2.2.-Z

  --ignore-trailing-space(-Z)

    忽略行尾的空格(认为行尾多个空格与没有空格等效)

  1. [view@file diffutils]$ diff -q blank blank2
  2. Files blank and blank2 differ
  3. [view@file diffutils]$ diff -q -Z blank blank2
  4. [view@file diffutils]$

1.2.3.-b

  --ignore-space-change(-b)

    效果就像上边两项的集合,忽略行尾、行中的“空格与多个空格”的差异。这里就就直接认为tab与空格是一回事,而且不限制“等长”。

  1. [view@file diffutils]$ diff -q blank blank2
  2. Files blank and blank2 differ
  3. [view@file diffutils]$ diff -q -E blank blank2
  4. Files blank and blank2 differ
  5. [view@file diffutils]$ diff -q -Z blank blank2
  6. Files blank and blank2 differ
  7. [view@file diffutils]$ diff -q -b blank blank2
  8. [view@file diffutils]$

1.2.4.-w

  --ignore-all-space(-w)

    忽略行首存在空白符时的差异(一个有、另一个没有)。空白符包括:水平制表符\t、垂直制表符\v、换页\f、回车\r、空格、以及其他定义的空白符。

  1. [view@file diffutils]$ diff -q -b blank blank2
  2. Files blank and blank2 differ
  3. [view@file diffutils]$ diff -q -w blank blank2
  4. [view@file diffutils]$

1.3.空白行(忽略)

--ignore-blank-lines(-B)

    空白行,就是“^?”。当这个空白行位于文件的不同“行位置”时,希望忽略这个情况,需要使用该选项。

  1. [view@file diffutils]$ diff -q blank blank2
  2. Files blank and blank2 differ
  3. [view@file diffutils]$ diff -q -B blank blank2
  4. [view@file diffutils]$

    当空白行是个“^[:blank:]?”,该选项就无法忽略差异了。

  1. [view@file diffutils]$ diff -q -B blank blank2
  2. Files blank and blank2 differ

1.4.匹配行(忽略)

   --ignore-matching-lines=regexp(-Iregexp)

    比如,写了一个脚本,在不同的环境下分别以“#/bin/sh”、“#/bin/bash”为首行。

  1. [view@file diffutils]$ diff -q blank blank2
  2. Files blank and blank2 differ
  3. [view@file diffutils]$ diff -q blank blank2 -I "^#"
  4. [view@file diffutils]$

1.5.字母大小写

  1. -i or --ignore-case

    忽略英文字母大小写。

2.输出格式


  例子文件

  1. # file t:
  2. The Nameless is the origin of Heaven and Earth;
  3. The named is the mother of all things.
  4.  
  5. Therefore let there always be non-being,
  6. so we may see their subtlety,
  7. And let there always be being,
  8. so we may see their outcome.
  9. The two are the same,
  10. But after they are produced,
  11. they have different names.
  12. They both may be called deep and profound.
  13. Deeper and more profound,
  14. The door of all subtleties!
  15.  
  16. # file l
  17. The Way that can be told of is not the eternal Way;
  18. The name that can be named is not the eternal name.
  19. The Nameless is the origin of Heaven and Earth;
  20. The Named is the mother of all things.
  21. Therefore let there always be non-being,
  22. so we may see their subtlety,
  23. And let there always be being,
  24. so we may see their outcome.
  25. The two are the same,
  26. But after they are produced,
  27. they have different names.

2.1.上下文显示

  上下文格式显示

  把上下文显示的格式保存为c,并在vim中同时打开来对比效果。

  1. 选项:--context[=lines] (-Clines) or -c
    默认:3
    输出上下文显示格式到文件
  1. [view@file diffutils]$ diff -c l t > c
  1. vim左侧打开上下部分分别打开lt;右侧打开c
  1. [view@file diffutils]$ vi -o l t
  2. :vsplit c
  3. Ctrl+W L
  4. Ctrl+w w
  5. Ctrl+w Ctrl+_
  6. Ctrl+w w
  7. Ctrl+w w
  8. Ctrl+w Ctrl+_

  下边以纯本本格式显示打开后的效果(单击图片可放大):

  第一行,前三列定义文件名称表示符号,以深绿色显示,分割代表文件名对应的文件内容。深黄色星号分割对比的区域。区域内,白色文字属于“上下文”。叹号开头的紫红色,表示差异部分。加号开头浅蓝色表示多出的差异部分。(代表文件名的星号第8行、与短横杠的第7行相同,不在c文件中显示。)

  百尺杆头,更近一步,把上下文设定为1行:

  1. [view@file diffutils]$ diff -C l t > c1
  2. [view@file diffutils]$ vi -o l t

2.2.统一显示格式

  使用选项: --unified[=lines](-Ulines), or-u;默认3行

  1. [view@file diffutils]$ diff -u l t
  2. --- l -- ::18.861252990 +
  3. +++ t -- ::30.759311361 +
  4. @@ -, +, @@
  5. -The Way that can be told of is not the eternal Way;
  6. -The name that can be named is not the eternal name.
  7. -The Nameless is the origin of Heaven and Earth;
  8. -The Named is the mother of all things.
  9. +meless is the origin of Heaven and Earth;
  10. +The named is the mother of all things.
  11. +
  12. Therefore let there always be non-being,
  13. so we may see their subtlety,
  14. And let there always be being,
  15. @@ -, +, @@
  16. The two are the same,
  17. But after they are produced,
  18. they have different names.
  19. +They both may be called deep and profound.
  20. +Deeper and more profound,
  21. +The door of all subtleties!

  前两行定义了怎么理解加、键号。-:l文件。+:t文件。缩进:相同内容。@开头,内容汇总信息,加减号于前边两行对应;9,3表示第9行往后3行。

2.3.并排显示

  两个文件的内容并排显示出来,使用选项:--side-by-side(-y)。可是使用 --width=columns(-Wcolumns) 指定显示宽度。

  1. [view@file diffutils]$ diff -y l t
  2. The Way that can be told of is n <
  3. The name that can be named is no <
  4. The Nameless is the origin of He The Nameless is the origin of He
  5. The Named is the mother of all t | The named is the mother of all t
  6. >
  7. Therefore let there always be no Therefore let there always be no
  8. so we may see their subtlety, so we may see their subtlety,
  9. And let there always be being, And let there always be being,
  10. so we may see their outcome. so we may see their outcome.
  11. The two are the same, The two are the same,
  12. But after they are produced, But after they are produced,
  13. they have different names. they have different names.
  14. > They both may be called deep and
  15. > Deeper and more profound,
  16. > The door of all subtleties!

2.4.默认输出

  默认输出,也称为一般输出,选项“--normal”为默认选项。

  1. [view@file diffutils]$ diff l t
  2. ,4c1,
  3. < The Way that can be told of is not the eternal Way;
  4. < The name that can be named is not the eternal name.
  5. < The Nameless is the origin of Heaven and Earth;
  6. < The Named is the mother of all things.
  7. ---
  8. > meless is the origin of Heaven and Earth;
  9. > The named is the mother of all things.
  10. >
  11. 11a11,
  12. > They both may be called deep and profound.
  13. > Deeper and more profound,
  14. > The door of all subtleties!

3.比较目录


  1. [view@file diffutils]$ ll
  2. total
  3. drwxrwxr-x work work Aug : dir1
  4. drwxrwxr-x work work Aug : dir2
  5. [view@file diffutils]$ tree
  6. .
  7. ├── dir1
  8. ├── l
  9. └── t
  10. └── dir2
  11. ├── l
  12. └── t
  13.  
  14. directories, files
  15. 文件是一致的时候,没有任何显示
  16. [view@file diffutils]$ diff dir1 dir2
  17. [view@file diffutils]$ rm -f dir1/l
  18. [view@file diffutils]$ diff dir1 dir2
  19. Only in dir2: l
  20. [view@file diffutils]$ touch dir1/l
  21. 当不同的文件是文本文件时,就比较俩文件内容
  22. [view@file diffutils]$ diff dir1 dir2
  23. diff dir1/l dir2/l
  24. 0a1,
  25. > The Way that can be told of is not the eternal Way;
  26. > The name that can be named is not the eternal name.
  27. > The Nameless is the origin of Heaven and Earth;
  28. > The Named is the mother of all things.
  29. > Therefore let there always be non-being,
  30. > so we may see their subtlety,
  31. > And let there always be being,
  32. > so we may see their outcome.
  33. > The two are the same,
  34. > But after they are produced,
  35. > they have different names.
  36. 把输出简化
  37. [view@file diffutils]$ diff -q dir1 dir2
  38. Files dir1/l and dir2/l differ
  39. 有差异时,不输出
  40. [work@file diffutils]$ diff -q dir1 dir2 >/dev/null
  41. [work@file diffutils]$ echo $?

  再看看二进制文件比较

  1. [view@file diffutils]$ diff bin1 bin2
  2. [view@file diffutils]$ mv bin1/ls bin1/lls
  3. [view@file diffutils]$ diff bin1 bin2
  4. Only in bin1: lls
  5. Only in bin2: ls
  1. 从结果看,比较二进制文件时,仅仅比较了文件名
  1. [view@file diffutils]$ echo $?

  比较两个目录时,如果包含子目录,会显示关于子目录的信息(目录亦即文件)。默认不递归比较,可以使用选项显式指定“-ror--recursive”。比较目录时,同样会关注其中文本文件的内容(不同时会显示出来)、二进制文件的名称(文件一致名称不同会显示不同文件)。

diffutils's diff的更多相关文章

  1. linux包之diff

    [root@84-monitor ~]# rpm -qf /usr/bin/diffdiffutils-2.8.1-28.el6.x86_64[root@84-monitor ~]# rpm -ql ...

  2. [Linux] diff命令:逐行进行文件比较

    1. 比较文件 $ diff file1 file2 2. 比较文件夹 $ diff -urNa dir1 dir2 -u, -U NUM, --unified[=NUM] output NUM (d ...

  3. 一个diff工具,用于判断两个目录下所有的改动(比较新旧版本文件夹)

    需求: 编写一个diff工具,用于判断两个目录下所有的改动 详细介绍: 有A和B两个目录,目录所在位置及层级均不确定 需要以B为基准找出两个目录中所有有改动的文件(文件或内容增加.修改.删除),将有改 ...

  4. git diff与linux diff的输出格式之unified format

    前言 前面有一篇文章<一个有些意思的项目--文件夹对比工具(一)>,里面简单讲了下diff算法之--Myers算法. 既然是算法,就会有实现,比如git diff中有Myers的实现,gi ...

  5. 虚拟dom与diff算法 分析

    好文集合: 深入浅出React(四):虚拟DOM Diff算法解析 全面理解虚拟DOM,实现虚拟DOM

  6. svn diff 详解

    UI版: 如果多人编辑同一段代码,常常容易出现冲突的情况: 如果出现冲突,我们如何解决他呢? 1 可以选择使用自己的文件mime file,也可以使用 他们的文件 their file 2 解决冲突, ...

  7. React Diff算法

    Web界面由DOM树来构成,当其中某一部分发生变化时,其实就是对应的某个DOM节点发生了变化.在React中,构建UI界面的思路是由当前状态决定界面.前后两个状态就对应两套界面,然后由React来比较 ...

  8. diff生成补丁与patch打补丁

    1.使用diff生成补丁: diff是Linux下的文件比较命令,参数这里就不说了,直接man一下就行了,不仅可以比较文件,也可以比较两个目录,并且可以将不同之处生成补丁文件,其实就是一种打补丁的命令 ...

  9. git diff 生成patch, git apply patch 打补丁方法说明,以及分支管理的简单操作。

    git diff 简易操作说明 先git log 查看commit ID, 记录你想要打的补丁的ID 比如说: git log commit 4ff35d800fa62123a28b7bda2a04e ...

随机推荐

  1. git中的分支管理

    /*游戏或者运动才能让我短暂的忘记心痛,现如今感觉学习比游戏和运动还重要——曾少锋*/ 如果对git基础不太熟悉的可以参考:http://www.cnblogs.com/zengsf/p/750621 ...

  2. [LeetCode&Python] Problem 463. Island Perimeter

    You are given a map in form of a two-dimensional integer grid where 1 represents land and 0 represen ...

  3. DoTween可视化编程用法详解

    DoTween可视化编辑 本文提供全流程,中文翻译.Chinar坚持将简单的生活方式,带给世人!(拥有更好的阅读体验 -- 高分辨率用户请根据需求调整网页缩放比例) Chinar -- 心分享.心创新 ...

  4. width百分比

    table中的td可以在页面中直接在元素上设置width:但是li不能只能在页面中写style: <!-- <li width="20%" class="p- ...

  5. Ubuntu配置操作总结

    一.fstab和mtab的区别 /etc/fstab:是开机自动挂载的配置文件,在开机时起作用.相当于启动linux的时候,自动使用检查分区的fsck命令和挂载分区的mount命令,检查分区和挂载分区 ...

  6. SQL Server获取指定行的数据

    SQL Server获取指定行(如第二行)的数据   --SQL Server获取指定行(如第二行)的数据-- --法一(对象法)-- select * from ( select * , numbe ...

  7. js 逻辑的短路运算

    && 与运算 同时为true,才为true: 表达式1为false,不用看表达式2: || 或运算 有一个为true,就为true: 表达式1为true,不用看表达式2: && ...

  8. windows python3 安装gittle

    1 从github clone gittle项目, git clone https://github.com/FriendCode/gittle.git 2 进行到下载的项目,执行安装 python ...

  9. linq to sql (Group By/Having/Count/Sum/Min/Max/Avg操作符) (转帖)

    http://wenku.baidu.com/link?url=2RsCun4Mum1SLbh-LHYZpTmGFMiEukrWAoJGKGpkiHKHeafJcx2y-HVttNMb1BqJpNdw ...

  10. svn 提交代码 自动过滤技巧,自动过滤不想提交的文件和文件夹

    原文:http://www.cnblogs.com/codealone/p/3208620.html 打开SVN客户端-----常规设置------全局忽略样式,添加 bin obj debug Re ...