On Monday 09 July 2007 00:00, Alex Schuster wrote:
> Mick writes:
> > Thanks Alex, I was trying your script, but just like Etaoin's script it
> > does not go beyond level 1 in the directory.  All the subdirectories and
> > files within them stay in Capital Case.
> >
> > How can I change it to recursively look into the directory?
>
> That's strange. I tried that and had no problem.
>
> Here's a slightly enhanced version with more options. -h shows a help, -d
> also converts directories, -r turns recursive operation on, and -u would
> convert into uppercase instead. So you would use it like that:
>   lowercase -dr *
> This assumes you also want the directory names converted. Leave the -d
> option off if not. And add a -t first so you see what would be done, in
> case the script would mess things up. It seems to work, but was written
> rather quickly. If all seems okay, start it again without the -t.
>
> If it still don't work, you can also use the find command to process each
> file individually:
>   find . -exec lowercase -d \{\} \;
> Or this if you like to change files only:
>   find . -type f -exec lowercase \{\} \;
>
> Feel free to email me directly if you are still having trouble, so we won't
> bother the list.

After some tests and minor changes that Alex introduced, I have had success 
with Alex's script as follows:
==================================================
#!/bin/bash

##########################################################
#                                                        #
# Change case of files and directories.                  #
#                                                        #
# Written by Alex Schuster ([EMAIL PROTECTED])         #
#                                                        #
# Use at your own risk.                                  #
# Feel free to do anything you want with it.             #
# Please send me an e-mail if you think you improved it. #
#                                                        #
##########################################################


# parse command line options
unset oDir oRec oTest oUpper
while getopts "dhrtu" opt
do
        case $opt in
        h )
                echo "
${0##*/} [-dhrtu] files...
Convert files into lowercase

Options:
  -d  convert directories, too
  -h  show this help
  -r  recursive
  -t  test, show what would be done
  -u  change to uppercase instead
"
                exit 0
                ;;
        d )       oDir=true ;;
        r )       oRec=true ;;
        t )      oTest=true ;;
        u )     oUpper=true ;;
        * )     exit 2
        esac
done
shift $(( OPTIND-1 ))

# decide whether to convert to lowercase or uppercase
if [[ $oUpper ]]
then
        from=[:lower:]
          to=[:upper:]
else
        from=[:upper:]
          to=[:lower:]
fi

if ! (( $# ))
then
        echo "Usage:"
        $0 -h
        exit 1
fi

# loop over arguments
while (( $# ))
do
        aFile=$1
        shift
        
        # check if file exists
        if ! [[ -e $aFile ]]
        then
                echo "File not found: '$aFile'"
                continue
        fi
        
        # is argument a directory?
        if [[ -d $aFile ]]
        then
                # remember original date of directory before renaming contents
                [[ $oTest ]] ||
                        date=$( ls -ld --full-time "$aFile" |
                                awk '{ print $6" "$7 }' |
                                sed 's/.00000*//g' )
                
                # process non-empty directories recursively first
                [[ $oRec ]] && [[ $( ls -A "$aFile" ) ]] &&
                        $0 ${oDir:+-d} ${oTest:+-t} ${oUpper:+-u} -r "$aFile"/*
                
                # skip renaming of directories without -d option
                [[ -d $aFile ]] && ! [[ $oDir ]] && continue
        fi
        
        # create new name
         dir=$( dirname  "$aFile" )
        base=$( basename "$aFile" )
        newFile=${dir:+$dir/}$( echo "$base" | tr "$from" "$to" )
        
        # rename file if necessary
        if ! [[ $aFile -ef $newFile ]]
        then
                ${oTest:+echo} mv -v "$aFile" "$newFile"
                # re-set date of directories
                if [[ -d $newFile ]] && ! [[ $oTest ]]
                then
                        touch -d "$date" "$newFile"
                fi
        fi
done

exit 0
==================================================

Thank you all and particularly Alex for your time and your help!  :)
-- 
Regards,
Mick

Attachment: pgpNBJ80ZaDFW.pgp
Description: PGP signature

Reply via email to