Hi folks. Today I had to write script that would delete all the empty directories in a directory tree recursively. Ie, removing any empty leaf dirs, and then moving up and removing the next layer if it is now empty, etc.

I thought that there must be a nice neat compact perlish way to do this, but it seemed to elude me. Anyway, the code I came up with is below, I'm wondering how compact this can be golfed down to by the folks out there that are good at golf. Requirements are to take a list of root dirs on the command line, delete any empty dirs, and then print out the number of dirs removed and the number kept. Ill accept anything that matches /D:\d+\s+F:\d+/ as the output string. It should die if there any errors removing the dirs, and the code need not worry about circular directories due to symlinks or the like. Have fun. :-) (You can assume there are no hidden or system files lurking in the tree, thanks davis for raising this issue.)

use strict; use warnings; use File::Find; use Cwd; my $deleted=0; my $keep=0; $|++; sub recurse { my $dir=shift; chdir $dir or die "chdir $dir:$!"; my $dirname=cwd; #print "$dirname\n"; my @dirs; my @files; for my $name (glob '*') { next unless $name=~/[^.]/; if (-d $name) { push @dirs,$name if recurse($name); } else { push @files,$name; } } chdir ".."; unless (@dirs or @files) { print "X: $dirname\n"; rmdir $dir or warn "$dirname:$!"; $deleted++; } else { print "k: $dirname (d: " . scalar(@dirs) . " | f: ".scalar(@files).")\n"; $keep++; } return @dirs+@files; } recurse $_ for @ARGV; print "Deleted $deleted directories, kept $keep directories\n";

---
demerphq

    First they ignore you, then they laugh at you, then they fight you, then you win.
    -- Gandhi



In reply to (golf) Recursively delete empty directories from a tree by demerphq

Title:
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, details, 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, summary, 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.