View difference between Paste ID: jAc5L198 and JxFJAdnM
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 ;  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
14+
# unalias pwd
15
# unalias export
16-
export strStartDir=$(pwd)
16+
# unalias cd
17-
cd $strStartDir
17+
# unalias echo
18
# unalias sed
19
# unalias mv
20
## Make sure we're in the right directory
21-
    if [[ -d $strDir ]]; then
21+
export strStartDir="$(pwd)"
22-
        cd $strDir
22+
cd "$strStartDir"
23
24
## Write in a function the loop that we'll use for to edit the files
25-
            echo "
25+
26
    if [[ -d "$strDir" ]]; then
27-
Entering current directory:  "$strDir
27+
        cd "$strDir"
28-
            echo ''
28+
29-
            echo "Current directory before edit:  " && echo $strDir/* | sed 's/ /\n/g' | sed 's!.*/!!' | tr '\r\n' ' '
29+
30
            echo '
31-
            for strItem in $(ls -a); do
31+
32
Entering current directory:  '"$strDir"
33-
#                if [[ $strItem != '.' || '..' ]]; then  ## Doesn't seem to be working.  :\ 
33+
34
Current directory before edit:  ' && ls
35-
                    strLowerCasedItem=$(echo $strItem | sed 's/[[:upper:]]*/\L&/')
35+
36
            for strItem in "$(ls -a)"; do
37-
                    if [[ $strLowerCasedItem != $strItem ]]; then
37+
38-
                        mv -i $(echo $strDir'/'$strItem) $(echo $strDir'/'$strLowerCasedItem)  ## '-i' makes sure that we check the user before clobbering
38+
                if [[ "$strItem" != '.' || '..' ]]; then  ## Doesn't seem to be working.  :\ 
39
                    ## Set a variable equal to the lowercased vesion of the original filename
40-
#                fi
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-
'
43+
                        mv -i $(echo "$strDir"'/'"$strItem") $(echo "$strDir"'/'"$strLowerCasedItem")  ## '-i' makes sure that we check with the user before clobbering
44-
            echo "Current directory after edit:  " && echo $strDir/* | sed 's/ /\n/g' | sed 's!.*/!!' | tr '\r\n' ' '
44+
45-
            echo ''
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-
for strDir in $(echo $strStartDir/*); do
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