diff命令用法举例

diff命令是linux上非常重要的工具,用于比较文件的内容,特别是比较两个版本不同的文件以找到改动的地方。
diff是svn、cvs、git等版本控制工具不可或缺的一部分。

1.命令格式:diff[参数][文件1或目录1][文件2或目录2]

2.命令功能:
diff命令能比较单个文件或者目录内容。如果指定比较的是文件,则只有当输入为文本文件时才有效。以逐行的方式,比较文本文件的异同处。
如果指定比较的是目录的的时候,diff 命令会比较两个目录下名字相同的文本文件。列出不同的二进制文件、公共子目录和只在一个目录出现的文件。

3.命令参数:
-                                指定要显示多少行的文本。此参数必须与-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参数时,仅显示不同之处。

4.使用实例:
[moiaopr@CNSZ443239 ~]$ cat test01.txt
I need to buy apples.
I need to run the laundry.
I need to wash the dog.
I need to get the car detailed.
[moiaopr@CNSZ443239 ~]$ cat test02.txt
I need to buy apples.
I need to do the laundry.
I need to wash the car.
I need to get the dog detailed.

4.1 比较两个文件
[moiaopr@CNSZ443239 ~]$ diff test01.txt test02.txt
2,4c2,4
< I need to run the laundry.
< I need to wash the dog.
< I need to get the car detailed.
---
> I need to do the laundry.
> I need to wash the car.
> I need to get the dog detailed.

说明: diff描述两个文件不同的方式是告诉我们怎么样改变第一个文件之后与第二个文件匹配。
我们看看上面的比较结果中的第一行 2,4c2,4 前面的数字2,4表示第一个文件中的行,中间有一个字母c表示需要在第一个文件上做的操作(a=add,c=change,d=delete),后面的数字2,4表示第二个文件中的行。
2,4c2,4 的含义是:第一个文件中的第[2,4]行(注意这是一个闭合区间,包括第2行和第4行)需要做出修改才能与第二个文件中的[2,4]行相匹配。
前面带<的部分表示左边文件的第[2,4]行的内容,而带>的部分表示右边文件的第[2,4]行的内容,中间的 --- 则是两个文件内容的分隔符号。

diff 的normal 显示格式有三种提示:
a - add
c - change
d - delete 

4.2 并排格式输出, 命令:  -y (并排显示)  -W(指定行宽)
[moiaopr@CNSZ443239 ~]$ diff test01.txt test02.txt -y -W 100
I need to buy apples.                           I need to buy apples.
I need to run the laundry.                    | I need to do the laundry.
I need to wash the dog.                       | I need to wash the car.
I need to get the car detailed.               | I need to get the dog detailed.

说明:
"|"表示前后2个文件内容有不同
"<"表示后面文件比前面文件少了1行内容
">"表示后面文件比前面文件多了1行内容

4.3 上下文输出格式, 命令: -c
[moiaopr@CNSZ443239 ~]$ diff test01.txt test02.txt -c
*** test01.txt  2016-11-17 15:48:28.543490644 +0800
--- test02.txt  2016-11-17 15:48:53.540609768 +0800
***************
*** 1,4 ****
  I need to buy apples.
! I need to run the laundry.
! I need to wash the dog.
! I need to get the car detailed.
--- 1,4 ----
  I need to buy apples.
! I need to do the laundry.
! I need to wash the car.
! I need to get the dog detailed.

说明:
这种方式在开头两行作了比较文件的说明,这里有三中特殊字符:
"+" 比较的文件的后者比前着多一行
"-" 比较的文件的后者比前着少一行
"!" 比较的文件两者有差别的行

4.4 统一格式输出, 命令: -u
[moiaopr@CNSZ443239 ~]$ diff test01.txt test02.txt -u
--- test01.txt  2016-11-17 15:48:28.543490644 +0800
+++ test02.txt  2016-11-17 15:48:53.540609768 +0800
@@ -1,4 +1,4 @@
 I need to buy apples.
-I need to run the laundry.
-I need to wash the dog.
-I need to get the car detailed.
+I need to do the laundry.
+I need to wash the car.
+I need to get the dog detailed.

说明:
它的第一部分,也是文件的基本信息:
--- test01.txt  2016-11-17 15:48:28.543490644 +0800
+++ test02.txt  2016-11-17 15:48:53.540609768 +0800
"---"表示变动前的文件,"+++"表示变动后的文件。

第二部分,变动的位置用两个@作为起首和结束。
@@ -1,4 +1,4 @@

前面的"-1,4"分成三个部分:减号表示第一个文件(test01.txt),"1"表示第1行,"4"表示连续4行。合在一起,就表示下面是第一个文件从第1行开始的连续4行。
同样的,"+1,4"表示变动后,成为第二个文件从第1行开始的连续4行。

4.5 比较两个文件不同,并生产补丁, 命令: -ruN > patch.log
[moiaopr@CNSZ443239 ~]$ diff -ruN test01.txt test02.txt >patch.log
[moiaopr@CNSZ443239 ~]$ cat patch.log
--- test01.txt  2016-11-17 15:48:28.543490644 +0800
+++ test02.txt  2016-11-17 15:48:53.540609768 +0800
@@ -1,4 +1,4 @@
 I need to buy apples.
-I need to run the laundry.
-I need to wash the dog.
-I need to get the car detailed.
+I need to do the laundry.
+I need to wash the car.
+I need to get the dog detailed.

4.6 打补丁:  执行完毕后两个文件内容相同
[moiaopr@CNSZ443239 ~]$ cat test01.txt 
I need to buy apples.
I need to run the laundry.
I need to wash the dog.
I need to get the car detailed.
[moiaopr@CNSZ443239 ~]$ diff -ruN test01.txt test02.txt >patch.log
[moiaopr@CNSZ443239 ~]$ patch test01.txt patch.log
patching file test01.txt
[moiaopr@CNSZ443239 ~]$ cat test01.txt
I need to buy apples.
I need to do the laundry.
I need to wash the car.
I need to get the dog detailed.

猜你喜欢

转载自blog.csdn.net/qq646748739/article/details/81182673