#! /bin/bash # (c) Joe Klemmer # Licensed under the Public Domain. # This script can be used to rename files to all upper case or lower case. # # NOTE: There is currently no error checking to make sure the target # directory exists. This would be a good check to add. # # NOTE: The above check was implimented as a simple check of $? after # the cd command. # Set the usage information. USAGE="Usage: $0 dir"; # Make sure that two parameters were passed on the command line. # If not display the usage information and exit. if [ $# -lt 2 ]; then echo $USAGE; exit 1; fi # Check to see which way we are converting; u -> l or l -> u. # Throw an error if something other than "u" or "l" is passed as # the second parameter. # If not display the usage information and exit. case $1 in "u") CASETO="[:upper:]" # All upper CASEFROM="[:lower:]" # All lower ;; "l") CASETO="[:lower:]" # All lower CASEFROM="[:upper:]" # All upper ;; *) echo $USAGE exit 1 esac # Save the current directory then cd into the directory passed # on the command line in the second parameter. The directory # passed must be either cannonical or relitive to the current # directory. CURDIR=`pwd`; cd $2; if [ $? -ne 0 ]; then echo "Target directory must be valid and either cannonical \ or relitive to the current working directory." exit 1 fi # Do the actual renaming of the files. It is left as an excersize for the # reader to figure out the logic used here. for i in `ls` do UCASE=`ls $i | tr $CASEFROM $CASETO`; if [ $i != $UCASE ]; then echo "Moving $i to $UCASE"; mv $i $UCASE 2> /dev/null; fi done # Change back to the original working directory and exit. cd $CURDIR; exit 0;