Chinaunix

标题: perl获取指定文件夹下的以cc开头的文件 [打印本页]

作者: ice_ma    时间: 2014-10-27 18:53
标题: perl获取指定文件夹下的以cc开头的文件
本帖最后由 ice_ma 于 2014-10-28 09:59 编辑

要显示的路径是这个/root/cs/release/,下面有很多各种各样格式的文件,现在想要显示以cc.开头的文件,比如cc.1212   cc.5435   cc.2231   cc.adasd.213等等类似这种格式的文件信息,搜了论坛改了下面这个脚本,但是是把/root/cs/release/下面的所有文件都显示了,

代码如下:现在想要只显示5分钟内产生的以cc.开头的文件~谁帮忙改改
  1. #!/usr/bin/perl
  2. opendir(DIR,'/root/cs/release/');
  3. while(my $file = readdir(DIR))
  4. {
  5.           next if ($file eq '.');
  6.           next if ($file eq '..');
  7.           print $file,"\n";

  8. }
  9. closedir(DIR);
复制代码

作者: huang6894    时间: 2014-10-27 19:58
额。。。。glob ?
作者: stanley_tam    时间: 2014-10-27 20:02
本帖最后由 stanley_tam 于 2014-10-27 20:03 编辑

这样?{:3_193:}
  1. #!/usr/bin/perl
  2. opendir(DIR,'/root/cs/release/');
  3. while(my $file = readdir(DIR))
  4. {
  5.           print $file,"\n" if index($file, 'cc.') == 0;
  6. }
  7. closedir(DIR);
复制代码

作者: ice_ma    时间: 2014-10-28 09:59
回复 3# stanley_tam

这样显示不出来,什么文件都没显示


   
作者: kingfighters    时间: 2014-10-28 11:18
本帖最后由 kingfighters 于 2014-10-28 11:19 编辑

回复 1# ice_ma
  1. #!/usr/bin/perl
  2. opendir(DIR,'/root/cs/release/');
  3. while(my $file = readdir(DIR))
  4. {
  5.           next if ($file eq '.');
  6.           next if ($file eq '..');
  7.           if ($file =~ /^ss/ ){
  8.               print $file,"\n";
  9.           }

  10. }
  11. closedir(DIR);
复制代码

作者: kingfighters    时间: 2014-10-28 11:21
回复 1# ice_ma

噢,还有个五分钟的限定,那你就在if的加上and stat($file)[10]-now > 300 这类的设定吧


   
作者: ice_ma    时间: 2014-10-28 12:05
回复 5# kingfighters


这样可以了,谢谢。
   
作者: ice_ma    时间: 2014-10-28 12:07
回复 6# kingfighters
  1. #!/usr/bin/perl
  2. opendir(DIR,'/root/cs/release/');
  3. while(my $file = readdir(DIR))
  4. {
  5.           next if ($file eq '.');
  6.           next if ($file eq '..');
  7.         if ($file =~ /^cc/) and (stat($file)[10]-now > 300) {   
  8.         print $file,"\n";
  9.                                }
  10. }
  11. closedir(DIR);
复制代码
这样写直接报错了,错误如下:
  1. syntax error at ./jk.pl line 7, near ") and"
  2. syntax error at ./jk.pl line 10, near "}"
  3. Execution of ./jk.pl aborted due to compilation errors.
复制代码
感觉perl好难啊

   
作者: huang6894    时间: 2014-10-28 13:19
本帖最后由 huang6894 于 2014-10-28 13:20 编辑

噗哧~
  1. #! /usr/bin/perl -w
  2. # File Name: t.pl

  3. my $now = time();
  4. my $dir = shift;
  5. my @file = glob("$dir/cc*");
  6. for my $f (@file){
  7.         my $t = (stat "$f")[9];
  8.         print $f.$/ unless $now - $t > 300;
  9.         
  10. }
复制代码

作者: ice_ma    时间: 2014-10-28 15:01
回复 9# huang6894


    这样写,没有任何输出,/root/cs/release/目录下有新产生的cc文件,但是不显示
作者: ice_ma    时间: 2014-10-28 15:02
回复 9# huang6894



我综合kingfighters 和 huang6894的内容,然后自己改成这样了,但是也不显示5分钟内产生的新cc文件
  1. #!/usr/bin/perl
  2. opendir(DIR,'/root/cs/release/');
  3. my $now = time();
  4. while(my $file = readdir(DIR))
  5. {
  6.           my $t = (stat "$file")[10];
  7.           next if ($file eq '.');
  8.           next if ($file eq '..');
  9.            if ($file =~ /^cc/) {
  10.                 print $file.$/ unless $now - $t > 300;
  11. }       
  12. }
  13. closedir(DIR);
复制代码





欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2