my $dir = "/path-to/some-dir"; while () { opendir(DIR, $dir) || do { # my error code here... }; for my $filefound (grep /\.foo$/, readdir DIR) { my $fullPath = File::Spec->catdir($dir, $filefound); # immediately rename the file my $newPath; ($newPath = $fullPath) =~ s/foo/bar/; rename ($fullPath, $newPath) || do { # my error code here... }; my $kidPid; unless (defined $kidPid = fork()) { # my error code here... } next if $kidPid; # parent continues with dir grep # I'm the child, I process the file then exit } }