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.