Aqui está um script que funciona e é razoavelmente eficiente. Note que ele requer precisamente que um espaço tenha sido adicionado antes do "(1)" e nenhum tenha sido adicionado depois para que ele funcione.
#!/usr/bin/bash
IFS=$'\n';
set -f
#Go deepest first to deal with copies within copied folders.
for copy in $(find . -regextype posix-egrep -regex "^.*\ \([0-9]+\)\s*(\.[^/.]*)?$" | awk '{print length($0)"\t"$0}' | sort -rnk1 | cut -f2-); do
orig=$(rev <<< "$copy" | sed -E 's/\)[0-9]+\(\ //' | rev)
if [ "$orig" != "$copy" ]; then
if [ -f "$orig" ]; then
if [ -f "$copy" ]; then
echo "File pair: $orig $copy"
if diff -q "$orig" "$copy" &>/dev/null; then
echo "Removing file: $copy"
rm -f "$copy";
fi
fi
fi
if [ -d "$orig" ]; then
if [ -d "$copy" ]; then
echo "Folder pair: $orig $copy"
if rmdir "$copy" &>/dev/null; then
#If the "copy" was an empty directory then we've removed it and so we're done.
echo "Removed empty folder: $copy"
else
#Non-destructively ensure that both folders have the same files at least.
rsync -aHAv --ignore-existing "$orig/" "$copy" &>/dev/null
rsync -aHAv --ignore-existing "$copy/" "$orig" &>/dev/null
if diff -qr "$orig" "$copy" &>/dev/null; then
echo "Removing folder: $copy"
rm -rf "$copy";
fi
fi
fi
fi
fi
done
unset IFS;
set +f