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.开头的文件~谁帮忙改改
#!/usr/bin/perl
opendir(DIR,'/root/cs/release/');
while(my $file = readdir(DIR))
{
next if ($file eq '.');
next if ($file eq '..');
print $file,"\n";
}
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:}
#!/usr/bin/perl
opendir(DIR,'/root/cs/release/');
while(my $file = readdir(DIR))
{
print $file,"\n" if index($file, 'cc.') == 0;
}
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
#!/usr/bin/perl
opendir(DIR,'/root/cs/release/');
while(my $file = readdir(DIR))
{
next if ($file eq '.');
next if ($file eq '..');
if ($file =~ /^ss/ ){
print $file,"\n";
}
}
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
#!/usr/bin/perl
opendir(DIR,'/root/cs/release/');
while(my $file = readdir(DIR))
{
next if ($file eq '.');
next if ($file eq '..');
if ($file =~ /^cc/) and (stat($file)[10]-now > 300) {
print $file,"\n";
}
}
closedir(DIR);
复制代码
这样写直接报错了,错误如下:
syntax error at ./jk.pl line 7, near ") and"
syntax error at ./jk.pl line 10, near "}"
Execution of ./jk.pl aborted due to compilation errors.
复制代码
感觉perl好难啊
作者:
huang6894
时间:
2014-10-28 13:19
本帖最后由 huang6894 于 2014-10-28 13:20 编辑
噗哧~
#! /usr/bin/perl -w
# File Name: t.pl
my $now = time();
my $dir = shift;
my @file = glob("$dir/cc*");
for my $f (@file){
my $t = (stat "$f")[9];
print $f.$/ unless $now - $t > 300;
}
复制代码
作者:
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文件
#!/usr/bin/perl
opendir(DIR,'/root/cs/release/');
my $now = time();
while(my $file = readdir(DIR))
{
my $t = (stat "$file")[10];
next if ($file eq '.');
next if ($file eq '..');
if ($file =~ /^cc/) {
print $file.$/ unless $now - $t > 300;
}
}
closedir(DIR);
复制代码
欢迎光临 Chinaunix (http://bbs.chinaunix.net/)
Powered by Discuz! X3.2