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

前端 未结 7 774
执念已碎
执念已碎 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:28

    Here is my contribution (this just searches for a specific file type, pdfs in this case) but it does so recursively:

    #!/usr/bin/env bash
    
    find . -type f | while read filename; do
        filename=$(basename -- "$filename")
        extension="${filename##*.}"
        if [[ $extension == "pdf" ]]; then
            fileNameCount=`find . -iname "$filename" | wc -l`
            if [[ $fileNameCount -gt 1 ]]; then
                echo "File Name: $filename, count: $fileNameCount"
            fi
        fi
    done
    

提交回复
热议问题