简体   繁体   English

如何使用Perl在具有特定名称模式的目录下列出文件?

[英]How can I list files under a directory with a specific name pattern using Perl?

I have a directory /var/spool and inside that, directories named 我有一个目录/var/spool ,在其中,目录名为

a  b  c  d  e  f  g  h i  j  k  l  m  n  o  p q  r  s  t  u  v  x  y z

And inside each "letter directory", a directory called " user " and inside this, many directories called auser1 auser2 auser3 auser4 auser5 ... 在每个“字母目录”中,都有一个名为“ user ”的目录,在此目录中,还有许多目录,分别名为auser1 auser2 auser3 auser4 auser5 ...

Every user directory contains mail messages and the file names have the following format: 2. 3. 4. 5. etc. 每个用户目录都包含邮件,文件名具有以下格式:2. 3. 4. 5.等等。

How can I list the email files for every user in every directory in the following way: 如何通过以下方式列出每个目录中每个用户的电子邮件文件:

/var/spool/a/user/auser1/11.
    /var/spool/a/user/auser1/9.
    /var/spool/a/user/auser1/8.
    /var/spool/a/user/auser1/10.
    /var/spool/a/user/auser1/2.
    /var/spool/a/user/auser1/4.
    /var/spool/a/user/auser1/12.
    /var/spool/b/user/buser1/12.
    /var/spool/b/user/buser1/134.
    /var/spool/b/user/buser1/144.

etc. 等等

I need that files and then open every single file for modify the header and body. 我需要这些文件,然后打开每个文件来修改标题和正文。 This part I already have, but I need the first part. 我已经有了这部分,但是我需要第一部分。

I am trying this: 我正在尝试:

dir = "/var/spool";

opendir ( DIR, $dir ) || die "No pude abrir el directorio $dirname\n";
while( ($filename = readdir(DIR))){
    @directorios1 = `ls -l "$dir/$filename"`;
    print("@directorios1\n");
}
closedir(DIR);

But does not work the way I need it. 但是不能按我需要的方式工作。

您可以使用File :: Find

使用File :: Find遍历目录树。

As others have noted, use File::Find : 如其他人所述,使用File :: Find

#!/usr/bin/perl

use strict;
use warnings;

use File::Find;

find(\&find_emails => '/var/spool');

sub find_emails {
    return unless /\A[0-9]+[.]\z/;
    return unless -f $File::Find::name;

    process_an_email($File::Find::name);
    return;
}

sub process_an_email {
    my ($file) = @_;
    print "Processing '$file'\n";
}

People keep recommending File::Find, but the other piece that makes it easy is my File::Find::Closures , which provides the convenience functions for you: 人们一直在推荐File :: Find,但是让它变得容易的另一部分是我的File :: Find :: Closures ,它为您提供了便捷的功能:

 use File::Find;
 use File::Find::Closures qw( find_by_regex );

 my( $wanted, $reporter ) = find_by_regex( qr/^\d+\.\z/ );

 find( $wanted, @directories_to_search );

 my @files = $reporter->();

You don't even need to use File::Find::Closures . 您甚至不需要使用File::Find::Closures I wrote the module so that you could lift out the subroutine you wanted and paste it into your own code, perhaps tweaking it to get what you needed. 我编写了该模块,以便您可以提取所需的子例程并将其粘贴到自己的代码中,也许可以对其进行调整以获得所需的内容。

For a fixed level of directories, sometimes it's easier to use glob than File::Find: 对于固定级别的目录,有时使用glob比File :: Find更容易:

while (my $file = </var/spool/[a-z]/user/*/*>) {
  print "Processing $file\n";
}

Try this: 尝试这个:

sub browse($);

sub browse($)
{    
    my $path = $_[0];

    #append a / if missing
    if($path !~ /\/$/)
    {
        $path .= '/';
    }

    #loop through the files contained in the directory
    for my $eachFile (glob($path.'*')) 
    {

        #if the file is a directory
        if(-d $eachFile) 
        {
            #browse directory recursively
            browse($eachFile);
        } 
        else 
        {
           # your file processing here
        }
    }   
}#browse

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用Perl列出目录中的所有文件? - How can I list all files in a directory using Perl? 如何在Perl中的目录下匹配多个文件的正则表达式模式? - How to match a regex pattern for multiple files under a directory in perl? 如何在目录名中使用通配符在Perl中打开文件? - How can I open a file in Perl using a wildcard in the directory name? 使用Perl,如何查找和删除具有特定后缀的文件? - Using Perl, how can I find and remove files with a specific suffix? 使用Perl,如何遍历具有特定文件名模板模式的文件? - Using Perl, how do I loop through files with a specific filename template pattern? 如何制作每隔几分钟重命名特定目录中文件的perl脚本? - How can I make a perl script that will rename files in a specific directory every couple minutes? 如何使用find和Perl列出文件? - How can I list files with find and Perl? 使用Perl,将目录中的文件列表推入数组,但基于文件名的一些选择文件除外 - Using Perl, push into an array a list of files in a directory except for a few select files based on file name 如何使用根目录中存在的所有目录下特定文件夹中的Shell脚本列出所有文件? - How to list all the files using shell script present in a specific folder under all the directories those are present in the root directory? 如何在Perl中获得目录中的所有文件,而不是子目录中的所有文件? - How can I get all files in a directory, but not in subdirectories, in Perl?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM