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

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

    Here is another solution (based on the suggestion by @jim-mcnamara) without awk:

    Solution 1

    #!/bin/sh 
    dirname=/path/to/directory
    find $dirname -type f | sed 's_.*/__' | sort|  uniq -d| 
    while read fileName
    do
    find $dirname -type f | grep "$fileName"
    done
    

    However, you have to do the same search twice. This can become very slow if you have to search a lot of data. Saving the "find" results in a temporary file might give a better performance.

    Solution 2 (with temporary file)

    #!/bin/sh 
    dirname=/path/to/directory
    tempfile=myTempfileName
    find $dirname -type f  > $tempfile
    cat $tempfile | sed 's_.*/__' | sort |  uniq -d| 
    while read fileName
    do
     grep "/$fileName" $tempfile
    done
    #rm -f $tempfile
    

    Since you might not want to write a temp file on the harddrive in some cases, you can choose the method which fits your needs. Both examples print out the full path of the file.

    Bonus question here: Is it possible to save the whole output of the find command as a list to a variable?

    0 讨论(0)
提交回复
热议问题