如何在两个目录中查找/列出所有唯一文件?


4

有一个很好的工具,fdupes,用于在两个(或更多)目录中查找重复文件。

我正在寻找一个简单的工具/命令,可以输出补充集 - 那些没有重复的文件的路径。

Answers:


6
find DIR1 DIR2 -type f -exec sha1sum '{}' \+ | sort | \
    uniq -c --check-chars 40 | egrep '^ *1 ' | cut -c 51-

1
使用uniq功能的微小修改而不是grepfind DIR1 DIR2 -type f -exec sha1sum {} + | sort | uniq -u --check-chars 40 | cut -c 43-
Daniel Andersson

0

作为替代方案,将保持jdupes的部分匹配(比完整哈希快得多)

jdupes -r -T -T "$DIR1" "$DIR2" | awk -v p1="$DIR1" -v p2="$DIR2" '$0 ~ p1 && $0 ~ p2'  RS="\n\n" ORS="\n\n" | sort > dupes
find "$DIR1 $DIR2" -type f | sort > all
comm -23 all dupes

我创建了两个列表:一个包含重复文件,一个包含所有文件。awk语句确保dupes文件只包含出现在$ DIR1和$ DIR2中的文件(--isolate显然还有其他内容)

然后我使用该comm实用程序来比较它们仅对all列表唯一的文件(而不是欺骗列表)。唯一需要注意的是jdupes只检查每个文件的第一个4K,这可能不足以确保唯一的匹配,所以对于个人使用我碰到了高达1MB并且它仍然运行得非常快。

注意:如果您只想要$ DIR1唯一的文件,请在find命令中删除$ DIR2搜索。

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.