my $pre="image"; my $post="jpg"; my @files=<$pre.*.jpg>; @files=reverse sort @files; #should work because you don't have image.10.jpg #if you do have image.10.jpg, then you will need to use #image.09.jpg for example foreach (@files) { next unless m/^$pre\.(\d)\.$post$/; #just to be sure rename_and_log($_, "$pre." . ($1 >= 8 ? 9 : $1+1) . ".$post\n"); }