Perl – Mass file renaming


When I usually have to mass rename files, I use the rename command provided by Debian and its derivatives (AFAIK). That is a good option when you can fit your renaming needs in the perl regex format. However, there are times when you need to mass rename files in a totally different way. While I was googling for this hoping that there would something similar to rename, I came across this cool post which has the solution in BASH as well as Perl. I am pasting the Perl solution below just coz I thing its much more cleaner than the bash version. You can get the other solutions here.


my $dir = '/path/to/images/directory';
my $names = '/path/to/file/with/the/wanted/names';
my $files = '/path/to/file/with/the/real/name';

chdir $dir or
die "Could not change dir to $dir: $!\n";

open NAMES, $names or die "Could not open $names: $!\n";

open FILES, $files or die "Could not open $files: $!\n";

my $name = <NAMES>;
chomp $name;

my $file = <FILES>;
chomp $file;

while($name && $file) {
print "renaming $file to $name...\n";
rename $file, $name;
$name = <NAMES>;
chomp $name;
$file = <FILES>;
chomp $file;

close NAMES;
close FILES; 

One Response to “Perl – Mass file renaming”

  1. vedang Says:

    interesting.. but you should also take a look at emacs dired-mode. I find it much easier to do mass renaming or copying from there. 🙂

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: