How to find duplicate filenames (recursively) in a given directory? BASH

前端 未结 7 776
执念已碎
执念已碎 2021-02-04 08:06

I need to find every duplicate filenames in a given dir tree. I dont know, what dir tree user will give as a script argument, so I dont know the directory hierarchy. I tried thi

7条回答
  •  盖世英雄少女心
    2021-02-04 08:36

    #!/bin/bash
    
    file=`mktemp /tmp/duplicates.XXXXX` || { echo "Error creating tmp file"; exit 1; }
    find $1 -type f |sort >  $file
    awk -F/ '{print tolower($NF)}' $file |
            uniq -c|
            awk '$1>1 { sub(/^[[:space:]]+[[:digit:]]+[[:space:]]+/,""); print }'| 
            while read line;
                    do grep -i "$line" $file;
            done
    
    rm $file
    

    And it also work with spaces in filenames. Here's a simple test (the first argument is the directory):

    ./duplicates.sh ./test
    ./test/2/INC 255286
    ./test/INC 255286
    

提交回复
热议问题