Вот один скрипт, который работает и достаточно эффективен. Обратите внимание, что для работы требуется ровно один пробел до «(1)», а после - ни один.
#!/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 '' | 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