3

This is how I currently delete files and directories recursively

foreach my $row(keys %$rows)
{
    my $md5 = $rows->{$row}->{'md5'};
    my $path = "/some/path/jpg/".substr( $md5, 0, 3 )."/$md5";

    `rm -rf $path`;
    print "removed - ".$path."\n";
}

There are hundreds of thousands of files/dirs that need to be deleted, so I would like to see a better solution other than calling "rm -rf" for each file/dir.

Maybe combine a list of files/dirs in array and then pass this array to a single "rm -rf" call?

Alex
  • 407
  • 4
  • 11

1 Answers1

10

Use rmtree from File::Path. In addition to being portable, it uses Perl's builtin unlink instead of firing up a whole shell every time you need to delete a directory, which is what you're doing now.

cjm
  • 61,471
  • 9
  • 126
  • 175
friedo
  • 65,762
  • 16
  • 114
  • 184
  • Does it mean that `rmtree` still goes inside the `foreach` loop and runs hundreds of thousand times? – Alex Jun 09 '11 at 04:34