Advertisement
Guest User

LowerIt

a guest
Mar 14th, 2015
299
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. ## Make sure we're in the right directory
  14. export strStartDir="$(pwd)"
  15.  
  16. ## Write in a function the loop that we'll use for to edit the files
  17. function editingloop {
  18.     if [[ -d "$strDir" ]]; then
  19.         cd "$strDir"
  20.         ## Check to make sure current directory isn't empty
  21. #        if [[ $(echo $strDir/*) != $(echo $strDir'/*') ]]; then  ## Doesn't seem to be working.  :\
  22.             echo '
  23. _______________________________________________________________________
  24. Entering current directory:  '"$strDir"
  25.             echo '
  26. Current directory before edit:  ' && ls
  27.             ## Start the editing loop
  28.             for strItem in "$(ls -a)"; do
  29.                 ## Save the computer some breath on . and ..
  30.                 if [[ "$strItem" != '.' || '..' ]]; then
  31.                     ## Set a variable equal to the lowercased vesion of the original filename
  32.                     strLowerCasedItem=$(echo "$strItem" | sed 's/[[:upper:]]*/\L&/')
  33.                     ## If this variable is the same as the original, then the original was already lowercased, so we skip it.  
  34.                     if [[ "$strLowerCasedItem" != "$strItem" ]]; then
  35.                         mv -i $(echo "$strDir"'/'"$strItem") $(echo "$strDir"'/'"$strLowerCasedItem")  ## '-i' makes sure that we check with the user before clobbering
  36.                     fi
  37.                 fi
  38.             done
  39.             echo '
  40. Current directory after edit:  ' && ls
  41. #        fi
  42.     fi
  43. }
  44.  
  45. ## Lowercase all items in the starting directory
  46. export strDir="$strStartDir"
  47. editingloop
  48. unset strDir
  49.  
  50. ## This loops the main loop through each subdirectory of the starting directory
  51. for strDir in $(echo "$strStartDir"/*); do
  52. editingloop
  53. done
  54.  
  55. exit 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement