🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: news.software.nntp
8 messages
8 total messages Started by Jeff Garzik Tue, 25 Feb 1997 00:00
A script to find and remove empty directories
#15318
Author: Jeff Garzik
Date: Tue, 25 Feb 1997 00:00
94 lines
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--


Re: A script to find and remove empty directories
#15350
Author: Jef Poskanzer
Date: Wed, 26 Feb 1997 00:00
17 lines
1353 bytes
miquels@cistron.nl (Miquel van Smoorenburg):
}find /var/spool/news -type d -empty -depth | xargs -r rmdir

Some versions of find need an explicit -print.  Some versions of find
don't have -empty and/or -depth; the -empty is not necessary, and the
-depth can be replaced with a sort -r step.  I don't even know what the
-r on xargs is for.  This command should work everywhere:

find /var/spool/news -type d -print | sort -r | xargs rmdir
---
Jef

         Jef Poskanzer  jef@acme.com  http://www.acme.com/jef/
          Ph'nglui mglw'nafh Barney R'lyeh wgah'nagl fhtagn.

Re: A script to find and remove empty directories
#15359
Author: Miquel van Smoorenburg
Date: Wed, 26 Feb 1997 00:00
34 lines
1858 bytes
In article <5f27vi$58m$1@shell3.ba.best.com>,
Jef Poskanzer  <jef@acme.com> wrote:
>miquels@cistron.nl (Miquel van Smoorenburg):
>}find /var/spool/news -type d -empty -depth | xargs -r rmdir
>
>Some versions of find need an explicit -print.  Some versions of find
>don't have -empty and/or -depth; the -empty is not necessary, and the
>-depth can be replaced with a sort -r step.  I don't even know what the
>-r on xargs is for.  This command should work everywhere:
>
>find /var/spool/news -type d -print | sort -r | xargs rmdir

You're absolutely right ofcourse. I've gotten used to Linux and other
systems with GNU utils too much, I think :).

By the way, the "-r" to xargs is useful:

       --no-run-if-empty, -r
              If  the  standard  input  does not contain any non�
              blanks, do not run the command.  Normally, the com�
              mand is run once even if there is no input.

It prevents

