Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

perl

xargs

rm

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?

like image 629
Alex Avatar asked Jan 23 '26 22:01

Alex


1 Answers

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.

like image 123
friedo Avatar answered Jan 26 '26 19:01

friedo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!