Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

comment on

( [id://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

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (5)
As of 2024-03-29 00:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found