开发者

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

开发者 https://www.devze.com 2023-03-11 21:38 出处:网络
T开发者_开发知识库his is how I currently delete files and directories recursively foreach my $row(keys %$rows)

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号