## presuming your sample vars for dir, old, new ## Somewhat untested, but . . . use Path::Tiny qw( path ); my $iter = path( $dir )->iterator( { recurse => 1 } ); while( my $path = $iter->() ) { next unless $path->is_file and $path =~ m{\. $old $}x; my $new_path = $path->basename( $old ) . ".$new"; unless( my $ret = $path->move( $new_path ) ) { warn "Problem moving '$path': $ret\n"; } }