Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
use 5.14.2; use warnings; our $VERSION = "1.01 - 20180217"; my $cmd = $0 =~ s{.*/}{}r; sub usage { my $err = shift and select STDERR; print "usage: $cmd [ --rmdir ] [ dir ... ]\n"; exit $err; } # usage use Getopt::Long qw(:config bundling nopermute); GetOptions ( "help|?" => sub { usage (0) }, "V|version" => sub { say "$cmd [$VERSION]"; exit 0; }, "d|delete|rm|rmdir" => \my $opt_d, ) or usage (1); use File::Find; no warnings "File::Find"; my @dir = @ARGV ? @ARGV : ("."); grep { ! -d $_ } @dir and usage (1); finddepth (sub { ! -d $_ || m/^\.\.?$/ and return; my ($dh, @d); unless (opendir $dh, $_ and @d = readdir $dh) { warn "Cannot read $File::Find::name\n"; return; } closedir $dh; if (@d == 2) { print "$File::Find::name\n"; $opt_d and rmdir $_; } }, @dir);

Used and tested on HP-UX, AIX, and Linux. Don't know if it works on Windows.

The find/exec has the problem that it needs to be run many times to cope with deep empty trees, and I had trees of 20+ levels on systems with 4Tb. It is no fun to run that many times.

Another advantage of above script is that it first reports. Actually removing is an option.

No warnings File::Find, because I don't care if folders are removed while running or symbolic links still exist or NFS (if a user chooses to do so) falls inactive.

Enjoy, Have FUN! H.Merijn

In reply to Re: Clean Up Empty Directories by Tux
in thread Clean Up Empty Directories by GotToBTru

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others cooling their heels in the Monastery: (5)
    As of 2020-09-19 22:53 GMT
    Find Nodes?
      Voting Booth?
      If at first I donít succeed, I Ö

      Results (116 votes). Check out past polls.