Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

im trying to make a script on reading all of the files inside a directory but it seems i cant.... the only thing i can is to list the names of the file inside the directory.So is there a way for me to list it ? (Kinda new to perl and linux :U)

#!/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
";}


exit;
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
154 views
Welcome To Ask or Share your Answers For Others

1 Answer

Assuming you have files that can be read line-by-line, as the directory name indicates log files:

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

If you want to read directories recursively, perhaps switch over to Path::Tiny.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...