How to efficiently remove long list / array of files and dirs in perl

This is how I am currently recursively deleting files and directories

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 / dirs files that need to be deleted, so I would like to see a better solution besides calling "rm -rf" for each / dir file.

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

+3
source share
1 answer

Use rmtreefrom File :: Path . In addition to portability, it uses Perl's built-in unlink instead of launching the entire shell every time you need to delete the directory that you are doing now.

+10
source

All Articles