#!/usr/bin/perl # # renum.pl blah*.jpg # blah1.jpg .. blah9.jpg blah10.jpg .. blah99.jpg blah100.jpg # -> blah001.jpg .. blah010.jpg .. blah100.jpg foreach ( @ARGV ) { next unless ($stem, $digits, $ext) = /(.*?)(\d+)\.([^\.]+)$/; $max = $len if ($len = length( $digits )) > $max; print "$_ $1\n"; } $fmt = "$stem\%0${max}d.$ext"; foreach ( @ARGV ) { next unless ($stem, $digits, $ext) = /(.*?)(\d+)\.([^\.]+)$/; $new = sprintf $fmt, $digits; rename $_, $new or warn $!; }