home *** CD-ROM | disk | FTP | other *** search
/ PCNET 2006 September - Disc 1 / PCNET_CD_2006_09.iso / linux / puppy-barebones-2.01r2.iso / pup_201.sfs / usr / sbin / spacereplace < prev    next >
Encoding:
Text File  |  2005-07-25  |  2.1 KB  |  94 lines

  1. #!/bin/bash
  2. # spacereplace v1.0.1 by Richard van Kampen - july 2005
  3. function helpme {
  4. echo ""
  5. echo "Help for spacereplace"
  6. echo ""
  7. echo "spacereplace version 1.0.1 - written by Richard van Kampen"
  8. echo "Tool to substitute spaces in file and directory names with underscores."
  9. echo ""
  10. echo "Usage: spacereplace [option] [PATH]"
  11. echo ""
  12. echo "List of options:"
  13. echo ""
  14. echo "-h or --help : show help."
  15. echo "-r : recursive. Also commit changes to all sub-directories."
  16. echo ""
  17. echo "You have to specify the path to the directory you want to process. Not specifying a path will just display help"
  18. echo ""
  19. echo "Example: 'spacereplace -r /home/richard/video' changes all spaces to underscores in /home/richard/mp3 and all directories below /home/richard/mp3."
  20. echo ""
  21. exit
  22. }
  23.  
  24. for arg in "$@" # grab command line options
  25. do
  26. if [ "$arg" != "-r" ] && [ "$arg" != "-h" ]; then
  27. dir="$arg"
  28. orgdir="$dir"
  29. sub=" and sub-directories"
  30. fi
  31. case "$arg" in
  32. -h | --help ) help=1 ;; # help option true.
  33. -r ) recursive=1 ;; # recursive is true
  34. esac
  35. done
  36.  
  37. if [ "$dir" == "" ];then
  38. helpme
  39. fi
  40. if [ ! -d "$dir" ]; then
  41. echo "Directory $dir does not exist. Please try again with full path"
  42. exit
  43. fi
  44.  
  45. if [ "$help" = "1" ]; then # do this if -h option is used
  46. helpme
  47. fi
  48.  
  49. function delspaces {
  50. strips=1;
  51. teller=1; # iterate through 'contentgrab' array
  52. cd $dir;
  53. # rename @ because we need it. Do it 5 times
  54. until [ $strips = "5" ];do
  55. rename @ - *
  56. let strips+=1
  57. done
  58. for i in $(ls | sed 's/\n//g' | sed 's/\ /@/g'); do
  59. i=${i//@/ }
  60. contentgrab[$teller]=$i
  61. replacespace=${contentgrab[$teller]// /_}
  62. if [ "${contentgrab[$teller]}" != "$replacespace" ] && [ ! -f "$replacespace" ] && [ ! -d "$replacespace" ]; then
  63. mv -- "${contentgrab[$teller]}" "$replacespace"
  64. fi
  65. let teller+=1;
  66. done
  67. }
  68.  
  69. teller3=1;
  70. function getdirs {
  71. for i in $(echo */); do
  72. if [ "$i" != "*/" ];then
  73. j[$teller3]=${i%/}
  74. contentdirs[$teller3]=$dir/${j[$teller3]}
  75. let teller3+=1
  76. fi
  77. done
  78. }
  79.  
  80. delspaces
  81. getdirs
  82. counter4=1
  83. if [ "$recursive" = "1" ]; then
  84. until [ "${contentdirs[$counter4]}" == "" ]; do
  85. dir=${contentdirs[$counter4]}
  86. let counter4+=1
  87. delspaces
  88. getdirs
  89. done
  90. orgdir="$orgdir$sub"
  91. fi
  92.  
  93. echo ""
  94. echo "Changed all spaces to underscores in $orgdir"