diff命令具体解释

diff命令參数:
diff - 找出两个文件的不同点
总览
diff [选项] 源文件 目标文件
以下是 GNU所接受的 diff 的全部选项的概要. 大多数的选项有两个同样的名字,一个是单个的跟在 - 后面字母, 还有一个是由 – 引出的长名字. 多个单字母选项(除非它们产生歧义)能够组合为单行的命令行语法 -ac 是等同于 -a -c. 长命名的选项能被缩短到他们的名字的不论什么唯一的前缀. 用 ([ 和 ]) 括起来显示选项产生歧义的选项
-行数(一个整数)
显示上下文 行数 (一个整数). 这个选项自身没有指定输出格式。这是没有效果的。除非和 -c 或者 -u 组合使用. 这是已废置的选项,对于正确的操作, 上下文至少要有两行。
-a
全部的文件都视为文本文件来逐行比較,甚至他们似乎不是文本文件.
-b
忽略空格引起的变化.
-B
忽略插入删除空行引起的变化.
–brief
仅报告文件是否相异,在乎区别的细节.
-c
使用上下文输出格式.
-C 行数(一个整数)

–context[=lines]
使用上下文输出格式,显示以指定 行数 (一个整数), 或者是三行(当 行数 没有给出时. 对于正确的操作, 上下文至少要有两行.
–changed-group-format=format
使用 format 输出一组包括两个文件的不同处的行,其格式是 if-then-else .
-d
改变算法或许发现变化的一个更小的集合.这会使 diff 变慢 (有时更慢).
-D name
合并 if-then-else 格式输出, 预处理宏(由name參数提供)条件.
-e

–ed
输出为一个有效的 ed 脚本.
–exclude=pattern
比較文件夹的时候。忽略和文件夹中与 pattern(样式) 相配的.
–exclude-from=file
比較文件夹的时候,忽略和文件夹中与不论什么包括在 file(文件) 的样式相配的文件和文件夹.
–expand-tabs
在输出时扩展tab为空格,保护输入文件的tab对齐方式
-f
产生一个非常象 ed 脚本的输出,可是可是在他们在文件出现的顺序有改变
-F regexp
在上下文和统一格式中。对于每一大块的不同。显示出匹配 regexp. 的一些前面的行.
–forward-ed
产生象 ed 脚本的输出,可是它们在文件出现的顺序有改变。
-h
这选项如今已没作用,它呈现Unix的兼容性.
-H
使用启示规则加速操作那些有很多离散的小差异的大文件.
–horizon-lines=lines
比較给定行数的有共同前缀的最后行,和有共同或缀的最前行.
-i
忽略大写和小写.
-I regexp
忽略由插入,删除行(由regexp 參数提供參考)带来的改变.
–ifdef=name
合并 if-then-else 格式输出, 预处理宏(由name參数提供)条件.
–ignore-all-space
在比較行的时候忽略空白.
–ignore-blank-lines
忽略插入和删除空行
–ignore-case
忽略大写和小写.
–ignore-matching-lines=regexp
忽略插入删除行(由regexp 參数提供參考).
–ignore-space-change
忽略空白的数量.
–initial-tab
在文本行(不管是常规的或者格式化的前后文关系)前输出tab取代空格. 引起的原因是tab对齐方式看上去象是常规的一样.
-l
产生通过 pr 编码的输出.
-L label

–label=label
使用 label 给出的字符在文件头取代文件名称输出.
–left-column
以并列方式印出两公共行的左边
–line-format=format
使用 format 输出全部的行,在 if-then-else 格式中.
–minimal
改变算法或许发现变化的一个更小的集合.这会使 diff 变慢 (有时更慢).
-n
输出 RC-格式 diffs; 除了每条指令指定的行数受影响外 象 -f 一样。
-N

–new-file
在文件夹比較中,假设那个文件仅仅在当中的一个文件夹中找到。那么它被视为在还有一个文件夹中是一个空文件.
–new-group-format=format
使用 format 以if-then-else 格式输出仅仅在第二个文件里取出的一个行组
–new-line-format=format
使用 format 以if-then-else 格式输出仅仅在第二个文件里取出的一行
–old-group-format=format
使用 format 以if-then-else 格式输出仅仅在第一个文件里取出的一个行组
–old-line-format=format
使用 format 使用 format 以if-then-else 格式输出仅仅在第一个文件里取出的一行
-p
显示带有c函数的改变.
-P
在文件夹比較中,假设那个文件仅仅在当中的一个文件夹中找到,那么它被视为在还有一个文件夹中是一个空文件.
–paginate
产生通过 pr 编码的输出.
-q
仅报告文件是否相异,不报告具体的差异.
-r
当比較文件夹时。递归比較不论什么找到的子文件夹.
–rcs
输出 RC-格式 diffs; 除了每条指令指定的行数受影响外 象 -f 一样。
–recursive
当比較文件夹时,递归比較不论什么找到的子文件夹.
–report-identical-files

-s
报告两个文件同样.
-S file
当比較文件夹时。由 file 開始. 这用于继续中断了的比較.
–sdiff-merge-assist
打印附加的信息去帮助 sdiff. sdiff 在执行 diff 时使用这些选项. 这些选项不是特意为使用者直接使用而准备的。
–show-c-function
显示带有c函数的改变.
–show-function-line=regexp
在上下文和统一的格式,对于每一大块的区别,显示出匹配 regexp. 的一些前面的行
–side-by-side
使用并列的输出格式.
–speed-large-files
使用启示规则加速操作那些有很多离散的小差异的大文件.
–starting-file=file
当比較文件夹时。由 file 開始. 这用于继续中断了的比較.
–suppress-common-lines
在并列格式中不印出公共行。
-t
在输出时扩展tab为空格,保护输入文件的tab对齐方式
-T
在文本行(不管是常规的或者格式化的前后文关系)前输出tab取代空格.引起的原因是tab对齐方式看上去象是常规的一样.
–text
全部的文件都视为文本文件来逐行比較。甚至他们似乎不是文本文件.
-u
使用统一的输出格式.
–unchanged-group-format=format
使用 format 输出两个文件的公共行组,其格式是if-then-else.
–unchanged-line-format=format
使用 format 输出两个文件的公共行,其格式是if-then-else.
–unidirectional-new-file
在文件夹比較中。假设那个文件仅仅在当中的一个文件夹中找到。那么它被视为在还有一个文件夹中是一个空文件.
-U lines

–unified[=lines]
使用前后关系格式输出,显示以指定 行数 (一个整数), 或者是三行(当 行数 没有给出时. 对于正确的操作, 上下文至少要有两行.
-v

–version
输出 diff 版本.
-w
在比較行时忽略空格
-W columns

–width=columns
在并列格式输出时,使用指定的列宽.
-x pattern
比較文件夹的时候,忽略和文件夹中与 pattern(样式) 相配的.
-X file
比較文件夹的时候。忽略和文件夹中与不论什么包括在 file(文件) 的样式相配的文件和文件夹.
-y
使用并列格式输出

实例:

有这样两个文件:
程序清单1 :hello.c

include

int main(void)
{
char msg[] = “Hello world!”;

puts(msg);
printf("Welcome to use diff commond.
");

return 0;    

}
程序清单2:hello_diff.c

include

include

int main(void)
{
char msg[] = “Hello world,fome hello_diff.c”;

puts(msg);
printf("hello_diff.c says,'Here you are,using diff.'
");

return 0;    

}
我们使用diff命令来查看这两个文件的不同之处。有一下几种方便的方法:
1、普通格式输出:
[root@localhost diff]# diff hello.c hello_diff.c
1a2

include

5c6
char msg[] = “Hello world,fome hello_diff.c”;
8c9
printf(“hello_diff.c says,’Here you are,using diff.’ ”);
[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 #include

                                                          > #include 

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. ”); | 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

int main(void)
{
! char msg[] = “Hello world!”;

    puts(msg);

! printf(“Welcome to use diff commond. ”);

    return 0;

}
— 1,12 —-
#include
+ #include

int main(void)
{
! char msg[] = “Hello world,fome hello_diff.c”;

    puts(msg);

! printf(“hello_diff.c says,’Here you are,using diff.’ ”);

    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

+#include

int main(void)
{
- char msg[] = “Hello world!”;
+ char msg[] = “Hello world,fome hello_diff.c”;

    puts(msg);

- printf(“Welcome to use diff commond. ”);
+ printf(“hello_diff.c says,’Here you are,using diff.’ ”);

    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. ”);

    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.’ ”);

    return 0;

}
[root@localhost diff]#
这里通过“ -I include”选项来忽略带有“ include”字样的行

部分内容来自:http://blog.chinaunix.net/u1/48325/showart_433936.html
http://hi.baidu.com/ilotus_y/blog/item/b94231d6f55a1e2a07088b91.html

原文地址:https://www.cnblogs.com/liguangsunls/p/6999965.html