Skip to content

Instantly share code, notes, and snippets.

@shang-lin
Created December 27, 2012 21:49
Show Gist options
  • Save shang-lin/4392352 to your computer and use it in GitHub Desktop.
Save shang-lin/4392352 to your computer and use it in GitHub Desktop.
Remove empty files from a directory given as a command line argument.
# Deletes empty (size 0) files from the directory provided as a command-line
# argument. Currently does not descend into subdirectories.
# Created August 2005.
# author: Shang-Lin Chen
use File::stat;
use File::Copy "move";
$num_args = @ARGV;
if ($num_args < 1) {
print "usage: del_empty_files <directory>\n";
exit;
}
$num_del = 0;
$dir = shift;
chdir($dir);
opendir(my $dh, $dir) || die "Cannot open $dir.\n";
# Each file listing becomes a separate entry in this array.
while (my $filename = readdir($dh)) {
#print "$filename\n";
if (! -d $filename) {
my $sb = stat($filename);
if ($sb->size == 0) {
print "$filename is empty.\n";
print "deleting $filename\n";
system("rm $filename");
$num_del++;
}
}
}
print "deleted $num_del files\n";
closedir($dh);
exit 1;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment