Perl – Mass file renaming

by

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.

#!/usr/bin/perl

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; 
Advertisements

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:

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s


%d bloggers like this: