#!/usr/bin/perl
# remove-empty-dirs - remove recursively empty directories
# 2002-2022 Vlado Keselj vlado@dnlp.ca http://vlado.ca

$| = 1;
&remove_empty_dirs(@ARGV);

sub remove_empty_dirs {
  my $count = 0;
  while ($#_ > -1) {
    my $dir = shift;
    # symbolic link
    if (-l $dir) { next }

    next if not -e $dir;

    # file
    if (not -d $dir) { next }
	   
    local ($_, *DIR);
    opendir(DIR, $dir) || die "can't opendir $dir: $!";
    map { /^\.\.?$/ ? '' : ($count += &remove_empty_dirs("$dir/$_")) }
      readdir(DIR);
    closedir(DIR);

    # is it empty?
    my $count1 = 0;
    opendir(DIR, $dir) || die "can't opendir $dir: $!";
    map { /^\.\.?$/ ? '' : ($count1 += 1) } readdir(DIR);
    closedir(DIR);
    if ($count1 == 0) {
      print "Removing $dir (empty dir)\n";
      rmdir $dir or die "Error removing $dir: $!";
      $count += 1;
    }
  }
  return $count;
}

exit 0;
__END__ # Documentation

=head1 NAME

remove-empty-dirs - remove recursively empty directories

=head1 SYNOPSIS

 $ remove-empty-dirs directories...

Removes all empty subdirectories recursively inside the given directories,
including directories themselves if they end up to be empty.

=head1 DESCRIPTION

The command C<remove-empty-dirs> recursively visits all subdirectories of
the directories given in the command line and removes all empty directories.
It will remove also directories that become empty after recursivelly removing
their empty subdirectories.

=head1 AUTHOR

Vlado Keselj vlado@dnlp.ca http://vlado.ca

=head1 LICENSE

Perl License (perl)

=head1 SCRIPT CATEGORIES

CPAN
Unix/System_administration

=head1 README

remove-empty-dirs - remove recursively empty directories

=cut
