Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any way to check whether a file is an empty file using Perl program?

Tags:

perl

I am looking a small script which will tell me whether a file is an empty file or not, but I am unable to display this.

I have used the below code:

opendir DIR,$directory ;
while (my $dir =readdir DIR) {
    if (-s "$dir") {
    print "This is an empty file";
    }
}

Here I am unable to print "This is an empty file" and my code does not went inside the if loop. Can anyone tell me what is the wrong in the above code?

like image 758
varun kumar Avatar asked Sep 05 '25 16:09

varun kumar


1 Answers

The relevant file test operators are:

  • -z: check if the file is empty.
  • -s: check if the file has nonzero size (returns size in bytes).

You are checking if $dir is non-empty, so opposite of what you are trying to achieve. Use -z (or !-s) instead.

Also, each $dir is just the filename without the path, so you need to include it yourself if you aren't processing the current directory.

if (-z "$directory/$dir") {
    print "This is an empty file";
}
like image 172
vlumi Avatar answered Sep 08 '25 16:09

vlumi