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 ?

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";
                }
        }
}
  • I want to add an additional check to my Perl script, that's why i need that – Deep Mukherjee Feb 20 '18 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 – Deep Mukherjee Feb 20 '18 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? – Deep Mukherjee Feb 20 '18 at 10:21
  • what this ~ /^.*\.log$ is actually doing? – Deep Mukherjee Feb 20 '18 at 12:07
  • It is a regular expression which will match anything that ends in .log – rusty shackleford Feb 20 '18 at 13:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.