Perlのログファイルの検証を検証する

Perlのログファイルの検証を検証する

"FDP_RecordLength_Error_02202018_020107.log" "FDP_HeaderOrTrailerRecord_Error_02202018_020107.log" "FDP_DetailRecord_Error_02202018_020107.log"などのログすべてのログのファイルサイズを確認したいと思います。ログファイルサイズが0の場合、「ログファイルサイズは0です。」または、「ログファイルサイズがゼロではありません」と印刷されます。 Perlでこれを行うにはどうすればよいですか?誰でも私を助けることができますか?

ベストアンサー1

以下を使用できますfind

find . -type f -size 0 -exec echo "The logfile has a 0 size: {}" \;

find . -type f ! -size 0 -exec echo "The logfile does not have a 0 size: {}" \;

またはperl:

#!/usr/bin/perl --
use File::Find;

# directory to start looking for log files
my $dir = '/tmp/a';

# search base directory and call subroutine for each file found
find(\&size_check, $dir);

# subroutine to be called by find
sub size_check{
        # check filename matches regex and is a file (not directory)
        if($_ =~ /^.*\.log$/ and -f $_){
                # call stat and put data into an array
                my @stat = stat($_);

                # check to see if the size is zero
                if($stat[7] == 0){
                        print $_ . " has a size of 0\n";
                }else{
                        print $_ . " has a " . $stat[7] . " size\n";
                }
        }
}

おすすめ記事