Gentoo Archives: gentoo-user

From: Alex Schuster <wonko@×××××××××.org>
To: gentoo-user@l.g.o
Subject: Re: [gentoo-user] Change the case of file names
Date: Sun, 08 Jul 2007 23:06:21
Message-Id: 200707090100.21779.wonko@wonkology.org
In Reply to: Re: [gentoo-user] Change the case of file names by Mick
1 Mick writes:
2
3 > Thanks Alex, I was trying your script, but just like Etaoin's script it
4 > does not go beyond level 1 in the directory. All the subdirectories and
5 > files within them stay in Capital Case.
6 >
7 > How can I change it to recursively look into the directory?
8
9 That's strange. I tried that and had no problem.
10
11 Here's a slightly enhanced version with more options. -h shows a help, -d
12 also converts directories, -r turns recursive operation on, and -u would
13 convert into uppercase instead. So you would use it like that:
14 lowercase -dr *
15 This assumes you also want the directory names converted. Leave the -d
16 option off if not. And add a -t first so you see what would be done, in
17 case the script would mess things up. It seems to work, but was written
18 rather quickly. If all seems okay, start it again without the -t.
19
20 If it still don't work, you can also use the find command to process each
21 file individually:
22 find . -exec lowercase -d \{\} \;
23 Or this if you like to change files only:
24 find . -type f -exec lowercase \{\} \;
25
26 Feel free to email me directly if you are still having trouble, so we won't
27 bother the list.
28
29 Alex
30
31 #!/bin/bash
32
33 # parse options
34 unset oDir oRec oTest oUpper
35 while getopts "dhrtu" opt
36 do
37 case $opt in
38 h )
39 echo "
40 ${0##*/} [-dhrtu] files...
41 Convert files into lowercase
42
43 Options:
44 -d convert directories, too
45 -h show this help
46 -r recursive
47 -t test, show what would be done
48 -u change to uppercase instead
49 "
50 exit 0
51 ;;
52 d ) oDir=true ;;
53 r ) oRec=true ;;
54 t ) oTest=true ;;
55 u ) oUpper=true ;;
56 * ) exit 1
57 esac
58 done
59 shift $(( OPTIND-1 ))
60
61 # decide whether to convert to lowercase or uppercase
62 if [[ $oUpper ]]
63 then
64 from=[:lower:]
65 to=[:upper:]
66 else
67 from=[:upper:]
68 to=[:lower:]
69 fi
70
71 # make * expand to empty string when no files are found
72 shopt -s nullglob
73
74 # loop over arguments
75 while (( $# ))
76 do
77 aFile=$1
78 shift
79
80 # check if file exists
81 if ! [[ -e $aFile ]]
82 then
83 echo "File not found: '$aFile'"
84 continue
85 fi
86
87 # process directories recursively first
88 if [[ -d $aFile ]] && [[ $oRec ]] && [[ $aFile/* ]]
89 then
90 $0 ${oDir:+-d} ${oTest:+-t} ${oUpper:+-u} -r "$aFile"/*
91 fi
92
93 # skip directories without -d option
94 [[ -d $aFile ]] && ! [[ $oDir ]] && continue
95
96 # create new name
97 dir=$( dirname "$aFile" )
98 base=$( basename "$aFile" )
99 newFile=${dir:+$dir/}$( echo "$base" | tr "$from" "$to" )
100
101 # rename file if necessary
102 [[ $aFile -ef $newFile ]] ||
103 ${oTest:+echo} mv -v "$aFile" "$newFile"
104 done
105
106
107 wonko@weird test --> ../lowercase -rd *
108 »A/A/X« -> »A/A/x«
109 »A/A/Y« -> »A/A/y«
110 »A/A« -> »A/a«
111 »A« -> »./a«
112 »B/B« -> »B/b«
113 »B« -> »./b«
114 »C/X« -> »C/x«
115 »C« -> »./c«
116 »X« -> »./x«
117 »Y« -> »./y«
118 --
119 gentoo-user@g.o mailing list