MLUG: Re: [MLUG] command to change case in filenames?
Re: [MLUG] command to change case in filenames?
Email address obfuscation in effect -- please click here to turn it off.

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
On Wed, 2 Oct 2002, Michael wrote:

> The 'tr' command can be used to change a filename to lowercase, remove
> spaces, etc.

You mean like this:

# echo "FILENAME" | tr '[A-Z]' '[a-z]'
filename

That's the way I was going to approach this, in a tcsh script like this
one:

----------begin script on next line------------
#!/usr/local/bin/tcsh -f

# lcf - translate filenames to lowercase and renames files
# Syntax:
# lcf filename
# Wildcards are allowed but * and ? should be escaped, that is,
# entered as \* and \?.  If a path is entered, as in
# lcf dir/subdir/filename
# then all dirs and subdirs must already be lowercase.

foreach file ("`\ls -1 $*`")
   set oldname="$file"
   set newname=`echo $file | tr '[A-Z]' '[a-z]'`
   mv "$oldname" "$newname"
end
----------end script on previous line----------


The benefit of that script is that you simply give it the filenames just
as you would give them to ls.  For example, if I were to name the script
lcf for "lowercase filenames", I could run this command:

lcf [LSW]* NO* ABCNEWS.txt APNI? www/junk/[A-Z]*

and it would find and rename all of the indicated files appropriately.
This is because the "$*" on the first line reads in all of the command
line arguments and sends them to ls ("\ls" to override aliases and "-1" to
make a single column), but there is one quirk to this:  it is safer to use
\* and \?  instead of * and ?.  For example:  lcf * doesn't work but
lcf \* does work.  The script seems to handle filenames with spaces just
fine.  It will not recurse, but it will work on files in subdirectories so
long as the subdirectories already have lowercase filenames.

Is this any better or worse than the other solutions?  (perl and bash?)

Mike

--
To unsubscribe, go to http://mlug.missouri.edu/members/edit.php

Archives are available at http://mlug.missouri.edu/list-archives/