递归查找具有相同名称的文件(bash4,关联数组)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了递归查找具有相同名称的文件(bash4,关联数组)相关的知识,希望对你有一定的参考价值。

  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=$'01' 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) '
  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 "[email protected]"; do printf "%s01%s " "$file" "${file##*/}"; done' _ {} +) >dfn

以上是关于递归查找具有相同名称的文件(bash4,关联数组)的主要内容,如果未能解决你的问题,请参考以下文章