0

I have "FDP_RecordLength_Error_02202018_020107.log" "FDP_HeaderOrTrailerRecord_Error_02202018_020107.log" "FDP_DetailRecord_Error_02202018_020107.log" and other log files as well. I want to check the file size for all the logs. If the log file size is zero then it will print "The log file is having zero size" or else "Log file is not having zero size". How can i do that in Perl? Can anyone help me out ?

1

1 Answer 1

4

You could just use 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: {}" \;

Or 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";
                }
        }
}
7
  • I want to add an additional check to my Perl script, that's why i need that Feb 20, 2018 at 9:32
  • In addition to that i want to check only the log files. If it is having zero bytes then it will print "It is a zero byte file" in perl Feb 20, 2018 at 9:37
  • could you please help me with the multiple no of log files? I mean if there are multiple no of log files in a path how to execute that? Feb 20, 2018 at 10:21
  • what this ~ /^.*\.log$ is actually doing? Feb 20, 2018 at 12:07
  • It is a regular expression which will match anything that ends in .log Feb 20, 2018 at 13:41

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .