删除文件夹中除最近的 perl 之外的所有内容
我试图将一些清理工作合并到我的输出目录中,其中除最近的文件之外的所有文件都将被删除。
my @files = grep{ -f && -t } glob($outputdir);
349
350 my @expected_file = grep { /SequenomComparisonSummary\_(\d+)\.txt/} @files;
351 foreach my $file(@expected_file){
352
353 unlink $file;
354 warn "Removing file $file\n";
355 }
但我想保留目录中最近的内容并删除其余的?有没有更简单的方法来做到这一点?
非常感谢您的建议。
I am trying to incorporate a bit of cleaning up to my output directory, in which all the files other the very recent ones are to be deleted.
my @files = grep{ -f && -t } glob($outputdir);
349
350 my @expected_file = grep { /SequenomComparisonSummary\_(\d+)\.txt/} @files;
351 foreach my $file(@expected_file){
352
353 unlink $file;
354 warn "Removing file $file\n";
355 }
But I want to keep the very recent ones in the directory and delete the rest? Is there a simpler way to do this ?
Thanks a lot for your suggestions.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
怎么样:(
或者您可以使用
-M > 7
作为grep
条件的一部分。)
grep
条件中的-t
可能没有按照您的想法进行。来自文档:因此,代码中的
-t
正在检查STDIN
是否是终端,我认为这不是您真正想要检查的。How about:
(Or you can use
-M > 7
as part of yourgrep
condition.)Ps. The
-t
in yourgrep
condition is probably not doing what you think it is. From the docs:So the
-t
in your code is checking whetherSTDIN
is a terminal, which I'd assume is not what you really wanted to check.我在我的网站上发布了一个脚本来仔细执行此操作(在 Windows 环境中)并允许进行试运行。该脚本位于 http://www.billruppert.com/2009/05 /deleting-old-files.html。这里复制的有点长。
I posted a script on my site to do this carefully (in a Windows environment) and to allow for a dry run. The script is at http://www.billruppert.com/2009/05/deleting-old-files.html. It's a bit long to copy here.