# Perl scripting file handling

**URL:** <https://boards.straightdope.com/t/perl-scripting-file-handling/132508>\
**Category:** Factual Questions\
**Created:** [October 16, 2002, 2:02pm UTC](https://boards.straightdope.com/t/perl-scripting-file-handling/132508 "2002-10-16T14:02:46Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Carnal\_knowledge](https://avatars.discourse-cdn.com/v4/letter/c/c57346/32.png) [@Carnal\_knowledge](https://boards.straightdope.com/u/Carnal_knowledge)\
**Post date:** [October 16, 2002, 2:02pm UTC](https://boards.straightdope.com/t/perl-scripting-file-handling/132508/1 "2002-10-16T14:02:46Z")

</div>

Any Perl experts out there?

I’ve got a a bunch of (diff) files in a directory, and I want to remove the zero length files, which comprise about 95% of them, in a script.

Any ideas on what the easiest way to do this would be?

Thanks.

---

<div class="post-metadata">

**Author:** ![CurtC](https://avatars.discourse-cdn.com/v4/letter/c/ce73a5/32.png) [@CurtC](https://boards.straightdope.com/u/CurtC)\
**Post date:** [October 16, 2002, 2:36pm UTC](https://boards.straightdope.com/t/perl-scripting-file-handling/132508/2 "2002-10-16T14:36:57Z")

</div>

Use the glob() function to return the list of files, then go through each one, using the stat() function to find the file size. If it’s zero, delete it with the unlink() function.

---

<div class="post-metadata">

**Author:** ![friedo](https://avatars.discourse-cdn.com/v4/letter/f/8edcca/32.png) [@friedo](https://boards.straightdope.com/u/friedo)\
**Post date:** [October 16, 2002, 4:25pm UTC](https://boards.straightdope.com/t/perl-scripting-file-handling/132508/3 "2002-10-16T16:25:21Z")

</div>

If you’re dealing with recursively searching directories, then [File::Find](http://search.cpan.org/author/JHI/perl-5.8.0/lib/File/Find.pm) is a very useful module.

Otherwise, I would do something simple like this:

```auto

use strict;

my $dir = shift;

opendir DIR $dir or die "Could not open $dir: $!";
my @files = grep !/^\./, readdir DIR;
for(@files) { 
    unlink $_ if 0 == (stat($_))[7];
}

```

An excellent site for asking Perl questions is [Perl Monks](http://www.perlmonks.org/).

---

<div class="post-metadata">

**Author:** ![friedo](https://avatars.discourse-cdn.com/v4/letter/f/8edcca/32.png) [@friedo](https://boards.straightdope.com/u/friedo)\
**Post date:** [October 16, 2002, 4:30pm UTC](https://boards.straightdope.com/t/perl-scripting-file-handling/132508/4 "2002-10-16T16:30:31Z")

</div>

If you’re dealing with recursively searching directories, then [File::Find](http://search.cpan.org/author/JHI/perl-5.8.0/lib/File/Find.pm) is a very useful module.

Otherwise, I would do something simple like this:

```auto

use strict;

my $dir = shift;

opendir DIR $dir or die "Could not open $dir: $!";
my @files = grep !/^\./, readdir DIR;
for(@files) { 
    unlink $_ if 0 == (stat($_))[7];
}

```

An excellent site for asking Perl questions is [Perl Monks](http://www.perlmonks.org/).
