This is just a handy script I use for renaming my music collection. It got to be too much trouble having the first letter of each word capitalised, getting new music with the names all in caps, or all in lowercase…so I decided to make everything lowercase and write a script to do it for me!
#!/usr/bin/perl -w
use strict;
# open 'find' process to list files recursively with paths
open(FIND, "find |");
while(<FIND>) {
chomp;
# don't rename ourself if script in same as executing
next if $_ eq $0;
# first move the file to $name~ and then back to the lowercase original to allow for fat32 ignoring case,
# and therefore claiming that a file with this name already exists
my $name = $_;
my $tmp = $_.'~';
rename($name, $tmp);
rename($tmp, lc($name));
}
close(FIND);
To start off the script just opens a handle on the linux find command with an output pipe and calls it…well…FIND. The while loop will then iterate through each line the find command returns and rename using the rename() function.
The rename function takes in two arguments, the old name (as returned by find) and the new name. As the current returned value we are looking at is stored in the special variable $_, we can pass this as the old file name. We can then just use the lc() function to convert the old mane to lowercase by putting $_ as the argument for lc(). Finally we close the filehandle on FIND. And voila!