Perl scripting file handling

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.

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.

If you’re dealing with recursively searching directories, then File::Find is a very useful module.

Otherwise, I would do something simple like this:



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.

If you’re dealing with recursively searching directories, then File::Find is a very useful module.

Otherwise, I would do something simple like this:



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.