perl如何遍历指定文件夹下的指定扩展名文件,并按时间顺序要求删除
2016-06-07 10:38
621 查看
(linux下)要求是,遍历/ccd/pa/下的所有后缀为log的文件(包括所有子文件夹),只保留时间顺序离现在最近的20个文件,其它删除。
;my $num = 20; my $pattern = 'log$'; #my $basedir = '/ccd/pa/'; my @files = (); my @dirs = ($basedir); die "error $basedir: $!" unless(-d $basedir); # recursively find all files while(@dirs){ $d = $dirs[0]; $d .= "/" unless($d=~/\/$/); for my $f (glob($d . '*')){ push(@dirs, $f) if(-d $f) ; push(@files,$f)if(-f $f && $f=~/$pattern/); } shift @dirs; } #sort according to modified-time @files=sort{ @sta1=stat($a); @sta2=stat($b); $sta2[9]<=>$sta1[9]; }@files; # delete files earlier than the $num-th @files2del = @files[$num..@files-1]; my $cnt = 0; if(@files2del){ local $"="\n\t"; print "going to delete these files:\n\t@files2del\n[y/N]?"; my $choice = <>||'N'; $cnt = unlink(@files2del)if($choice =~ /^\s*y\s*$/i); } print "$cnt files deleted\n";
觉得不错,转载下,来自百度知道
相关文章推荐
- Spring Autowired错误???
- Inside Qt Series (全集)
- Spring IOC
- 【转】JavaScript 正则表达式上——基本语法
- tjut 5296
- hdu 1717 && 计蒜课 52 【分数小数的互相转化】【数论?】
- Java多线程(三)
- 通知中心,代理,KVO详细解释与对比
- 数据库编程细节问题 -- 3.mysql中拼接字符串
- Android DynamicGrid:拖曳交换位置
- mybatis Mapped Statements collection already contains value for
- Python模块之---Pexpect
- 常见的文件以级4大对象
- java 简单权限管理设计
- Android DynamicGrid:拖曳交换位置
- 制作一个听话的电影种子挖掘器
- linux 杂记
- IPv6-only 的兼容性解决方案-b
- 改变服务器sshd 的22的端口
- [javaSE] IO流(管道流)