递归查找具有相同文件名集的目录

时间:2017-03-09 23:27:51

标签: shell recursion directory

我正在寻找一种方法来查找当前目录中的任何目录是否有任何重复的目录,递归。

/user/guy/textfile1.txt
/user/guy/textfile2.txt
/user/guy/textfile3.txt
/user/girl/textfile1.txt
/user/girl/textfile2.txt
/user/girl/textfile3.txt
/user/fella/textfile1.txt
/user/fella/textfile2.txt
/user/fella/textfile3.txt
/user/fella/textfile4.txt
/user/rudiger/rudy/textfile1.txt
/user/rudiger/rudy/textfile2.txt
/user/rudiger/rudy/textfile3.txt
/user/julian/rudy/textfile1.txt
/user/julian/rudy/textfile2.txt
/user/julian/rudy/textfile3.txt

/ girl和/ guy / rudy将是重复的目录,所以/ julian和rudiger。我们还将检查是否有任何其他文件包含与“user”相同的文件/目录。当我们从“user”运行脚本作为当前目录时,我们要检查当前目录以及任何重复的行。

我当前的代码有效...但它不是递归的,这是一个问题。

for d in */ ; do
  for d2 in */ ; do
    if [ "$d" != "$d2" ] ; then 
        string1="$(ls "$d2")"
        string2="$(ls "$d")"
        if [ "$string1" == "$string2" ] ; then
            echo "The directories $d and $d2 are the same"
        fi
    fi
  done
done

1 个答案:

答案 0 :(得分:2)

#!/usr/bin/env bash
#              ^^^^- must be bash, not /bin/sh, and version 4.0 or newer.

# associative array mapping hash to first directory seen w/ same
declare -A hashes=( )

# sha256sum requiring only openssl, vs GNU coreutils
sha256sum() { openssl dgst -sha256 -r | sed -e 's@[[:space:]].*@@'; }

while IFS= read -r -d '' dirname; do
  hash=$(cd "$dirname" && printf '%s\0' * | sha256sum)
  if [[ ${hashes[$hash]} ]]; then
    echo "COLLISION: Directory $dirname has same filenames as ${hashes[$hash]}"
  else
    hashes[$hash]=$dirname
  fi
done < <(find . -type d -print0)