diff命令比较两个文件的不同

本文详细介绍了Linux下diff命令的使用方法及各种格式输出示例,包括普通格式、并排格式、上下文格式和统一格式输出,适合初学者快速掌握diff命令的使用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

刚刚发现realarm 板子的version不同,硬件配置不同。冤啊。

于是,想搞清楚有何不同:

http://www.google.com.hk/search?sourceid=chrome&ie=UTF-8&q=realarm%E6%9D%BF%E5%AD%90%E7%9A%84version

 

偶然发现,原来华天正以前做2410的板子,只是没有6410这么火啊。

看到了他的2410说明书,arm32的网址,也换了主人啊。

不过,服务还是挺周到的,后面有一些linux 的命令。我竟然还没用过。

===============

diff,既可以比较两个文件,也可以比较两个目录中同名字文件。


 

================

http://www.huanxiangwu.com/241/linux-diff命令详解

一.简介:

比较文件的差异。

二.用法:

diff [-abBcdefHilnNpPqrstTuvwy][-<行数>][-C <行数>][-D <巨集名称>][-I <字符或字符串>][-S <文件>][-W <宽度>][-x <文件或目录>][-X <文件>][--help][--left-column][--suppress-common-line][文件或目录1][文件或目录 2]

补充说明:diff以逐行的方式,比较文本文件的异同处。所是指定要比较目录,则diff会比较目录中相同文件名的文件,但不会比较其中子目录。

选项说明:

 -<行数>  指定要显示多少行的文本。此参数必须与-c或-u参数一并使用。

 -a或–text  diff预设只会逐行比较文本文件。

 -b或–ignore-space-change  不检查空格字符的不同。

 -B或–ignore-blank-lines  不检查空白行。

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

 -C<行数>或–context<行数>  与执行”-c-<行数>“指令相同。

 -d或–minimal  使用不同的演算法,以较小的单位来做比较。

 -D<巨集名称>或ifdef<巨集名称>  此参数的输出格式可用于前置处理器巨集。

 -e或–ed  此参数的输出格式可用于ed的script文件。

 -f或-forward-ed  输出的格式类似ed的script文件,但按照原来文件的顺序来显示不同处。

 -H或–speed-large-files  比较大文件时,可加快速度。

 -l<字符或字符串>或–ignore-matching-lines<字符或字符串>  若两个文件在某几行有所不同,而这几行同时都包含了选项中指定的字符或字符串,则不显示这两个文件的差异。

 -i或–ignore-case  不检查大小写的不同。

 -l或–paginate  将结果交由pr程序来分页。

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

 -N或–new-file  在比较目录时,若文件A仅出现在某个目录中,预设会显示:Only in目录:文件A若使用-N参数,则diff会将文件A与一个空白的文件比较。

 -p  若比较的文件为C语言的程序码文件时,显示差异所在的函数名称。

 -P或–unidirectional-new-file  与-N类似,但只有当第二个目录包含了一个第一个目录所没有的文件时,才会将这个文件与空白的文件做比较。

 -q或–brief  仅显示有无差异,不显示详细的信息。

 -r或–recursive  比较子目录中的文件。

 -s或–report-identical-files  若没有发现任何差异,仍然显示信息。

 -S<文件>或–starting-file<文件>  在比较目录时,从指定的文件开始比较。

 -t或–expand-tabs  在输出时,将tab字符展开。

 -T或–initial-tab  在每行前面加上tab字符以便对齐。

 -u,-U<列数>或–unified=<列数>  以合并的方式来显示文件内容的不同。

 -v或–version  显示版本信息。

 -w或–ignore-all-space  忽略全部的空格字符。

 -W<宽度>或–width<宽度>  在使用-y参数时,指定栏宽。

 -x<文件名或目录>或–exclude<文件名或目录>  不比较选项中所指定的文件或目录。

 -X<文件>或–exclude-from<文件>  您可以将文件或目录类型存成文本文件,然后在=<文件>中指定此文本文件。

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

 –help  显示帮助。

 –left-column  在使用-y参数时,若两个文件某一行内容相同,则仅在左侧的栏位显示该行内容。

 –suppress-common-lines  在使用-y参数时,仅显示不同之处

三.示例:

diff /etc/named.conf.rpmnew /etc/named.conf

diff -u 1.txt 2.txt 显示diff统一格式输出,统一格式会在报告的每个改动块前后加上3行上下文(修改日期跟时间等信息)并用+和-给出两个文件的差异

使用sdiff命令可以使用另外一种查看方式,左右并排显示两个文件的输出并做对比

diff -u 1.txt 2.txt > patchfile.txt 生产两个文件的补丁

patch 1.txt < patchfile.txt 将补丁文件应用到1.txt文件

=========================

http://blogold.chinaunix.net/u/16292/showart_389843.html

摘要:本文详细介绍了diff命令的基本用法

 作者:zieckey (zieckey@yahoo.com.cn)
    All Rights Reserved!

有这样两个文件:

程序清单1 :hello.c

#include <stdio.h>

int main(void)
{
    char msg[] = "Hello world!";
    
    puts(msg);
    printf("Welcome to use diff commond./n");
    
    return 0;    
}


程序清单2:hello_diff.c

#include <stdio.h>
#include <stdlib.h>

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 <stdlib.h>
5c6
<       char msg[] = "Hello world!";
---
>       char msg[] = "Hello world,fome hello_diff.c";
8c9
<       printf("Welcome to use diff commond./n");
---
>       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
#include <stdio.h>                                              #include <stdio.h>
                                                              > #include <stdlib.h>

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]# 
这种并排格式的对比一目了然,可以快速找到不同的地方。
-W选择可以指定输出列的宽度,这里指定输出列宽为130

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 <stdio.h>
  
  int main(void)
  {
!       char msg[] = "Hello world!";
  
        puts(msg);
!       printf("Welcome to use diff commond./n");
  
        return 0;
  }
--- 1,12 ----
  #include <stdio.h>
+ #include <stdlib.h>
  
  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 <stdio.h>
+#include <stdlib.h>
 
 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、其他
假如你想查看两个文件是否不同又不想显示差异之处的话,可以加上-q选项:
[root@localhost diff]# diff hello.c hello_diff.c -q
Files hello.c and hello_diff.c differ
[root@localhost diff]# 另外你还可以提供一些匹配规则来忽略某中差别,可以用 -I regexp

[root@localhost diff]# diff hello.c hello_diff.c -c -I include
*** 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]# 

这里通过“ -I include”选项来忽略带有“ include”字样的行 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

等风来不如迎风去

你的鼓励是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值