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

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

    This solution writes one temporary file to a temporary directory for every unique filename found. In the temporary file, I write the path where I first found the unique filename, so that I can output it later. So, I create a lot more files that other posted solutions. But, it was something I could understand.

    Following is the script, named fndupe.

    #!/bin/bash
    
    # Create a temp directory to contain placeholder files.
    tmp_dir=`mktemp -d`
    
    # Get paths of files to test from standard input.
    while read p; do
      fname=$(basename "$p")
      tmp_path=$tmp_dir/$fname
      if [[ -e $tmp_path ]]; then
        q=`cat "$tmp_path"`
        echo "duplicate: $p"
        echo "    first: $q"
      else
        echo $p > "$tmp_path" 
      fi
    done
    
    exit
    

    Following is an example of using the script.

    $ find . -name '*.tif' | fndupe
    

    Following is example output when the script finds duplicate filenames.

    duplicate: a/b/extra/gobble.tif
        first: a/b/gobble.tif
    

    Tested with Bash version: GNU bash, version 4.1.2(1)-release (x86_64-redhat-linux-gnu)

提交回复
热议问题