天天看點

Linux diff

由于linux檔案系統中目錄也是檔案。diff指令可以用于比較兩個目錄是否含有相同檔案。

請看例子:

[[email protected] tt]$ ls -ltrh t1
total 8.0K
drwxr-xr-x 2 oracle oinstall 4.0K Jan 17 10:32 t11
-rw-r--r-- 1 oracle oinstall    6 Jan 17 10:35 t1.txt
[[email protected] tt]$ ls -ltrh t1/t11
total 4.0K
-rw-r--r-- 1 oracle oinstall 3 Jan 17 10:46 t11.txt
[[email protected] tt]$ ls t2
t1.txt
[[email protected] tt]$ diff t1 t2
Only in t1: t11
diff t1/t1.txt t2/t1.txt
1c1
< 12345
---
> 1234
           

可以看到diff不僅找出t11目錄在t1下存在但在t2下沒有。而且比較了t1和t2下兩個同名檔案的内容。

-r: 遞歸式比較各級子目錄。有:

[[email protected] tt]$ ls t2
t1.txt
[[email protected] tt]$ mkdir t2/t11
[[email protected] tt]$ diff t1 t2
Common subdirectories: t1/t11 and t2/t11
diff t1/t1.txt t2/t1.txt
1c1
< 12345
---
> 1234
[[email protected] tt]$ diff -r t1 t2
Only in t1/t11: t11.txt
diff -r t1/t1.txt t2/t1.txt
1c1
< 12345
---
> 1234
           

繼續閱讀