How to efficiently delete a long list/array of files and dirs in perl
T开发者_开发知识库his 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?
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.
精彩评论