#!/usr/bin/perl -w $CMD=''; $HELP=0; $YES=0; $rename_try=0; $rename_ok=0; $ERR=''; while($_=shift @ARGV) { if(/^s([\/%]).+\1[gi]*/) { $CMD=$_; } elsif(/^-y/) { $YES=1; } else { $HELP=1 }; } if(!defined $CMD || $CMD eq'' || $HELP) { print STDERR "Mass substitution rename tool. Usage:\n ls *some*files* | $0 s/some/other/ [-y] s/some/other/ Substitution operator (sed/perl alike) -y Do actual rename \n"; exit 64; } while(){ chomp; $new=$_; eval "\$new=~$CMD"; if($new eq $_) { print "# leaving $_\n"; next; }; print qq%mv "$_" "$new"\n%; if($YES) { $rename_try++; if( rename($_, $new) ){ $rename_ok++; } else { print "# ^^^ ERROR RENAMING \"$_\"\n"; $ERR.=qq%Error renaming "$_" to "$new"\n%; } }; } my $bad=$rename_try-$rename_ok; print STDERR "Renames done: $rename_ok", $bad?", failed: $bad":'', "\n"; if($ERR ne''){ print STDERR $ERR; exit 1; }