![在给定目录中查找是否存在 2 个内容完全相同的文件,无论其名称如何](https://linux22.com/image/147095/%E5%9C%A8%E7%BB%99%E5%AE%9A%E7%9B%AE%E5%BD%95%E4%B8%AD%E6%9F%A5%E6%89%BE%E6%98%AF%E5%90%A6%E5%AD%98%E5%9C%A8%202%20%E4%B8%AA%E5%86%85%E5%AE%B9%E5%AE%8C%E5%85%A8%E7%9B%B8%E5%90%8C%E7%9A%84%E6%96%87%E4%BB%B6%EF%BC%8C%E6%97%A0%E8%AE%BA%E5%85%B6%E5%90%8D%E7%A7%B0%E5%A6%82%E4%BD%95.png)
如果可以在命令行上使用一系列命令来解决问题,那么对我来说这比编写脚本更好
答案1
for x in *; do for y in *; do [ "$x" = "$y" ] && continue; cmp -s "$x" "$y" && echo Same: "$x" and "$y"; done; done|head -1
或者,为了可读性而进行一些分解:
for x in *
do
for y in *
do
[ "$x" = "$y" ] && continue
cmp -s "$x" "$y" && echo Same: "$x" and "$y"
done
done | head -1
这head
只是为了保持镜像向下报告(“a = b”和“b = a”)。
答案2
find . -type f -exec md5sum "{}" \; | awk 'seen[$1] { print "Duplicate file "$2" with hash "$1" at "seen[$1]" } ! seen[$1] {seen[$1]=$2}'