Guest User

Untitled

a guest
Apr 23rd, 2018
233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.96 KB | None | 0 0
  1. #
  2. # From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
  3. # Newsgroups: comp.unix.shell,comp.os.linux.misc
  4. # Subject: GNU Bash Script to fix filenames
  5. # Date: 28 Mar 1996 14:54:43 -0800
  6. # Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
  7. #
  8. #This is a script which takes a list of directories, descends through each one
  9. #and ``corrects'' filenames that:
  10. #
  11. # - contain filename globbing characters: * ? [ ]
  12. # - quote characters: ' "
  13. # - control characters: 0-31 (127 is not dealt with---oops)
  14. # - - or + as the first character
  15. #
  16. # The GNU version of 'tr' is required. Also requires 'sed'.
  17. #
  18. # Script to process a given list of directories recursively
  19. # and rename each file to something that is reasonable.
  20. #
  21. # The rules are:
  22. #
  23. # 1. replace each space, [, ], *, ", and ' character in the name with a
  24. # period.
  25. # 2. replace each control character 1..31 with a printable character obtained
  26. # by adding 64 to the ascii value. ^A becomes A, ^B becomes B and so on.
  27. # 3. replace a - or + occuring at the beginning of the name with a #
  28. #
  29. # 4. if the resulting name has been changed in any way, then
  30. # 5. if a file of the new name already exists, then
  31. # 6. add a . to the new name and goto step 5.
  32. # 7. rename the old name to the new name
  33. #
  34. # written by Kaz Kylheku <kaz@cafe.net>
  35. # March 1996
  36. # Vancouver, Canada
  37. #
  38. # requires GNU 'bash', GNU 'tr', and some sort of 'sed' program.
  39. #
  40. # minimal conversion to bash v2 syntax done by Chet Ramey
  41.  
  42. processfile()
  43. {
  44. new_name="`echo -n $1 | tr '\173\175\052\077\042\047 ' '.......' |
  45. tr '[\000-\037]' '[\100-\137]' |
  46. sed -e 's/^-/#/' -e 's/+/#/'`"
  47. if [ "$new_name" != "$1" ] ; then
  48. while [ -e "$new_name" ] ; do
  49. new_name="${new_name}."
  50. done
  51. echo changing \"$1\" to \"$new_name\" in `pwd`
  52. mv -- "$1" "$new_name"
  53. fi
  54. }
  55.  
  56. processdir()
  57. {
  58. set -f
  59. local savepwd="$PWD"
  60. if cd "$1" ; then
  61. set +f
  62. for file in * ; do
  63. set -f
  64. if [ "$file" != "." -a "$file" != ".." ] ; then
  65. if [ -L "$file" ] ; then
  66. echo "skipping symlink" $file in `pwd`
  67. elif [ -d "$file" ] ; then
  68. processdir "$file"
  69. elif [ -f "$file" ] ; then
  70. processfile "$file"
  71. fi
  72. fi
  73. done
  74. cd "$savepwd"
  75. fi
  76. }
  77.  
  78. shopt -s nullglob dotglob
  79.  
  80. if [ $# = 0 ] ; then
  81. echo "$0: must specify a list of directories" >&2
  82. echo "$0: usage: $0 directory [directory ...]" >&2
  83. exit 2
  84. fi
  85.  
  86. while [ $# != 0 ] ; do
  87. processdir "$1"
  88. shift
  89. done
  90.  
  91. exit 0
Add Comment
Please, Sign In to add comment