天天看点

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
           

继续阅读