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. zabbix监控sockets连接数

    配置zabbix客户端配置文件 vim /etc/zabbix/zabbix_agentd.conf 添加  Include=/etc/zabbix/zabbix_agentd.d/ 添加脚本对soc ...

  2. idea 破解转(肉测好用,测试2018.4.16)

    首先,打开蓝雨的官网--->http://idea.lanyus.com/,找到这个jar包 之后,去官网下载IDEA--->https://www.jetbrains.com/idea/ ...

  3. Keepalived + MySQLfailover + GTIDs 高可用

    架构图     10.1.1.207    mysql master + keepalived     10.1.1.206    mysql slave ( backup master ) + ke ...

  4. VSCode调试.net core 2.0 输出窗口乱码

    Q:输出窗口乱码 A:修改.vscode文件夹下,tasks.json文件,具体内容见图

  5. Google Code Jam 2014 Round 1 A:Problem C. Proper Shuffle

    Problem A permutation of size N is a sequence of N numbers, each between 0 and N-1, where each numbe ...

  6. Nginx+tomcat集群中,session的共享

    nginx,tomcat集群后多个session分配到同一个应用 单节点低负荷的情况下,我们通常把一个WEB应用打成WAR包放WEB应用服务器,如TOMCAT下运行就行了(如图1).但随着用户量的增加 ...

  7. PHP面试题总结

    2017年5月15日19:20:26 1.请用最简单的语言告诉我PHP是什么? PHP全称:Hypertext Preprocessor,是一种用来开发动态网站的服务器脚本语言. 2. 面试题地址:h ...

  8. PHPUnit_Framework_Assert单元测试

    先发下简书的干货: 教你一步一步写一个phpunit testcase:https://www.jianshu.com/p/ba6829a6f3ec 程序地址 https://github.com/y ...

  9. 看了就很快学会jQuery

    一.jQuery简介与第一个jQuery程序 1.1.jQuery简介 1.2.jQuery特点 1.3.jQuery版本 1.4.获得jQuery库 1.5.第一个jQuery程序 二.jQuery ...

  10. [Sdoi2013]直径(树的直径)

    //36分 #include<cstdio> #include<cstdlib> #include<cstring> #include<ctime> # ...