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

发布于 2024-08-11 05:14:08 字数 1089 浏览 3 评论 0原文

我有一个目录 /var/spool ,其中有一个名为 /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

,在每个“字母目录”内,有一个名为“user”的目录,其中有许多名为 < code>auser1 auser2 auser3 auser4 auser5 ...

每个用户目录都包含邮件消息和文件名具有以下格式:2.3.4.5.等。

如何通过以下方式列出每个目录中每个用户的电子邮件文件:

    /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.

等。

我需要这些文件,然后打开每个文件进行修改标头和正文。这部分我已经有了,但我需要第一部分。

我正在尝试这个:

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);

但没有按照我需要的方式工作。

I have a directory /var/spool and inside that, directories named

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 ...

Every user directory contains mail messages and the file names have the following format: 2. 3. 4. 5. etc.

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

感情旳空白 2024-08-18 05:14:08

您可以使用File::Find

You can use File::Find.

倒数 2024-08-18 05:14:08

正如其他人所指出的,使用 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";
}

As others have noted, use 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";
}
夜访吸血鬼 2024-08-18 05:14:08

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

Use File::Find to traverse a directory tree.

不乱于心 2024-08-18 05:14:08

对于固定级别的目录,有时使用 glob 比 File::Find 更容易:

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

For a fixed level of directories, sometimes it's easier to use glob than File::Find:

while (my $file = </var/spool/[a-z]/user/*/*>) {
  print "Processing $file\n";
}
当梦初醒 2024-08-18 05:14:08

人们一直推荐 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->();

您甚至不需要使用File::Find::Closures。我编写了该模块,以便您可以取出所需的子例程并将其粘贴到您自己的代码中,也许可以对其进行调整以获得您需要的内容。

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:

 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. 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.

青春如此纠结 2024-08-18 05:14:08

试试这个:

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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文