[Techtalk] Uppercase to lowercase

Maria Blackmore mariab at cats.meow.at
Thu Dec 5 05:18:55 EST 2002


On Wed, 4 Dec 2002, Phil Savoie wrote:

> Was wondering if anyone had a script they would like to share to convert 
> upercase filenames to lowercase?

Hi

I got a bit bored, so I hacked together some pretty (evil! ;) bash script
to do it for you, it requires the GNU shell utils or an equivalent to be
installed in order to work.  I think i've pretty much covered everything
and tried to make it as safe as possible.  If you find any bugs, let me
know :)

run it with -h for usage, it'll probably be happiest in /usr/local/bin
though i've made it not care where it is, and it won't mind what you call
it.

yes, it's so totally overengineered, but I loved the challenge :)

I've attached it to this email as a plain text attachment, for ease of
saving to disk

-- 
Maria
BSSFH
(Bitch Shell Scripter From Hell)
-------------- next part --------------
#!/bin/bash

movefile() {
	DEST=`echo $1 | tr 'A-Z' 'a-z'`
	if [ -e $DEST ]; then
		if [ "$2" != "-f" ]; then
			echo -e "$WHATIAM: File $DEST would be overwritten by renamed $1\n(-f not specified)"
			exit 1
		else
		echo "Overwriting $DEST, as requested by -f"
		fi
	fi
	
	mv -f $1 $DEST 2> /dev/null
	if [ $? -ne 0 ]; then
		echo "$WHATIAM: Error renaming file $1"
		exit 1
	fi
	
}

printhelp() {
	echo
	echo "Filename case convertor, Copyright (c)2002 Maria Blackmore"
	echo
	echo "Converts any and all upper case letters in filenames in the specified"
	echo "directory to lower case."
	echo
	echo "Usage:	$WHATIAM [-f] [directory]"
	echo
	echo "	-f	Force overwriting of destination files if they already exist"
	echo "	-h	This help message"
	echo
	exit 0
}


WHEREIAM=`dirname $0`
WHATIAM=`basename $0`

if [ -z "$1" ]; then
	printhelp
fi

if [ "$1" == "-h" ]; then
	printhelp
fi

if [ "$1" == "-m" ]; then
	movefile $2 $3
	exit 0
fi

if [ "$1" == "-f" ]; then
	DASHF="-f"
	if [ -z "$2" ]; then
		printhelp
	fi
	DIRECTORY=`dirname $2`
else
	DIRECTORY=`dirname $1`
fi

if [ $WHATIAM == "$0" ]; then
	WHEREIAM=""
elif [ "$WHEREIAM" == "." ]; then
	WHEREIAM=`pwd`
elif [ "$WHEREIAM" == ".." ]; then
	WHEREIAM=`pwd`/..
elif [ `echo $WHEREIAM | cut -c1` != "/" ]; then
	FOO=$WHEREIAM
	WHEREIAM=`pwd`/$FOO
fi

if [ "$DIRECTORY" != "." ]; then
	if [ -d $DIRECTORY ]; then
		echo "Entering $DIRECTORY"
		cd $DIRECTORY 2> /dev/null
		if [ "$?" -ne 0 ]; then
			echo -e "$WHATIAM: Error entering directory $DIRECTORY"
			exit 1
		fi
	else 
		echo "$WHATIAM: No such directory $DIRECTORY"
		exit 1 
	fi 
fi

find . -maxdepth 1 | grep \[:A-Z:\] | xargs -i $WHEREIAM/$WHATIAM -m {} $DASHF

if [ "$DIRECTORY" != "." ]; then
	cd -
fi


More information about the Techtalk mailing list