shell diff
来源:百度文库 编辑:神马文学网 时间:2024/05/02 02:35:21
diff命令的使用
摘要:本文详细介绍了diff命令的基本用法,diff以逐行的方式,比较文本文件的异同处。所是指定要比较目录,则diff会比较目录中相同文件名的文件,但不会比较其中子目录。 有这样两个文件:
程序清单1 :hello.c
#include
int main(void)
{
char msg[] = "Hello world!";
puts(msg);
printf("Welcome to use diff commond.\n");
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.'\n");
return 0;
}
我们使用diff命令来查看这两个文件的不同之处,有一下几种方便的方法:
1、普通格式输出:
[root@localhost diff]# diff hello.c hello_diff.c
1a2
> #include
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 //-y以并列的方式显示文件的异同之处;-W<宽度>或--width<宽度> 在使用-y参数时,指定栏宽
#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.\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 //-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.\n");
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.'\n");
return 0;
}
[root@localhost diff]#
这种方式在开头两行作了比较文件的说明,这里有三中特殊字符:
+ 比较的文件的后者比前着多一行
- 比较的文件的后者比前着少一行
! 比较的文件两者有差别的行
4、统一输出格式
[root@localhost diff]# diff hello.c hello_diff.c -u //-u,-U<列数>或--unified=<列数> 以合并的方式来显示文件内容的不同。
--- 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.\n");
+ printf("hello_diff.c says,'Here you are,using diff.'\n");
return 0;
}
[root@localhost diff]#
正如看到的那样,统一格式的输出更加紧凑,所以更易于理解,更易于修改。
5、其他
假如你想查看两个文件是否不同又不想显示差异之处的话,可以加上-q选项://-q或--brief 仅显示有无差异,不显示详细的信息。
[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”字样的行
若要了解diff 各个选项的含义,请参考http://www.isstudy.com/linux/387.html;这才是真正的简洁,不用把一些无关紧要的东西加入进来,不要把自己当作字典一样进行储存;觉得这样才是学命令的方法;
摘要:本文详细介绍了diff命令的基本用法,diff以逐行的方式,比较文本文件的异同处。所是指定要比较目录,则diff会比较目录中相同文件名的文件,但不会比较其中子目录。 有这样两个文件:
程序清单1 :hello.c
#include
int main(void)
{
char msg[] = "Hello world!";
puts(msg);
printf("Welcome to use diff commond.\n");
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.'\n");
return 0;
}
我们使用diff命令来查看这两个文件的不同之处,有一下几种方便的方法:
1、普通格式输出:
[root@localhost diff]# diff hello.c hello_diff.c
1a2
> #include
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 //-y以并列的方式显示文件的异同之处;-W<宽度>或--width<宽度> 在使用-y参数时,指定栏宽
#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.\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 //-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.\n");
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.'\n");
return 0;
}
[root@localhost diff]#
这种方式在开头两行作了比较文件的说明,这里有三中特殊字符:
+ 比较的文件的后者比前着多一行
- 比较的文件的后者比前着少一行
! 比较的文件两者有差别的行
4、统一输出格式
[root@localhost diff]# diff hello.c hello_diff.c -u //-u,-U<列数>或--unified=<列数> 以合并的方式来显示文件内容的不同。
--- 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.\n");
+ printf("hello_diff.c says,'Here you are,using diff.'\n");
return 0;
}
[root@localhost diff]#
正如看到的那样,统一格式的输出更加紧凑,所以更易于理解,更易于修改。
5、其他
假如你想查看两个文件是否不同又不想显示差异之处的话,可以加上-q选项://-q或--brief 仅显示有无差异,不显示详细的信息。
[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”字样的行
shell diff
Text Diff Algorithm
Linux Shell
shell 1
Shell 知识点
shell命令
Shell Script
Linux Shell常用shell命令
diff和patch使用指南 - Linux天空
Bourne Shell及shell编程 1
Bourne Shell及shell编程 2
Bourne Shell及shell编程 4
【Linux Shell简介】
快速编辑 Shell 命令行
Shell Script初體驗
Shell 設計入門 -- 臥龍小三
SHELL 十三问
Shell编程资源
Shell 条件判断总结
shell常见知识
Shell基本语法:
shell case--八个部分
shell awk-------未完待续
shell数组 | Linux中国