diff命令是Linux中非常重要的工具,用于比较文件的内容,特别是比较两个版本不同的文件以找到改动的地方。diff在命令行中打印每一行的改动。最新版本的diff还支持二进制文件。diff程序的输出被称为补丁(patch),因为Linux系统中还有一个patch程序,可以根据diff的输出将a.c的文件内容更新为b.c。diff是svn,cvs,git等版本控制工具中不可缺少的一部分。
【文件格式】
diff【参数】【文件1或者目录1】【文件2或者目录2】
【命令功能】
diff命令能比较单个文件或者目录内容。如果指定比较的是文件,则只有输入为文本文件的时候才有效。以逐行的方式,比较文本文件的异同处。如果指定比较的是目录的时候,diff命令会比较两个目录下名字相同的文件。列出不同的二进制文件、公共子目录和只在一个目录中出现的文件。
【使用实例】
(1)
[root@centos65 liumengyang]# diff test001.txt test.txt
2a3
> My English is very good!
[root@centos65 liumengyang]#
[root@centos65 liumengyang]#
[root@centos65 liumengyang]#
[root@centos65 liumengyang]# cat test.txt
hello world
nice to meet you!
My English is very good!
[root@centos65 liumengyang]#
[root@centos65 liumengyang]# diff test.txt test001.txt
3d2
< My English is very good!
上面的“3d2”表示第一个文件比第二个文件多了第三行。
diff的normal显示格式有三种提示:
a --add
c --change
d --delete
(2)
并排格式输出
[root@centos65 liumengyang]#
[root@centos65 liumengyang]# diff -y -W 50 test.txt test001.txt
hello world hello world
nice to meet you! nice to meet you!
My English is very go <
[root@centos65 liumengyang]#
[root@centos65 liumengyang]# diff -y -W 50 test.txt test001.txt
hello world hello world
nice to meet you! nice to meet you!
My English is very go | My Japanese is not ve
[root@centos65 liumengyang]#
说明:
"|"表示前后两个文件内容有所不同
"<"表示后面文件比前面文件少了一行
">"表示前面文件比后面文件少了一行
注:并排输出的时候,需要指明宽度
(3)
上下文输出格式
[root@centos65 liumengyang]# diff -c test.txt test001.txt
*** test.txt 2015-09-30 13:54:44.499154418 +0800
--- test001.txt 2015-09-30 14:02:58.033338911 +0800
***************
*** 1,3 ****
hello world
nice to meet you!
! My English is very good!
--- 1,3 ----
hello world
nice to meet you!
! My Japanese is not very good!
[root@centos65 liumengyang]# diff -c test.txt test001.txt
*** test.txt 2015-09-30 14:13:55.675565551 +0800
--- test001.txt 2015-09-30 14:02:58.033338911 +0800
***************
*** 1,2 ****
--- 1,3 ----
hello world
nice to meet you!
+ My Japanese is not very good!
说明:
这里有三种特殊字符:
“+” 比较的文件的后者比前者多一行
“-” 比较的文件的后者比前者少一行
“!” 比较的两个文件两者直接内容有差别
(5)
比较文件夹的不同
[root@centos65 liumengyang]#
[root@centos65 liumengyang]#
[root@centos65 liumengyang]# diff test001 test002
diff test001/test001.txt test002/test001.txt
3c3
< My English is not very good!
---
> My Japanese is not very good!
Only in test002: test.txt
[root@centos65 liumengyang]#
[root@centos65 liumengyang]#
(6)
比较两个文件的不同,并且产生补丁
[root@centos65 test002]# diff -ruN 001.txt 002.txt > patch.log
[root@centos65 test002]#
[root@centos65 test002]#
[root@centos65 test002]# ll
total 20
-rw-r--r-- 1 root root 18 Sep 30 14:56 001.txt
-rw-r--r-- 1 root root 18 Sep 30 14:56 002.txt
-rw-r--r-- 1 root root 140 Sep 30 15:03 patch.log
-rw-r--r-- 1 root root 60 Sep 30 14:30 test001.txt
-rw-r--r-- 1 root root 32 Sep 30 14:52 test.txt
[root@centos65 test002]#
[root@centos65 test002]#
[root@centos65 test002]# cat patch.log
--- 001.txt 2015-09-30 14:56:07.622939973 +0800
+++ 002.txt 2015-09-30 14:56:25.199620806 +0800
@@ -1,3 +1,3 @@
00000
00000
-00000
+11111
[root@centos65 test002]#
(7)
打补丁
[root@centos65 test002]# patch patch.log 002.txt
patch: **** Only garbage was found in the patch input.
[root@centos65 test002]# patch 002.txt patch.log
patching file 002.txt
Reversed (or previously applied) patch detected! Assume -R? [n] y
[root@centos65 test002]# ll
total 24
-rw-r--r-- 1 root root 18 Sep 30 14:56 001.txt
-rw-r--r-- 1 root root 18 Sep 30 15:05 002.txt
-rw-r--r-- 1 root root 18 Sep 30 14:56 002.txt.orig
-rw-r--r-- 1 root root 140 Sep 30 15:03 patch.log
-rw-r--r-- 1 root root 60 Sep 30 14:30 test001.txt
-rw-r--r-- 1 root root 32 Sep 30 14:52 test.txt
[root@centos65 test002]#
[root@centos65 test002]# cat 002.txt
00000
00000
00000
[root@centos65 test002]# cat 002.txt.orig
00000
00000
11111
[root@centos65 test002]#