su
chmod +x rename.pl
#!/usr/bin/perl
#rename.pl - rename filenames with Perl regexp substitution
($regexp = shift @ARGV) || die "Usage: rename perlexpr [filenames]\n";
if (!@ARGV) {
@ARGV = <STDIN>;
chomp(@ARGV);
}
foreach $_ (@ARGV) {
$old_name = $_;
eval $regexp;
die $@ if $@;
rename($old_name, $_) unless $old_name eq $_;
}
exit(0);
rename.pl 's/^[^\d]+/example/' a1.jpg b2.jpg cde3.jpg
#!/usr/bin/perl
$testmode = 0; #false
if ($ARGV[0] eq '-t') {
$testmode = 1; #true
shift;
}
($regexp = shift @ARGV) || die "Usage: rename [-t] perlexpr [filenames]\n-t : option to test action (nothing really done)\n";
if (!@ARGV) {
@ARGV = <STDIN>;
chomp(@ARGV);
}
print "Test mode\n" if ($testmode);
foreach $_ (@ARGV) {
$old_name = $_;
eval $regexp;
die $@ if $@;
unless ($old_name eq $_) {
print "$old_name -> $_\n";
rename($old_name, $_) unless $testmode;
}
}
exit(0);
It looks like you're new here. If you want to get involved, click one of these buttons!