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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.
|
|