用 diff 比較兩個目錄 -BASH

2月 18th, 2009 由 Nica Leave a reply »

譯者:nica < nicaliu at gmail dot com>

【Nica 前言:這篇文章很短,主要是簡單敘述目錄的比較 – diff 及同步化 – rsync 的方式。在這裡我使用我的 CentOS 實作這幾個指令,進行解釋】

#ls 0103theme/
blackberry cupofcoffee fragrance incorporated meadows newsbeat transient
chasmogamous embouteillage goldengate instant monoblock stardust voyage

# ls 0215theme/
blackberry chasmogamous embouteillage goldengate meadows newsbeat transient
blogging convergence fragrance incorporated monoblock stardust voyage

# diff -r –brief 0103theme/ 0215theme/
Only in 0215theme/: blogging
Only in 0215theme/: convergence
Only in 0103theme/: cupofcoffee
Only in 0103theme/: instant

利用 rsync 將 0103theme 目錄的內容,複製至 0215theme,但不要包含已經存在或相同的檔案 (這是取代 rcp 更快、更有彈性的方法):

# rsync -a 0103theme/ 0215theme/

然後再次比較這兩個檔:

# diff -r –brief 0103theme/ 0215theme/
Only in 0215theme/: blogging
Only in 0215theme/: convergence

接下來將 0215theme 目錄 rsync 至 0103theme

# rsync -a 0215theme/ 0103theme/

再次比較兩個目錄:
# diff -r –brief 0103theme/ 0215theme/

Nica 註:無任何輸出,表兩個目錄沒有不同。
接下來,man diff 看看這些參數的意義:
–brief:只輸出檔案是否有不同處,不說明相異處的細節。
-r:在比較目錄時,遞廻比較子目錄。

至於 rsync 的參數:
-a:等同於 –archive。打包模式,同於 -rlptgoD (沒有 -H:hardlink)

原文出處:Compare two directories using diff – BASH

Advertisement

尚無回應

(Required)
(Required, will not be published)