http://www.perlmonks.org?node_id=828155


in reply to How to Get the Last Subdirectories

I don't think there is a way to do it inline in the File::Find subroutine without recursion, but if you can wait until the end, I think this should do it:

#!/usr/bin/perl use strict; use File::Find; my %seen; find( sub { if (-d) { $seen{$File::Find::name}++; delete $seen{$File:: +Find::dir} }; }, '/tmp/a' ); print join "\n", (keys %seen, undef);
This creates a hash key for every directory, and deletes every key that is the same name as the parent. Therefore directories who have no children will never get deleted.