如何使用 Perl 列出具有特定名称模式的目录下的文件?
我有一个目录 /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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您可以使用File::Find。
You can use File::Find.
正如其他人所指出的,使用 File::Find:
As others have noted, use File::Find:
使用 File::Find 遍历目录树。
Use File::Find to traverse a directory tree.
对于固定级别的目录,有时使用 glob 比 File::Find 更容易:
For a fixed level of directories, sometimes it's easier to use glob than File::Find:
人们一直推荐 File::Find,但另一个让它变得简单的部分是我的 File: :Find::Closures,它为您提供了方便的功能:
您甚至不需要使用
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:
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.试试这个:
Try this: