• diffutils's diff


      比较文件的差异

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

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

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

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

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

     

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

     

    1.比较方法


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

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

     

    1.1.差异组

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

    # file1
    i
    love
    you
    # file2
    you
    love
    i

    1.2.空白符(忽略)

    1.2.1.-E

      --ignore-tab-expansion(-E)

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

    [view@file diffutils]$ diff -q blank blank2
    Files blank and blank2 differ
    [view@file diffutils]$ diff -q -E blank blank2
    [view@file diffutils]$

    1.2.2.-Z

      --ignore-trailing-space(-Z)

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

    [view@file diffutils]$ diff -q blank blank2
    Files blank and blank2 differ
    [view@file diffutils]$ diff -q -Z blank blank2
    [view@file diffutils]$

    1.2.3.-b

      --ignore-space-change(-b)

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

    [view@file diffutils]$ diff -q blank blank2
    Files blank and blank2 differ
    [view@file diffutils]$ diff -q -E blank blank2
    Files blank and blank2 differ
    [view@file diffutils]$ diff -q -Z blank blank2
    Files blank and blank2 differ
    [view@file diffutils]$ diff -q -b blank blank2
    [view@file diffutils]$

    1.2.4.-w

      --ignore-all-space(-w)

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

    [view@file diffutils]$ diff -q -b blank blank2
    Files blank and blank2 differ
    [view@file diffutils]$ diff -q -w blank blank2
    [view@file diffutils]$

     

    1.3.空白行(忽略)

    --ignore-blank-lines(-B)

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

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

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

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

    1.4.匹配行(忽略)

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

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

    [view@file diffutils]$ diff -q blank blank2
    Files blank and blank2 differ
    [view@file diffutils]$ diff -q blank blank2 -I "^#"
    [view@file diffutils]$

    1.5.字母大小写

    -i or --ignore-case

        忽略英文字母大小写。

    2.输出格式


      例子文件

    # file t:
    The Nameless is the origin of Heaven and Earth;
    The named is the mother of all things.
    
    Therefore let there always be non-being,
      so we may see their subtlety,
    And let there always be being,
      so we may see their outcome.
    The two are the same,
    But after they are produced,
      they have different names.
    They both may be called deep and profound.
    Deeper and more profound,
    The door of all subtleties!
    
    # file l
    The Way that can be told of is not the eternal Way;
    The name that can be named is not the eternal name.
    The Nameless is the origin of Heaven and Earth;
    The Named is the mother of all things.
    Therefore let there always be non-being,
      so we may see their subtlety,
    And let there always be being,
      so we may see their outcome.
    The two are the same,
    But after they are produced,
      they have different names.

    2.1.上下文显示

      上下文格式显示

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

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

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

    wKiom1mK0ROQ9PlgAADbsrgbVNQ405.png

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

     

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

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

    wKioL1mK272wbOMLAADKJ6mfLhg815.png

    2.2.统一显示格式

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

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

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

    2.3.并排显示

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

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

    2.4.默认输出

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

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

    3.比较目录


    [view@file diffutils]$ ll
    total 8
    drwxrwxr-x 2 work work 4096 Aug 10 18:04 dir1
    drwxrwxr-x 2 work work 4096 Aug 10 18:05 dir2
    [view@file diffutils]$ tree
    .
    ├── dir1
    │   ├── l
    │   └── t
    └── dir2
        ├── l
        └── t
    
    2 directories, 4 files
    文件是一致的时候,没有任何显示
    [view@file diffutils]$ diff dir1 dir2
    [view@file diffutils]$ rm -f dir1/l
    [view@file diffutils]$ diff dir1 dir2
    Only in dir2: l
    [view@file diffutils]$ touch dir1/l
    当不同的文件是文本文件时,就比较俩文件内容
    [view@file diffutils]$ diff dir1 dir2
    diff dir1/l dir2/l
    0a1,11
    > The Way that can be told of is not the eternal Way;
    > The name that can be named is not the eternal name.
    > The Nameless is the origin of Heaven and Earth;
    > The Named is the mother of all things.
    > Therefore let there always be non-being,
    >   so we may see their subtlety,
    > And let there always be being,
    >   so we may see their outcome.
    > The two are the same,
    > But after they are produced,
    >   they have different names.
    把输出简化
    [view@file diffutils]$ diff -q dir1 dir2
    Files dir1/l and dir2/l differ
    有差异时,不输出
    [work@file diffutils]$ diff -q dir1 dir2 1>/dev/null
    [work@file diffutils]$ echo $?
    1

      再看看二进制文件比较

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

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

    一切代码都是为了生活,一切生活都是调剂
  • 相关阅读:
    linux recv 返回值与linux socket 错误分析
    位域
    mysql修改root密码的方法
    mysql Plugin ‘InnoDB’ init function returned error
    centos查看版本
    CentOS 7.0 使用 yum 安装 MariaDB 及 简单配置
    CentOS 7.X 中systemctl命令用法详解
    phpMyAdmin关于PHP 5.5+ is required. Currently installed version is: 5.4.16问题
    linux下tar.gz、tar、bz2、zip等解压缩、压缩命令小结
    自动设置IP地址bat脚本
  • 原文地址:https://www.cnblogs.com/argor/p/7908792.html
Copyright © 2020-2023  润新知