NameChange 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. #!/bin/sh
  2. # rename files
  3. ERROR()
  4. {
  5. echo error: $*
  6. exit 1
  7. }
  8. USAGE()
  9. {
  10. [ -z "$1" ] || echo error: $*
  11. echo usage: $(basename "$0") '<options>'
  12. echo ' --help -h this message'
  13. echo ' --verbose -v more messages'
  14. echo ' --dest=dir -d <dir> directory of files [image]'
  15. echo ' --wiki -w rename pedia to wiki [default]'
  16. echo ' --pedia -p rename wiki to pedia'
  17. exit 1
  18. }
  19. # main program
  20. # ------------
  21. verbose=no
  22. to=wiki
  23. from=pedia
  24. dest=image
  25. getopt=/usr/local/bin/getopt
  26. [ -x "${getopt}" ] || getopt=getopt
  27. args=$(${getopt} -o hvd:wp --long=help,verbose,dest:,wiki,pedia -- "$@") || exit 1
  28. # replace the arguments with the parsed values
  29. eval set -- "${args}"
  30. while :
  31. do
  32. case "$1" in
  33. -v|--verbose)
  34. verbose=yes
  35. shift
  36. ;;
  37. -w|--wiki)
  38. to=wiki
  39. from=pedia
  40. shift
  41. ;;
  42. -p|--pedia)
  43. to=pedia
  44. from=wiki
  45. shift
  46. ;;
  47. -d|--dest)
  48. dest="$2"
  49. shift 2
  50. ;;
  51. --)
  52. shift
  53. break
  54. ;;
  55. -h|--help)
  56. USAGE
  57. ;;
  58. *)
  59. USAGE invalid option: $1
  60. ;;
  61. esac
  62. done
  63. [ -d "${dest}" ] || USAGE not a directory: ${dest}
  64. case "${verbose}" in
  65. [yY]|[yY][eE][sS])
  66. echo rename "${from}" to "${to}"
  67. echo in directory: "${dest}"
  68. ;;
  69. *)
  70. ;;
  71. esac
  72. find "${dest}" -name "${from}"\* | while read file junk
  73. do
  74. d=$(basename "${file}")
  75. d=$(dirname "${file}")/${to}${d#${from}}
  76. case "${verbose}" in
  77. [yY]|[yY][eE][sS])
  78. echo rename "${file}" to "${d}"
  79. ;;
  80. *)
  81. ;;
  82. esac
  83. mv "${file}" "${d}"
  84. done