SHOW:
|
|
- or go back to the newest paste.
| 1 | #!/bin/bash | |
| 2 | ## | |
| 3 | ## "LowerIt" | |
| 4 | ## | |
| 5 | - | ## Original code found here: http://www.linuxjournal.com/content/convert-filenames-lowercase |
| 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 | - | unset pwd && unset export && unset cd && unset echo && unset ls && unset sd && unset mv |
| 13 | + | |
| 14 | unset pwd && unset export && unset cd && unset echo && unset sed && unset mv | |
| 15 | - | export strDir=$(pwd) |
| 15 | + | |
| 16 | - | cd $strDir |
| 16 | + | export strStartDir=$(pwd) |
| 17 | - | echo "Entering current directory: "$strDir |
| 17 | + | cd $strStartDir |
| 18 | - | ## Need to make a loop or something here that repeats this for every subdirectory ad nauseum |
| 18 | + | |
| 19 | ## Write in a function the loop that we'll use for to edit the files | |
| 20 | - | ## Show the user what's in the current directory pre-edit |
| 20 | + | function editingloop {
|
| 21 | - | echo " |
| 21 | + | if [[ -d $strDir ]]; then |
| 22 | - | Current directory before edit: " && ls -a |
| 22 | + | cd $strDir |
| 23 | - | ## Start the editing loop |
| 23 | + | ## Check to make sure current directory isn't empty |
| 24 | - | for strItem in $(ls -a) |
| 24 | + | # if [[ $(echo $strDir/*) != $(echo $strDir'/*') ]]; then ## Doesn't seem to be working. :\ |
| 25 | - | do |
| 25 | + | echo " |
| 26 | - | ## Save the computer some breath on . and .. |
| 26 | + | _______________________________________________________________________ |
| 27 | - | # if [[ $strItem != '.' || '..' ]]; then |
| 27 | + | Entering current directory: "$strDir |
| 28 | - | # continue |
| 28 | + | echo '' |
| 29 | - | # fi |
| 29 | + | echo "Current directory before edit: " && echo $strDir/* | sed 's/ /\n/g' | sed 's!.*/!!' | tr '\r\n' ' ' |
| 30 | - | ## Set a variable equal to the lowercased vesion of the original filename |
| 30 | + | ## Start the editing loop |
| 31 | - | strLowerCasedItem=$(echo $strItem | sed 's/[[:upper:]]*/\L&/') |
| 31 | + | for strItem in $(ls -a); do |
| 32 | - | ## If this variable is the same as the original, then the original was already lowercased, so we skip it. |
| 32 | + | ## Save the computer some breath on . and .. |
| 33 | - | if [[ $strLowerCasedItem != $strItem ]]; then |
| 33 | + | # if [[ $strItem != '.' || '..' ]]; then ## Doesn't seem to be working. :\ |
| 34 | - | mv -i $(echo $strDir'/'$strItem) $(echo $strDir'/'$strLowerCasedItem) |
| 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 | - | ## Show the user what's in the current directory post-edit |
| 37 | + | if [[ $strLowerCasedItem != $strItem ]]; then |
| 38 | - | echo " |
| 38 | + | mv -i $(echo $strDir'/'$strItem) $(echo $strDir'/'$strLowerCasedItem) ## '-i' makes sure that we check the user before clobbering |
| 39 | - | Current directory after edit: " && ls -a |
| 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 |