# How to check for empty file in Perl?

**URL:** <https://community.unix.com/t/how-to-check-for-empty-file-in-perl/187741>\
**Category:** UNIX for Dummies Questions & Answers\
**Created:** [March 4, 2008, 11:44am UTC](https://community.unix.com/t/how-to-check-for-empty-file-in-perl/187741 "2008-03-04T11:44:44Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![deepakwins](https://community.unix.com/letter_avatar/deepakwins/32/5_5575768a8748004e209b776fc1b2916d.png) [@deepakwins](https://community.unix.com/u/deepakwins)\
**Post date:** [March 4, 2008, 11:44am UTC](https://community.unix.com/t/how-to-check-for-empty-file-in-perl/187741/1 "2008-03-04T11:44:44Z")

</div>

Hi,

May I know how to check for empty file in Perl.  
Iam missing something, somewhere.

#!/usr/bin/perl

my $open\_dir = '/path/';  
my $file;  
my $ma = "abc\_.\*.\.psv\$"

opendir(VAR, $open\_dir) or die "Can't open $oepn\_dir: $!\n";

```
    while\( defined \($file = readdir VAR\) \) \#read all the files in the directory
    \{
            if \(\($file =~ /$ma/i\) && \( -s $file \)\) \{
                 print $file;
           \}
 \}

```

It doest work.  
I doesnt give out any non empty file. pls correct me.

---

<div class="post-metadata">

**Author:** ![deepakwins](https://community.unix.com/letter_avatar/deepakwins/32/5_5575768a8748004e209b776fc1b2916d.png) [@deepakwins](https://community.unix.com/u/deepakwins)\
**Post date:** [March 4, 2008, 2:00pm UTC](https://community.unix.com/t/how-to-check-for-empty-file-in-perl/187741/2 "2008-03-04T14:00:37Z")

</div>

No worries, Figured it out.  
I should have given absolute path for the file.  
Thanks anyways.
