2009年3月5日 星期四

Linux指令 - diff

在Linux下有三種比較檔案的指令:diff、cmp、patch
目前先來整理一下diff吧~

cmp是以byte為單位來比較兩個文件;而diff是以行為單位來比較兩個文件~

hello.c

1  #include <stdio.h>
2
3 int main(void)
4 {
5 char msg[] = "Hello world!";
6
7 printf("Hello World!!\n");
8 puts(msg);
9 printf("Welcome to use diff commond.\n");
10
11 return 0;
12 }


hello_diff.c
1  #include <stdio.h>
2 #include <stdlib.h>
3
4 int main(void)
5 {
6 char msg[] = "Hello world,fome hello_diff.c";
7
8 puts(msg);
9 printf("hello_diff.c says,'Here you are,using diff.'\n");
10
11 return 0;
12 }


$ diff hello.c hello_diff.c

結果與解示:
1a2
> #include <stdlib.h>
右邊文件的第2行是新增的(也就是左邊的文件中沒有這一行)
5c6
< char msg[] = "Hello world!";
---
> char msg[] = "Hello world,fome hello_diff.c";
左邊的第5行修改為右邊的第6行
7d7
< printf("Hello World!!\n");
原來左邊的第7行被刪掉了
9c9
< printf("Welcome to use diff commond.\n");
---
> printf("hello_diff.c says,'Here you are,using diff.'\n");
左邊的第9行修改為右邊的第9行


基本上呢,以上都是在描述左邊的文件
>:表示左邊增加一行
<:表示左邊減少一行


而可以用colordiff會有顏色,比較漂亮哩~

參考資料:
鳥哥
Linux: 加上顏色區別的 diff - colordiff

沒有留言: