Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

How do I delete a directory without emptying it first?

( #29734=categorized question: print w/ replies, xml ) Need Help??
Contributed by Adam on Aug 26, 2000 at 00:53 UTC
Q&A  > directories


Description:

On an NT machine I managed to do it with:
`rd /s /q "$_"` for grep {-d} glob '*';
(That deletes all the sub directories (and their contents) in the current dir.) But I was wondering if there is a "Perl" way.

Answer: How do I delete a directory without emptying it first?
contributed by Zombie DracoWulf

The simplest cross-platform method I found was with the module File::Path. You pass it a matrix of dirs you want to delete. It will delete the entire tree.

use File::Path; $delA = '/tmp/foo'; $delB = '/tmp/bar'; rmtree([$delA, $delB]);
Answer: How do I delete a directory without emptying it first?
contributed by gav^

I'm not sure if this is a better method than using File::Path, but it is a hell of a lot better than rolling your own with File::Find :)

use File::Remove qw(remove); remove \1, "dir";

gav^

Answer: How do I delete a directory without emptying it first?
contributed by ZZamboni

The question is: what would happen to the files in that directory after you delete it? Moved? Deleted? That's why most operating systems don't allow you to remove a directory that has any files in it. The operating system cannot guess what you want done with the files. You have to do something with them first.

Answer: How do I delete a directory without emptying it first?
contributed by cLive ;-)

*nix/linux?

my $dir = '/directory/path'; system('rm','-r',$dir);
And if you're doing this from input data... well if you are, you're probably mad :)

cLive ;-)

Answer: How do I delete a directory without emptying it first?
contributed by trala

Okay, just came up with this for a script I am working on. If this is really stupid, please someone let me know!

use File::Find; finddepth (\&remove_dir, "$directory"); rmdir ( "$directory" ) or die ("Could not remove $directory"); sub remove_dir { # for a directory, this will be 0 if ( ! (stat("$File::Find::name"))[7] ) { rmdir("$File::Find::name"); } else { unlink("$File::Find::name"); } }
In theory, finddepth will find the deepest items first (files inside the directories). Once the files are unlinked, the directories will be empty and can be removed.
Answer: How do I delete a directory without emptying it first?
contributed by merlyn

I suppose you could go in and edit the i-node, and then let the next fsck run clean up your mess.

But the real question is, "why". Why do you want to do that?

Please (register and) log in if you wish to add an answer



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • 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, 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, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            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.
  • Log In?
    Username:
    Password:

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

    How do I use this? | Other CB clients
    Other Users?
    Others scrutinizing the Monastery: (10)
    As of 2014-07-22 21:57 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      My favorite superfluous repetitious redundant duplicative phrase is:









      Results (129 votes), past polls