Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: Find Empty directories and removing it

by Tanktalus (Canon)
on Jan 04, 2006 at 05:02 UTC ( #520793=note: print w/replies, xml ) Need Help??


in reply to Find Empty directories and removing it

Just remove them. If they're not empty, rmdir will fail. In this case, you don't care that it fails, ignore the error, and keep going.

use File::Find; finddepth(sub { rmdir $_ if -d }, $some_path);

Update: added 'depth' as per merlyn's correction.

Replies are listed 'Best First'.
Re^2: Find Empty directories and removing it
by merlyn (Sage) on Jan 04, 2006 at 05:33 UTC
    You have to use finddepth, not find, because you need to act on the current directory last. Consider a directory that is empty except for containing two other empty directories.

    -- Randal L. Schwartz, Perl hacker
    Be sure to read my standard disclaimer if this is a reply.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://520793]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (4)
As of 2022-05-28 07:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you prefer to work remotely?



    Results (98 votes). Check out past polls.

    Notices?