rename.sh 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. #!/bin/sh
  2. usage() {
  3. echo 'Usage: ${0} -p prefix [-d directory] [-t extension] [-u]'
  4. echo " -p prefix to be removed"
  5. echo " -d destination folder"
  6. echo " default current directory"
  7. echo " -t files extension"
  8. echo " default to ogg"
  9. echo " -u encode filename in UTF-8"
  10. echo '---------------------'
  11. }
  12. processing()
  13. {
  14. echo processing "${@}"
  15. newfile=`echo "$@" | sed "s/${prefix}//g"`
  16. [ -n ${iconv} ] && newfile=`echo ${newfile} | iconv -cs -t UTF-8`
  17. echo " : copying <$newfile> into <${dest}>"
  18. cp "${@}" "${dest}/${newfile}"
  19. }
  20. while getopts "p:d:t:u" option
  21. do
  22. case ${option} in
  23. p)
  24. prefix="${OPTARG}"
  25. ;;
  26. d)
  27. dest="${OPTARG}"
  28. ;;
  29. t)
  30. ext="${OPTARG}"
  31. ;;
  32. u) iconv="y"
  33. ;;
  34. *)
  35. usage
  36. exit 1
  37. ;;
  38. esac
  39. done
  40. dest=${dest:-.}
  41. ext=${ext:-ogg}
  42. [ -z "${dest}" ] && usage && exit 1
  43. [ -z "${prefix}" ] && usage && exit 1
  44. echo
  45. echo "Copy and rename ${ext} files, removing <${prefix}> prefixes, into <${dest}>."
  46. [ -n "${iconv}" ] && echo '* Filename UTF-8 encoding is on.'
  47. echo
  48. mkdir -p "${dest}" || (echo "unable to create ${dest}" && exit 1)
  49. find . -maxdepth 1 -type f -name "*.${ext}" -print | while IFS= read file; do processing "${file}" ; done