Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

How do I delete a directory without emptying it first?

by Adam (Vicar)
on Aug 26, 2000 at 00:53 UTC ( [id://29734]=perlquestion: print w/replies, xml ) Need Help??

Adam has asked for the wisdom of the Perl Monks concerning the following question: (directories)

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.

Originally posted as a Categorized Question.

Replies are listed 'Best First'.
Re: How do I delete a directory without emptying it first?
by DracoWulf (Initiate) on Sep 12, 2000 at 01:19 UTC
    The simplest cross-platform method I have found was with the File::Path function rmtree. You pass it an array of directories you want to delete. It will delete the entire tree.
    use File::Path; rmtree([ '/tmp/foo', '/tmp/bar' ]);
Re: How do I delete a directory without emptying it first?
by gav^ (Curate) on Jan 17, 2002 at 07:04 UTC
    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^

Re: How do I delete a directory without emptying it first?
by swngnmonk (Pilgrim) on Mar 09, 2004 at 17:50 UTC
    What about rmtree?
    use File::Path qw(rmtree); rmtree($dir);
    File::Path and File::Path::Functions are great to use when you're looking for perl solutions to file management, and want to avoid system-specific issues.

    Originally posted as a Categorized Answer.

Re: How do I delete a directory without emptying it first?
by ZZamboni (Curate) on Aug 26, 2000 at 02:36 UTC
    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.
Re: How do I delete a directory without emptying it first?
by trala (Initiate) on Jan 17, 2002 at 05:07 UTC
    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.
      To make your example a bit shorter you could go for:
      use File::Find; finddepth( sub { (-d) ? rmdir : unlink }, $directory);

      But I would recommend using File::Remove as per my example.

      gav^

Re: How do I delete a directory without emptying it first?
by cLive ;-) (Prior) on Apr 02, 2001 at 08:59 UTC
    *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 ;-)

Re: How do I delete a directory without emptying it first?
by Anonymous Monk on Mar 09, 2004 at 16:04 UTC
    rm -ri directory before use this command, please man rm first.

    Originally posted as a Categorized Answer.

Re: How do I delete a directory without emptying it first?
by merlyn (Sage) on Aug 26, 2000 at 01:00 UTC
    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?

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (2)
As of 2025-07-12 17:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.