Article View: news.software.nntp
Article #15318A script to find and remove empty directories
From: Jeff Garzik
Date: Tue, 25 Feb 1997 00:00
Date: Tue, 25 Feb 1997 00:00
94 lines
3078 bytes
3078 bytes
This is a multi-part message in MIME format.
--------------4DA3AEB33E27E30747BB2B0B
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
Here is a quick and dirty Perl script that removes empty directories
recursively. There may be some way to do this with shell utils, but it
was faster for me to simply write this. :) It should be easy to modify
the output to show newsgroups removed, for logging purposes or whatever.
The output on my spool was
find-empty-dirs: 9693 dirs removed, 4100 seconds
I have about 25,000 groups in my active file. Hopefully this will make
some difference in news.daily and find operations.
Jeff
--------------4DA3AEB33E27E30747BB2B0B
Content-Type: text/plain; charset=us-ascii; name="find-empty-dirs"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline; filename="find-empty-dirs"
#!/usr/local/bin/perl
#
# find-empty-dirs -- find empty news spool directories and remove em
#
# Jeff Garzik <jgarzik@pobox.com>
#
$SpoolDir = '/news/spool/articles';
$start_time = time;
$removed = 0;
&remove_empty($SpoolDir);
$elapsed = time - $start_time;
print "find-empty-dirs: $removed dirs removed, $elapsed seconds\n";
exit(0);
sub numerically { return $a <=> $b; }
sub remove_empty {
my ($dir) = @_;
my (@files, $fn, @pos, $i);
# DEBUG statement
# print "DIR: $dir\n";
if (!opendir(DIR, $dir)) {
print STDERR "Cannot open $dir: $!\n";
return 0;
}
@files = readdir(DIR);
close(DIR);
return 1 if (($#files == 1) &&
(($files[0] eq ".") || ($files[0] eq "..")) &&
(($files[1] eq ".") || ($files[1] eq "..")));
foreach $i (0 .. $#files) {
next if ($files[$i] =~ /^\d+$/);
next if (($files[$i] eq ".") || ($files[$i] eq ".."));
if ((-d "$dir/" . $files[$i]) && (&remove_empty("$dir/" . $files[$i]))) {
if (rmdir("$dir/" . $files[$i])) {
$removed++;
print "REMOVED: $dir/", $files[$i], "\n";
push(@pos, $i);
} else {
print STDERR "Cannot remove $dir: $!\n";
}
}
}
foreach $i (reverse sort numerically @pos) {
splice(@files, $i, 1);
}
return 1 if (($#files == 1) &&
(($files[0] eq ".") || ($files[0] eq "..")) &&
(($files[1] eq ".") || ($files[1] eq "..")));
return 0;
}
--------------4DA3AEB33E27E30747BB2B0B--
Technical Details
Message-ID:
<33132F95.54538512@spinne.com>Path:
mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!illumos!news.pugleaf.net!.TX!archive.pugleaf.net!archive.newsdeef.eu!mbox2nntp-news.software.nntp.mbox.zip!not-for-mailShow Full Headers
Complete Article Headers
Message-ID: <33132F95.54538512@spinne.com> Subject: A script to find and remove empty directories Date: Tue, 25 Feb 1997 00:00:00 +0000 From: Jeff Garzik <jeff.garzik@spinne.com> Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!illumos!news.pugleaf.net!.TX!archive.pugleaf.net!archive.newsdeef.eu!mbox2nntp-news.software.nntp.mbox.zip!not-for-mail X-Deja-AN: 221370525 Content-Type: multipart/mixed; boundary="------------4DA3AEB33E27E30747BB2B0B" Organization: Spinne http://spinne.com Mime-Version: 1.0 Newsgroups: news.software.nntp X-Mailer: Mozilla 3.01Gold (X11; I; Linux 2.0.28 i586)