#!/usr/bin/perl
use strict;
use warnings;
#locate directories
my $DIR = "/home/aimanhalim/LOG";
opendir(DIR, $DIR) or die $!;
#open Directory and read all the file.
while (my $DIR = readdir(DIR)) {print "$DIR\n";}
exit;
答案 0 :(得分:1)
假设您具有可以逐行读取的文件,因为目录名指示日志文件:
use strict;
use warnings;
use autodie;
my $DIR = '/home/aimanhalim/LOG';
chdir $DIR;
opendir my $dh, $DIR;
while (my $entry = readdir $dh) {
next if $entry =~ /^[.]/; # skip the '.' and '..' entries and hidden files
if (-f $entry) { # skip entries that are not files
open my $fh, '<', $entry;
while (my $line = $fh->getline) {
# do something with the content
}
}
}
如果您要递归读取目录,则可以切换到Path::Tiny。