[mdlug] Hideous script for recursively renaming a directory tree to lower case

Aaron Kulkis akulkis3 at hotpop.com
Mon Feb 4 16:27:46 EST 2008


Jeff Hanson wrote:
> I was trying to get an old application to work under Wine.  As you all
> know, file and directory names are case sensitive in Linux but not in
> Windows.  Wine hides the differences from the Windows applications.
> When an executable update is applied to a Windows app that is designed
> to overwrite existing files, Wine treats them as case insensitive so
> the correct ones get clobbered.  But in the case of the app I was
> working with, the update is a zip file that needs to be extracted over
> the existing app tree in order to replace some of the files in various
> subdirectories.  Just to make it fun, the file names in the zip had
> different case than the target tree.  Not just all upper or lower case
> either - lots of random CamelCase also.  Since Wine hides the case
> from the app renaming everything to lower case doesn't affect it so I
> went looking for a shell script, command, or graphical utility to do
> that.  I found lots of script examples and apps but none of them
> worked.  The problem is that none of them can handle renaming
> directories recursively, especially more than one level deep.  The
> "find" command can exec another command like "rename" but it parses
> the full tree first then runs the app which then fails as soon as it
> tries to access a subdirectory of a directory it just renamed.  I
> threw together this hideous script that exhaustively parses the tree
> but does anyone have a better method?  My script handles hidden file
> names with spaces and other nonsense and I attached a tar.gz of the
> tree I was testing against.


You didn't take advantage of the find command:

I would approach it like this:


#!/bin/bash
# recursively rename files and directories by translating
# upper case letters to lower case.
#
# Note -- beware of filename collisions when updating with
# files from microsoft platforms:
#
# 1.  Run once to convert all files to lowercase BEFORE
#     installing update files
#
# 2. Install updated MS-platform files
#
# 3. Run again, in event of name collision,
#     new files will overwrite older files
#
#
#
# usage:  scriptname
#
# No arguments... operates on the contents of the current directory
#
# Date		Author			Action
# ------------	-----------------------	---------------------------
# 04 Feb 2008	Aaron R. Kulkis		0.1 Code (untested)


filetolower()
{ FILENAME=$1
   NEWFILENAME=`echo $FILENAME | tr [:upper:] [:lower:] `
   mv $FILENAME $NEWFILENAME
}

find . -print -exec  filetolower \{\}\;






More information about the mdlug mailing list