use File::Wildcard; my $wc = File::Wildcard->new( path => './//', match => qr{^(.*?Folder_\d+)/(\w+.jpg)$}, derive => [ '$1_$2' ], ); while (my $match = $wc->next) { my ($from,$to) = @$match; print "Renaming $from to $to\n"; rename $from $to; }