Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: Find Empty directories and removing it

by Tanktalus (Canon)
on Jan 04, 2006 at 05:02 UTC ( [id://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?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (3)
As of 2024-04-19 05:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found