lowercase script

Kevin Buettner kev@primenet.com
Thu, 27 Jul 2000 15:11:29 -0700


On Jul 27,  2:42pm, Lucas Vogel wrote:

> I remember someone asking a question like this and getting an answer posted,
> so here goes. I don't have my Bash book in hand...
> 
> How do I rename files that are all uppercased to all lowercase in a script? 

--- rename-to-lower ---
#!/usr/bin/perl -w

foreach my $uname (@ARGV) {
    next unless $uname =~ /[A-Z]/;
    my $lname = $uname;
    $lname =~ tr [A-Z] [a-z];
    if (-e $lname) {
	rename $lname, "$lname~";
	print "Warning: $lname already exists; renamed to $lname~\n";
    }
    rename $uname, $lname;
}
--- end rename-to-lower ---

Here's an example of it's use:

ocotillo:tst$ ls
ocotillo:tst$ echo A >A
ocotillo:tst$ echo CaT >CaT
ocotillo:tst$ echo cat >cat
ocotillo:tst$ rename-to-lower A CaT
Warning: cat already exists; renamed to cat~
ocotillo:tst$ ls
a  cat  cat~
ocotillo:tst$ cat a
A
ocotillo:tst$ cat cat
CaT
ocotillo:tst$ cat cat~
cat