epifn
epifn

Reputation: 53

Finding repeated file names in directory (without specifying the exact file name)

I have a directory full of files all following the naming convention "file_001" "file_002" etc. The files are all stored in various sub directories and some have the same name.

I have used the find command and redirected the output to a text file which has a list of the paths for all the files in the directory and what I'm trying to do is search the text file for any repeated file name.

My best guess for how to do this is using grep but I can't figure out the proper syntax.

Upvotes: 1

Views: 587

Answers (1)

whoan
whoan

Reputation: 8521

It prints just the names of the repeated files:

find /your/path -type f -printf "%f\n" | sort | uniq -d

It prints the path of the repeated files:

Method 1:

find /your/path -type f | grep -F -f <(find /your/path -type f -printf "%f\n" | sort | uniq -d)

This is my favourite due to it doesn't save any temporary files in the disk. It's using process substitution, so take care to invoke your script with an explicit #!/bin/bash line. You can see detailed info in this question: Syntax error in shell script with process substitution

Method 2:

find /your/path -type f > your_file_with_paths.txt
find /your/path -type f -printf "%f\n" | sort | uniq -d |
while read FILENAME; do
    grep -F "$FILENAME" your_file_with_paths.txt
done

Explanation:

find /your/path -type f

This command returns all the files' paths under /your/path.


find /your/path -type f -printf "%f\n" | sort | uniq --repeated

It takes only the filenames instead of the complete path, sort them, and then filter only the repeated ones (--repeated is just the long form of -d).


grep -F -f <(find /your/path -type f -printf "%f\n" | sort | uniq --repeated)
# or
grep -F "$FILENAME" your_file_with_paths.txt

For any repeated filename, look for their paths.

Upvotes: 3

Related Questions