2017-03-09 68 views
0

我正在寻找一种方法来查找当前目录以前的任何目录是否有递归的任何重复目录。
递归找到具有相同文件名集的目录

/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 

/女孩/男孩/鲁迪是重复的目录,所以会/ Julian和吕迪格。我们还将检查是否有任何其他文件包含与“用户”相同的文件/目录。当我们从“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 
+0

生成的每个目录的内容的散列值。按哈希排序该列表。两行相邻,具有相同的散列==两个具有相同名称条目的目录。 –

+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 '[email protected][[: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) 
相关问题