[comp.unix.questions] lcase - Shell script to translate filenames to lower case

wes@engr.uky.edu (Wes Morgan) (09/25/87)

I realize this may be a trivial thing, but there's no reason for
hundreds of people to reinvent the wheel.

I'm one of those people who despise uppercase letters in filenames.
Therefore, I developed this shell script to translate any upper-
case letters in filenames to lowercase.

LCASE can also be used on directories.  The argument may be either a
filename or a directory name.

This script was written for sh under System V.


#
# lcase - shell script to convert any uppercase letters in file
#         name or directory to lowercase
#
# Written by wes@ukecc.uucp, 25 Sep 87

if [ $1 ]                                                 # Is there an arg? 
   then
# Is it a directory?
      if [ -d $1 ]                                        
         then
            ls $1 | (while read filnam                    # List the files
               do
                  newname=`echo $filnam | tr "[A-Z]" "[a-z]"` # Translate chars
                  if [ $filnam != $newname ]              # Is is the same now?
                     then
                        mv $1/$filnam $1/$newname         # If not, change it
                  fi
               done)
         else
# If not a directory, is it a file?
           if [ -r $1 ]                                   
              then
                 newname=`echo $1 | tr "[A-Z]" "[a-z]"`  # Translate chars    
                 if [ $1 != $newname ]                   # Is it the same?
                    then
                       mv $1 $newname                    # If not, change it
                 fi
              else 
                 echo lcase: $1 not found                # File not found
           fi
      fi
   else
# No argument specified, display usage 
      echo "Usage: lcase {filename | directoryname}" 
fi
#
# End of shell script
#