如何使用Perl列出目录中的所有文件? [重复]

时间:2023-02-07 15:06:06

This question already has an answer here:

这个问题在这里已有答案:

Is there a function in Perl that lists all the files and directories in a directory? I remember that Java has the File.list() to do this? Is there a comparable method in Perl?

Perl中是否有一个列出目录中所有文件和目录的函数?我记得Java有File.list()来做这个吗? Perl中有类似的方法吗?

6 个解决方案

#1


71  

If you want to get content of given directory, and only it (i.e. no subdirectories), the best way is to use opendir/readdir/closedir:

如果你想获得给定目录的内容,只有它(即没有子目录),最好的方法是使用opendir / readdir / closedir:

opendir my $dir, "/some/path" or die "Cannot open directory: $!";
my @files = readdir $dir;
closedir $dir;

You can also use:

您还可以使用:

my @files = glob( $dir . '/*' );

But in my opinion it is not as good - mostly because glob is quite complex thing (can filter results automatically) and using it to get all elements of directory seems as a too simple task.

但在我看来它并不是那么好 - 主要是因为glob是非常复杂的东西(可以自动过滤结果)并使用它来获取目录的所有元素似乎是一个太简单的任务。

On the other hand, if you need to get content from all of the directories and subdirectories, there is basically one standard solution:

另一方面,如果您需要从所有目录和子目录获取内容,基本上有一个标准解决方案:

use File::Find;

my @content;
find( \&wanted, '/some/path');
do_something_with( @content );

exit;

sub wanted {
  push @content, $File::Find::name;
  return;
}

#2


12  

readdir() does that.

readdir()就是这么做的。

Check http://perldoc.perl.org/functions/readdir.html

查看http://perldoc.perl.org/functions/readdir.html

opendir(DIR, $some_dir) || die "can't opendir $some_dir: $!";
@dots = grep { /^\./ && -f "$some_dir/$_" } readdir(DIR);
closedir DIR;

#3


11  

Or File::Find

或文件::查找

use File::Find;
finddepth(\&wanted, '/some/path/to/dir');
sub wanted { print };

It'll go through subdirectories if they exist.

如果子目录存在,它将通过子目录。

#4


10  

this should do it.

这应该做到这一点。

my $dir = "bla/bla/upload";
opendir DIR,$dir;
my @dir = readdir(DIR);
close DIR;
foreach(@dir){
    if (-f $dir . "/" . $_ ){
        print $_,"   : file\n";
    }elsif(-d $dir . "/" . $_){
        print $_,"   : folder\n";
    }else{
        print $_,"   : other\n";
    }
}

#5


3  

If you are a slacker like me you might like to use the File::Slurp module. The read_dir function will reads directory contents into an array, removes the dots, and if needed prefix the files returned with the dir for absolute paths

如果你像我一样懒,你可能想使用File :: Slurp模块。 read_dir函数将目录内容读入数组,删除点,如果需要,用绝对路径的dir返回的文件前缀

my @paths = read_dir( '/path/to/dir', prefix => 1 ) ;

#6


2  

This will list Everything (including sub directories) from the directory you specify, in order, and with the attributes. I have spent days looking for something to do this, and I took parts from this entire discussion, and a little of my own, and put it together. ENJOY!!

这将按顺序列出您指定的目录中的Everything(包括子目录),并使用属性。我花了几天时间寻找能够做到这一点的事情,并且我从整个讨论中获取了部分内容,并将其作为我自己的一部分,并将其整合在一起。请享用!!

#!/usr/bin/perl --
print qq~Content-type: text/html\n\n~;
print qq~<font face="arial" size="2">~;

use File::Find;

# find( \&wanted_tom, '/home/thomas/public_html'); # if you want just one website, uncomment this, and comment out the next line
find( \&wanted_tom, '/home');
exit;