rmdir: too few arguments
Try `rmdir --help' for more information.

But I digress.. as usual with any Usenet posting ofcourse. I'm just holding
up tradition :)

Mike.

Re: A script to find and remove empty directories
#15361
Author: Jeff Garzik
Date: Wed, 26 Feb 1997 00:00
29 lines
1775 bytes
Michael Hucka wrote:
> >>>>> On 25 Feb 1997, Jeff Garzik <jeff.garzik@spinne.com> wrote:
>   JG> Here is a quick and dirty Perl script that removes empty directories
>   JG> recursively.  There may be some way to do this with shell utils, but it
>   JG> was faster for me to simply write this.  :) It should be easy to modify
>   JG> the output to show newsgroups removed, for logging purposes or
>   JG> whatever.

> I don't mean this in any rude way, but it turns out there are probably a
> half-dozen other programs available to do this task.


Actually the programs in the areas you cite clean up, in one shape or
another, *files*.  Most are meant to be piped to fastrm.  There was one
script, dropgroup, on Stephen Jones' script page that came close to what
I was looking for, but even then there was not an rmdir to be found.

	Jeff



-- 
"The New York Times is read by the people who run the country.  The
Washington Post is read by the people who think they run the country.
The National Enquirer is read by the people who think Elvis is alive
and running the country ..."  -- Robert J Woodhead

Re: A script to find and remove empty directories
#15376
Author: Michael Hucka
Date: Wed, 26 Feb 1997 00:00
28 lines
1682 bytes
>>>>> On 25 Feb 1997, Jeff Garzik <jeff.garzik@spinne.com> wrote:

  JG> Here is a quick and dirty Perl script that removes empty directories
  JG> recursively.  There may be some way to do this with shell utils, but it
  JG> was faster for me to simply write this.  :) It should be easy to modify
  JG> the output to show newsgroups removed, for logging purposes or
  JG> whatever.

I don't mean this in any rude way, but it turns out there are probably a
half-dozen other programs available to do this task.

See the following web pages:

    http://www.math.psu.edu/barr/INN.html
    ftp://ftp.isc.org/isc/inn/unoff-contrib/

One of the most efficient programs for this task is Matt Dillon's newsprune,
available from the following site:

    http://www.backplane.com/news/

-- 
Mike Hucka    hucka@umich.edu    http://www.eecs.umich.edu/~hucka    University
 PhD to be, computational models of human visual processing (AI Lab)     of
   UNIX systems administrator & programmer/analyst (EECS DCO)         Michigan

Re: A script to find and remove empty directories
#15382
Author: Jeff Garzik
Date: Wed, 26 Feb 1997 00:00
34 lines
1973 bytes
(courtesy copy sent via e-mail)

Miquel van Smoorenburg wrote:
> >> >>>>> On 25 Feb 1997, Jeff Garzik <jeff.garzik@spinne.com> wrote:
> >>   JG> Here is a quick and dirty Perl script that removes empty directories
> >>   JG> recursively.  There may be some way to do this with shell utils, but it
> >>   JG> was faster for me to simply write this.  :) It should be easy to modify
> >>   JG> the output to show newsgroups removed, for logging purposes or
> >>   JG> whatever.

> find /var/spool/news -type d -empty -depth | xargs -r rmdir


I just checked this with strace under Linux.  This command stats *every*
file in /var/spool/news to determine whether it is a directory or not. 
(-type d)

My script reads the directory, then skips all files with all-digit
filenames, so only directories are stat'd.  (special cases like alt.2600
fall through the cracks)  There is a big performance difference when you
are talking about hundreds of thousands of files.

	Jeff



-- 
"The New York Times is read by the people who run the country.  The
Washington Post is read by the people who think they run the country.
The National Enquirer is read by the people who think Elvis is alive
and running the country ..."  -- Robert J Woodhead

Re: A script to find and remove empty directories
#15384
Author: Miquel van Smoorenburg
Date: Wed, 26 Feb 1997 00:00
25 lines
1711 bytes
In article <3313F711.79B8AA0A@spinne.com>,
Jeff Garzik  <jeff.garzik@spinne.com> wrote:
>Michael Hucka wrote:
>> >>>>> On 25 Feb 1997, Jeff Garzik <jeff.garzik@spinne.com> wrote:
>>   JG> Here is a quick and dirty Perl script that removes empty directories
>>   JG> recursively.  There may be some way to do this with shell utils, but it
>>   JG> was faster for me to simply write this.  :) It should be easy to modify
>>   JG> the output to show newsgroups removed, for logging purposes or
>>   JG> whatever.
>
>> I don't mean this in any rude way, but it turns out there are probably a
>> half-dozen other programs available to do this task.
>
>
>Actually the programs in the areas you cite clean up, in one shape or
>another, *files*.  Most are meant to be piped to fastrm.  There was one
>script, dropgroup, on Stephen Jones' script page that came close to what
>I was looking for, but even then there was not an rmdir to be found.

find /var/spool/news -type d -empty -depth | xargs -r rmdir

Mike.

Re: A script to find and remove empty directories
#15393
Author: Casper H.S. Dik - Network Securi...
Date: Thu, 27 Feb 1997 00:00
28 lines
1521 bytes
Jeff Garzik <jeff.garzik@spinne.com> writes:

>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.



Here's my Q&D shell version:

	cd /var/spool/news
	find . -depth -type d -print | xargs rmdir

(rmdir will only remove non-empty directories, -depth causes find to
print subdirectories befor ethere parents.

So you get a lot of error messages, who cares?

Casper
--
Expressed in this posting are my opinions.  They are in no way related
to opinions held by my employer, Sun Microsystems.
Statements on Sun products included here are not gospel and may
be fiction rather than truth.

Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads