To: ilug@linux.ie Subject: Re: [ILUG] Script junkies Date: Tue, 10 Apr 2001 16:55:24 +0100 From: Colm Buckley > Can someone tell me how I would rename all files in a folder and subfolders > to lowercase filenames please. Assuming you want to rename the directories as well as the files: find /path/to/folder -print | while read x ; do mv $x $(echo $x | tr 'A-Z' 'a-z') done ... should do the trick. If you don't want to rename the directories, things are a little trickier; you'll have to pass the filename part to tr, not the directory part: find /path/to/folder -type f -print | while read x ; do mv $x $(dirname $x)/$(basename $x | tr 'A-Z' 'a-z') done Colm -- Colm Buckley, NewWorld Commerce, 16 South Cumberland St., Dublin 2, Ireland. Personal: +353 87 2469146 / colm@tuatha.org / http://www.tuatha.org/~colm/ Business: +353 1 4334334 / colm@nwcgroup.com / http://www.nwcgroup.com/ You need to log into www.clue.com and issue the GET command. Date: Tue, 10 Apr 2001 18:37:47 +0100 From: Padraig Brady To: Fergal Moran CC: "'ilug@linux.ie'" Subject: Re: [ILUG] Script junkies Fergal Moran wrote: > Can someone tell me how I would rename all files in a folder and subfolders > to lowercase filenames please. > > Cheers, > > Fergal > -- > WASP Technologies You could do this a few ways. The bottom is most efficient by far. note run the "find case lint" component of http://freashmeat.net/projects/fslint if you're worried about loosing files where there is a case clash ------------------------------------------- find root_of_cd -depth -exec mv {} `echo {} | tr A-Z a-z` \; ------------------------------------------- #!/bin/sh #pass either filenames or nothing #note doesn't touch directories if [ $# = 0 ] then FILES=$(find -type f -maxdepth 1) else FILES="$@" fi for FILE in $FILES do DEST=$(echo $FILE | tr A-Z a-z) if [ "$DEST" != "$FILE" ] #if file already lowercase then mv -i "$FILE" "$DEST" #-i prompts for confirmation (-f is opposite) fi done ------------------------------------------- #!/bin/sh # change any uppercase letters in file and directory # names in the current directory and below. # note filenames with \1 or \n will cause problems # note this is still much slower than it could be, # as there is a seperate mv executed for each file/dir. # It would be much more efficient to pass the output # of this to a python script for e.g. that does the rename find -depth \( -type f -o -type d \) | grep -E "/[^/]*[A-Z]+[^/]*$" | #only process upcase names sed -e ' { #escape any chars that could cause problems #s/\([]"{}\$]\)/\\\1/g #keep orig path in hold space h #put basename in pattern space s/.*\/\([^/]*$\)/\1 #lowercase basename y/ABCDEFGHIJKLMNOPQRSTUVWXYZ/abcdefghijklmnopqrstuvwxyz/ #exchange pattern and hold space x #format mv args 1 s/\(.*\/\)\([^/]*$\)/\1\2/ #format mv args 2 G }' | tr -d '\n' | #format mv args 3 tr 'xargs -r0 -n2 mv #execute mv ------------------------------------------- Date: Tue, 10 Apr 2001 20:57:00 +0100 From: Conor Daly To: ilug@linux.ie Subject: Re: [ILUG] Script junkies X-Mailer: Mutt 1.0.1i On Tue, Apr 10, 2001 at 05:18:24PM +0100 or so it is rumoured hereabouts, Fergal Moran thought: >> Assuming you want to rename the directories as well as the files: >> >> find /path/to/folder -print | while read x ; do >> mv $x $(echo $x | tr 'A-Z' 'a-z') >> done > That almost worked - it did everything in the root folder and one directory > deep - but did not work any deeper than that - while it was running there > were a load of messages like > > mv: cannot stat `/home/www/docs/oracle/server.815/A58231/CH2A.GIF': No such > file or directory I'd be inclined to think that the directory "A58231" has been mv'ed to "a58231" _after_ "CH2A.GIF" has been *found* but _before_ it gets mv'ed so the path is no longer there. To avoid that, you'd need to do some kind of recursive directory suck where you descend th structure to the end of a branch, mv everything in there, go up one level, repeat for each leaf directory there before mv'ing the contents of that directory in turn. I had to do something of that nature for my MP3 indexing script (a nice little something that will index all the mp3's in a branch with individual indexes for each directory plus group indexes in the directories above. It may be available from my webpage (www.geocities.com/~conor_daly) but if not, it will be some day.) Something like the following should do it: ####################### #!/bin/bash for i in `find -name \* -maxdepth 1 -type d | xargs`; do cd $i $0 cd .. done for i in `find -name \* -maxdepth 1 | xargs`; do #whatever the clever mv | tr command was goes here done ####################### That looks like the thing. It will descend a directory structure, node by node and only when it reaches a leaf directory will it start renaming. It'll then rename everything in the current directory, go up one directory and repeat for any other directories in that dir before renaming everything in the then current directory. Conor -- Conor Daly Domestic Sysadmin :-) --------------------- Faenor.cod.ie 9:32pm up 42 days, 7:07, 0 users, load average: 0.00, 0.00, 0.00 Hobbiton.cod.ie 8:49pm up 42 days, 5:59, 1 user, load average: 0.00, 0.04, 0.02 To: linux-questions-only@ssc.com Subject: Re: [TAG] Upper to lower case using shell utilities From: Kapil Hari Paranjape Date: Tue, 3 Dec 2002 13:59:24 +0530 > Can I change characters from upper to lower case and vice-versa usiing > shell utilities and without using sed/awk ? Always fun to be the first one to spot an easy one! From "Unix 101" ( :-) ) and "man tr": | tr '[[:lower:][:upper:]]' '[[:upper:][:lower:]' | Kapil. #!/bin/sh # lowercase any filenames with uppercase chars for file in $* do if [ -f $file ] then lcfile=`echo $file | tr [:upper:] [:lower:]` if [ $file != $lcfile ] then mv -i $file $lcfile fi fi done