sub wanted_tom {
($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,$blksize,$blocks) = stat ($_);
$mode = (stat($_))[2];
$mode = substr(sprintf("%03lo", $mode), -3);

if (-d $File::Find::name) {
print "<br><b>--DIR $File::Find::name --ATTR:$mode</b><br>";
 } else {
print "$File::Find::name --ATTR:$mode<br>";
 }
  return;
}

#1


71  

If you want to get content of given directory, and only it (i.e. no subdirectories), the best way is to use opendir/readdir/closedir:

如果你想获得给定目录的内容,只有它(即没有子目录),最好的方法是使用opendir / readdir / closedir:

opendir my $dir, "/some/path" or die "Cannot open directory: $!";
my @files = readdir $dir;
closedir $dir;

You can also use:

您还可以使用:

my @files = glob( $dir . '/*' );

But in my opinion it is not as good - mostly because glob is quite complex thing (can filter results automatically) and using it to get all elements of directory seems as a too simple task.

但在我看来它并不是那么好 - 主要是因为glob是非常复杂的东西(可以自动过滤结果)并使用它来获取目录的所有元素似乎是一个太简单的任务。

On the other hand, if you need to get content from all of the directories and subdirectories, there is basically one standard solution:

另一方面,如果您需要从所有目录和子目录获取内容,基本上有一个标准解决方案:

use File::Find;

my @content;
find( \&wanted, '/some/path');
do_something_with( @content );

exit;

sub wanted {
  push @content, $File::Find::name;
  return;
}

#2


12  

readdir() does that.

readdir()就是这么做的。

Check http://perldoc.perl.org/functions/readdir.html

查看http://perldoc.perl.org/functions/readdir.html

opendir(DIR, $some_dir) || die "can't opendir $some_dir: $!";
@dots = grep { /^\./ && -f "$some_dir/$_" } readdir(DIR);
closedir DIR;

#3


11  

Or File::Find

或文件::查找

use File::Find;
finddepth(\&wanted, '/some/path/to/dir');
sub wanted { print };

It'll go through subdirectories if they exist.

如果子目录存在,它将通过子目录。

#4


10  

this should do it.

这应该做到这一点。

my $dir = "bla/bla/upload";
opendir DIR,$dir;
my @dir = readdir(DIR);
close DIR;
foreach(@dir){
    if (-f $dir . "/" . $_ ){
        print $_,"   : file\n";
    }elsif(-d $dir . "/" . $_){
        print $_,"   : folder\n";
    }else{
        print $_,"   : other\n";
    }
}

#5


3  

If you are a slacker like me you might like to use the File::Slurp module. The read_dir function will reads directory contents into an array, removes the dots, and if needed prefix the files returned with the dir for absolute paths

如果你像我一样懒,你可能想使用File :: Slurp模块。 read_dir函数将目录内容读入数组,删除点,如果需要,用绝对路径的dir返回的文件前缀

my @paths = read_dir( '/path/to/dir', prefix => 1 ) ;

#6


2  

This will list Everything (including sub directories) from the directory you specify, in order, and with the attributes. I have spent days looking for something to do this, and I took parts from this entire discussion, and a little of my own, and put it together. ENJOY!!

这将按顺序列出您指定的目录中的Everything(包括子目录),并使用属性。我花了几天时间寻找能够做到这一点的事情,并且我从整个讨论中获取了部分内容,并将其作为我自己的一部分,并将其整合在一起。请享用!!

#!/usr/bin/perl --
print qq~Content-type: text/html\n\n~;
print qq~<font face="arial" size="2">~;

use File::Find;

# find( \&wanted_tom, '/home/thomas/public_html'); # if you want just one website, uncomment this, and comment out the next line
find( \&wanted_tom, '/home');
exit;

sub wanted_tom {
($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,$blksize,$blocks) = stat ($_);
$mode = (stat($_))[2];
$mode = substr(sprintf("%03lo", $mode), -3);

if (-d $File::Find::name) {
print "<br><b>--DIR $File::Find::name --ATTR:$mode</b><br>";
 } else {
print "$File::Find::name --ATTR:$mode<br>";
 }
  return;
}