Advertisement
Guest User

LowerIt

a guest
Mar 14th, 2015
287
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/bin/bash
  2. ##
  3. ## "LowerIt"
  4. ##
  5. ## Original code found here:  http://www.linuxjournal.com/content/convert-filenames-lowercase ;  though this looks nothing like it.  
  6. ## Modified by Maiya78 to change directory names, to work recursively, and to support unicode filenames on GNU systems.  I also copyedited the comments.  :3
  7. ##
  8. ## From the current directory, recursively convert to lower case all file and directory names
  9. ## Should ask for verification before overwriting an existing file.  
  10. ##
  11. ## ------------------------------------------------------------------------------------------------------------
  12.  
  13. ## Clean up aliases
  14. # unalias pwd
  15. # unalias export
  16. # unalias cd
  17. # unalias echo
  18. # unalias sed
  19. # unalias mv
  20. ## Make sure we're in the right directory
  21. export strStartDir="$(pwd)"
  22. cd "$strStartDir"
  23.  
  24. ## Write in a function the loop that we'll use for to edit the files
  25. function editingloop {
  26.     if [[ -d "$strDir" ]]; then
  27.         cd "$strDir"
  28.         ## Check to make sure current directory isn't empty
  29. #        if [[ $(echo $strDir/*) != $(echo $strDir'/*') ]]; then  ## Doesn't seem to be working.  :\
  30.             echo '
  31. _______________________________________________________________________
  32. Entering current directory:  '"$strDir"
  33.             echo '
  34. Current directory before edit:  ' && ls
  35.             ## Start the editing loop
  36.             for strItem in "$(ls -a)"; do
  37.                 ## Save the computer some breath on . and ..
  38.                 if [[ "$strItem" != '.' || '..' ]]; then  ## Doesn't seem to be working.  :\
  39.                     ## Set a variable equal to the lowercased vesion of the original filename
  40.                     strLowerCasedItem=$(echo "$strItem" | sed 's/[[:upper:]]*/\L&/')
  41.                     ## If this variable is the same as the original, then the original was already lowercased, so we skip it.  
  42.                     if [[ "$strLowerCasedItem" != "$strItem" ]]; then
  43.                         mv -i $(echo "$strDir"'/'"$strItem") $(echo "$strDir"'/'"$strLowerCasedItem")  ## '-i' makes sure that we check with the user before clobbering
  44.                     fi
  45.                 fi
  46.             done
  47.             echo '
  48. Current directory after edit:  ' && ls
  49. #        fi
  50.     fi
  51. }
  52.  
  53. ## Lowercase all items in the starting directory
  54. export strDir="$strStartDir"
  55. editingloop
  56. unset strDir
  57.  
  58. ## This loops the main loop through each subdirectory of the starting directory
  59. for strDir in $(echo "$strStartDir"/*); do
  60. editingloop
  61. done
  62.  
  63. exit 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement