Advertisement
Guest User

LowerIt

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