如何在 perl 中有效删除一长串文件和目录
这就是我目前递归删除文件和目录的方式。
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";
}
有数十万个文件/目录需要删除,所以我希望看到一个比为每个文件/目录调用“rm -rf”更好的解决方案。
也许将文件/目录列表合并到数组中,然后将该数组传递给单个“rm -rf”调用?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用 File::Path 中的
rmtree
。除了可移植之外,它还使用 Perl 的内置 unlink 而不是每次都启动整个 shell当您需要删除目录时,这就是您现在正在做的事情。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.