1. 程式人生 > >diff 比較兩個文件的不同

diff 比較兩個文件的不同

方式 指定 oot host clas roo ash 文件內容 cal

1.命令功能

diff 逐行比較文件內容,並輸出文件差異。

2.語法格式

diff option file1 file2

diff 選項 文件1 文件2

參數說明

參數

參數說明

-y

以並列方式顯示文件的異同之處

-W

在使用-y時,指定顯示跨度

-c

上下文顯示輸出格式

-u

統一格式輸出

3.使用範例

範例1 diff 不接任何參數

[root@localhost ~]# cat test1

1

2

3

4

5

[root@localhost ~]# cat test2

8

2

5

4

5

[root@localhost ~]# diff test1 test2

1c1

< 1

---

> 8

3c3

< 3

---

> 5

範例2 並排顯示差異

[root@localhost ~]# diff -y  test1 test2

1                                                             | 8

2                                                               2

3                                                             | 5

4                                                               4

5                                                               5

範例3 並排顯示差異,並指定寬度

[root@localhost ~]# diff -y -W 20  test1 test2

1     | 8

2       2

3     | 5

4       4

5       5

範例4 -c參數上下文輸出格式

[root@localhost ~]# diff -c test1 test2

*** test1       2018-04-24 17:38:25.472481461 +0800

--- test2       2018-04-24 17:45:18.490482647 +0800

***************

*** 1,5 ****

! 1

 2

! 3

 4

 5

--- 1,6 ----

! 8

 2

! 5

 4

 5

+ 6

說明:!表示不同;+表示test2比test1多的行。

diff 比較兩個文件的不同