在Perl中以目录模式查找数以千计的文件 [英] finding many thousands of files in a directory pattern in Perl

查看:233
本文介绍了在Perl中以目录模式查找数以千计的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Perl中找到一个目录模式的文件模式,它会返回许多条目,如下所示:

  find〜/ mydir / * / ?? / ??? / ???? -name\ *。$ refinfilebase.search-print 

不同的方式来处理它?即:

  File :: Find 
glob()
opendir,readdir,grep
钻石操作员,例如:my @files =< $ refinfilebase.search>

哪一个最适合在老版本的Perl或最低版本上运行脚本的Perl?

解决方案

您还有DirHandle



DirHandle:

 使用DirHandle; 
$ d = new DirHandle。;
if(defined $ d){
while(defined($ _ = $ d-> read)){something($ _); }
$ d->倒带;
while(defined($ _ = $ d-> read)){something_else($ _); }
undef $ d;

对于readdir和glob的用例,请参阅
有什么理由有更喜欢glob over readdir(反之亦然)在Perl?



我更喜欢使用 glob 来快速抓取(无子目录)中的文件列表,并处理它们,如
$ b $ map {process_bam($ _)} glob(bam_files / *。bam)



这样比较方便,因为它不需要。和...甚至是你要求(*),也返回完整的路径,如果你使用一个目录在glob模式。



你也可以快速使用glob作为当你需要预处理列表的文件名时,oneliner通过管道传递给xargs或bash for循环:

  perl -lE'print join(\\\
,map {s / srf\ /(.+)。srf / $ 1 /; $ _} glob(srf / 198 * .srf))'| xargs -n 1 .....

Readdir在其他场景中具有冒险性,所以您需要使用一个更适合你的行动。


I would like to find a file pattern on a directory pattern in Perl that will return many thousands of entries, like this:

find ~/mydir/*/??/???/???? -name "\*.$refinfilebase.search" -print

I've been told there are different ways to handle it? I.e.:

File::Find
glob()
opendir, readdir, grep
Diamond operator, e.g.: my @files = <$refinfilebase.search>

Which one would be most adequate to be able to run the script on older versions of Perl or minimal installations of Perl?

解决方案

Also you have DirHandle

DirHandle:

use DirHandle;
$d = new DirHandle ".";
if (defined $d) {
    while (defined($_ = $d->read)) { something($_); }
    $d->rewind;
    while (defined($_ = $d->read)) { something_else($_); }
    undef $d;
}

For use cases of readdir and glob see What reasons are there to prefer glob over readdir (or vice-versa) in Perl?

I prefer to use glob for quickly grab a list of files in a dir (no subdirs) and process them like

map{process_bam($_)} glob(bam_files/*.bam)

This is more convenient because it does not take the . and .. even is you ask for (*) and also returns the full path if you use a dir in the glob pattern.

Also you can use glob quickly as a oneliner piped to xargs or in a bash for loop when you need to preprocess the filenames of the list:

perl -lE 'print join("\n", map {s/srf\/(.+).srf/$1/;$_} glob("srf/198*.srf"))' | xargs -n 1.....

Readdir has adventages in other scenarios so you need to use the one that fits better for your actions.

这篇关于在Perl中以目录模式查找数以千计的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