rsync 僅刪除符合的文件

rsync 僅刪除符合的文件

如何使用 rsync (或其他程式)刪除目錄之間的相同檔案?為了相同,位置和元資料應該要匹配(權限、所有權、時間戳記)

例如,我將目錄X備份到Y。

我想從 X 中刪除與 Y 中相同的所有檔案/目錄。

注意:我熟悉 jdupes,但我並不想刪除任何相同的檔案。我想刪除目錄位置和檔案名稱也相同的檔案。

答案1

cd /path/to/X
find -type f -exec ls -l {} \; > /tmp/LIST # Get a list of all files in X
cd /path/to/Y
find -type f -exec ls -l {} \; >> /tmp/LIST # Get a list of all files in Y (combine with list from X)
cd /tmp
sort LIST > SORT # Sort all listed
uniq -d SORT > DUP # Exclude files that aren't listed twice
cd /path/to/X
cat /tmp/DUP | xargs -d '\n' rm # Delete all files listed as duplicate
find -type d -empty -delete # Optional, delete all empty directories

警告-

此解決方案使用 的輸出來比較文件ls -l,因此它比較元資料日期+時間+擁有者+權限+文件名,而不比較文件內的位元組。此外,名稱中帶有換行符的檔案也不安全。

相關內容