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.

        Alex

#!/bin/bash

# parse 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 1
        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

# make * expand to empty string when no files are found
shopt -s nullglob

# loop over arguments
while (( $# ))
do
        aFile=$1
        shift
        
        # check if file exists
        if ! [[ -e $aFile ]]
        then
                echo "File not found: '$aFile'"
                continue
        fi
        
        # process directories recursively first
        if [[ -d $aFile ]] && [[ $oRec ]] && [[ $aFile/* ]]
        then
                $0 ${oDir:+-d} ${oTest:+-t} ${oUpper:+-u} -r "$aFile"/*
        fi
        
        # skip directories without -d option
        [[ -d $aFile ]] && ! [[ $oDir ]] && continue
        
        # create new name
         dir=$( dirname  "$aFile" )
        base=$( basename "$aFile" )
        newFile=${dir:+$dir/}$( echo "$base" | tr "$from" "$to" )
        
        # rename file if necessary
        [[ $aFile -ef $newFile ]] ||
                ${oTest:+echo} mv -v "$aFile" "$newFile"
done


[EMAIL PROTECTED] test --> ../lowercase -rd *
»A/A/X« -> »A/A/x«
»A/A/Y« -> »A/A/y«
»A/A« -> »A/a«
»A« -> »./a«
»B/B« -> »B/b«
»B« -> »./b«
»C/X« -> »C/x«
»C« -> »./c«
»X« -> »./x«
»Y« -> »./y«
--
[EMAIL PROTECTED] mailing list

Reply via email to