Find files with identical names recursively (bash4, assoc. arrays)

/ Published in: Bash
Save to your folder(s)

Copy this code and paste it in your HTML
  1. # Find files with duplicate names. Generate a file ("dfn") with "mv" commands
  2. # to move the duplicates to a subdirectory ("DUPS") with mangled filename.
  3. # (e.g. multiple files "foo.txt" become foo_1.txt, foo_2.txt and so on).
  4. # NOTE: Even after sourcing dfn (and thus moving the files) there may be
  5. # duplicate names, since there may already have been a "foo_1.txt" in the first place (and
  6. # so now you have 2). So the scriptlet has to be called multiple times (i.e.
  7. # until dfn is empty)
  8. unset fl; declare -A fl
  9. while IFS=$'\001' read -r ff f; do
  10. if [[ ${fl[$f]} ]]; then
  11. (( fl[$f]++ )); sfx="${f##*.}"
  12. printf 'mv -- "%s" DUPS/"%s" # Duplicate Filename: "%s" (%i)\n'\
  13. "$ff" "${f%.*}_${fl[$f]}.${sfx}" "$f" "${fl[$f]}"
  14. else
  15. fl[$f]=0;
  16. fi done < <(find . -type f \
  17. -exec bash -c 'for file in "$@"; do printf "%s\001%s\n" "$file" "${file##*/}"; done' _ {} +) >dfn

Report this snippet


RSS Icon Subscribe to comments

You need to login to post a comment.