在给定目录中查找是否存在 2 个内容完全相同的文件,无论其名称如何

在给定目录中查找是否存在 2 个内容完全相同的文件,无论其名称如何

如果可以在命令行上使用一系列命令来解决问题,那么对我来说这比编写脚本更好

答案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}'

相关内容