我从这个简单的代码开始,它通过/ home /并确定对象是文件还是目录
#!/usr/bin/perl
# My Starting directory. I could also read this from the command line
$start = "/home/";
$count = 0; # How many non-file objects found. Line un-necessary
# Initialize the list
push (@dirs, $start);
# Iterate through the list (really a queue)
# We could also do this with a shift, but this works
foreach $curr (@dirs)
{
# Get the directory listing for the current directory
# Note that -F appends a character for the type of object it is
# on the end (/) for directory.
my @lines = split /\n/, `ls -F $curr`;
# Iterate through the things we got from the ls
foreach $line (@lines)
{
# The chomp is not necessary because the split strips out
# the seperator characters.
#chomp $line;
# If it ends in a / it's a directory. add it to the end of
# the list
if ($line =~ m#/$#)
{
print "Directory of -> " . $curr.$line . "\n";
push(@dirs, ($curr.$line));
}
else
{
print "File of ==> " . $curr.$line . "\n";
$count++;
}
}
}
# Silly print statement
print "I found " . $count . " non-directory objects.\n";
Run Code Online (Sandbox Code Playgroud)
我尝试修改它以使用ls -l,因为程序的其余部分取决于它的信息,但这就是事情变得奇怪的地方.
我修改了什么:
my @lines = `ls -l $curr`;
if ($line =~ m#-.........#)
elsif ($line =~ m#d.........#)
Run Code Online (Sandbox Code Playgroud)
我不是故意保持正则表达式,一旦我得到正确的进行,我将以正确的方式做到这一点.
在foreach之后,我添加了一个愚蠢的调试
print("$line \n");
Run Code Online (Sandbox Code Playgroud)
我应该得到的是这样的:
File of ==> /home/paul/perl/spider*
Directory of -> /home/paul/perl/test/
I found 9 non-directory objects.
Run Code Online (Sandbox Code Playgroud)
相反,我明白了:
total 8
drwxr-xr-x 28 paul paul 4096 2014-03-11 01:31 paul
Directory of -> /home/drwxr-xr-x 28 paul paul 4096 2014-03-11 01:31 paul
drwxr-xr-x 2 test test 4096 2014-03-10 02:12 test
Directory of -> /home/drwxr-xr-x 2 test test 4096 2014-03-10 02:12 test
ls: cannot access /home/drwxr-xr-x: No such file or directory
ls: cannot access 28: No such file or directory
ls: cannot access paul: No such file or directory
ls: cannot access paul: No such file or directory
ls: cannot access 4096: No such file or directory
ls: cannot access 2014-03-11: No such file or directory
ls: cannot access 01:31: No such file or directory
ls: cannot access paul: No such file or directory
ls: cannot access /home/drwxr-xr-x: No such file or directory
ls: cannot access 2: No such file or directory
ls: cannot access 4096: No such file or directory
ls: cannot access 2014-03-10: No such file or directory
ls: cannot access 02:12: No such file or directory
test:
total 0
test:
total 0
test:
total 0
I found 0 non-directory objects.
Run Code Online (Sandbox Code Playgroud)
我现在已经结束了,并且不明白为什么我尝试过的其他事情也都失败了.提前致谢!
如果需要目录递归,File :: Find可能比滚动自己的目录遍历逻辑更好,更容易.但由于问题主要是关于解析ls,我将更详细地介绍一下.
解析ls输出有很多复杂性.正确的方法是自己处理目录.
opendir(my $d, "/path/to/dir") or die "$!";
while ($item = readdir($d)) {
if (-d $item) {
print "dir $item\n";
} else {
print "notdir $item\n";
}
}
closedir ($d);
Run Code Online (Sandbox Code Playgroud)
还有许多其他的文件测试操作之外-d.如果您想要更详细地控制每个项目,也请查看stat.