SHOW:
|
|
- or go back to the newest paste.
1 | #!/bin/bash | |
2 | ## | |
3 | ## "Lowercase all the things" | |
4 | ## | |
5 | ## Copyright (C) 2015 under CC0 (Creative Commons 0; ie I waive all my copyright rights for this code) | |
6 | ## By 'Maiya78' from the Paradox forums | |
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 | ## Should also decapitalize Cyrillic, Greek, and other mixed-case scripts. | |
11 | ## | |
12 | ## ----------------------------------------------------------------------------------------------------------- | |
13 | ||
14 | ## Have script handle spaces | |
15 | export IFS=' | |
16 | ' | |
17 | ||
18 | ## Setting an important early variable | |
19 | export strStartDir=$(pwd) | |
20 | ||
21 | ## Write in a function the loop that we'll use for to edit the files | |
22 | function editingloop { | |
23 | - | if [[ -d "$strDir" ]]; then |
23 | + | cd "$strDir" |
24 | - | cd "$strDir" |
24 | + | ## Check to make sure current directory isn't empty |
25 | - | ## Check to make sure current directory isn't empty |
25 | + | # if [[ $(ls -a) != {. ..} ]]; then ## Not sure how to do this without a loop, and don't want to use a loop for it. |
26 | - | # if [[ $(ls -a) != {. ..} ]]; then ## Not sure how to do this without a loop, and don't want to use a loop for it. |
26 | + | echo ' |
27 | - | echo ' |
27 | + | |
28 | Entering current directory: '"$strDir" | |
29 | echo ' | |
30 | - | echo ' |
30 | + | |
31 | ## Dimensioning variables like a responsible person | |
32 | - | ## Dimensioning variables like a responsible person |
32 | + | local strItem |
33 | - | local strItem |
33 | + | local strLowerCasedItem |
34 | - | local strLowerCasedItem |
34 | + | ## Start the editing loop |
35 | - | ## Start the editing loop |
35 | + | for strItem in $(ls); do |
36 | - | for strItem in $(ls); do |
36 | + | ## Save the computer some breath on . and .. |
37 | - | ## Save the computer some breath on . and .. |
37 | + | if [[ "$strItem" != '.' || '..' ]]; then |
38 | - | if [[ "$strItem" != '.' || '..' ]]; then |
38 | + | ## Set a variable equal to the lowercased vesion of the original filename |
39 | - | ## Set a variable equal to the lowercased vesion of the original filename |
39 | + | strLowerCasedItem=$(echo "$strItem" | sed 's/[[:upper:]]*/\L&/') |
40 | - | strLowerCasedItem=$(echo "$strItem" | sed 's/[[:upper:]]*/\L&/') |
40 | + | ## If this variable is the same as the original, then the original was already lowercased, so we skip it; otherwise, we rename it to the lowercased vesion. |
41 | - | ## If this variable is the same as the original, then the original was already lowercased, so we skip it; otherwise, we rename it to the lowercased vesion. |
41 | + | if [[ "$strLowerCasedItem" != "$strItem" ]]; then |
42 | - | if [[ "$strLowerCasedItem" != "$strItem" ]]; then |
42 | + | mv -i $(echo "$strDir"'/'"$strItem") $(echo "$strDir"'/'"$strLowerCasedItem") ## '-i' makes sure that we check with the user before clobbering |
43 | - | mv -i $(echo "$strDir"'/'"$strItem") $(echo "$strDir"'/'"$strLowerCasedItem") ## '-i' makes sure that we check with the user before clobbering |
43 | + | fi |
44 | - | fi |
44 | + | fi |
45 | - | fi |
45 | + | done |
46 | - | done |
46 | + | echo ' |
47 | - | echo ' |
47 | + | |
48 | # fi | |
49 | - | # fi |
49 | + | |
50 | - | fi |
50 | + | |
51 | ## Lowercase all items in the starting directory | |
52 | export strDir="$strStartDir" | |
53 | editingloop | |
54 | ||
55 | ## This loops the main loop through each subdirectory of the starting directory | |
56 | for strDir in $(find -type d); do | |
57 | export strDir=$(echo "$strStartDir"$(echo "$strDir" | sed 's/^.//')) | |
58 | - | for strDir in $(ls); do |
58 | + | echo "$strDir" |
59 | editingloop | |
60 | done | |
61 | ||
62 | exit 0 |