Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Delete Directory & Everything in it

Posted on 1999-07-15
8
Medium Priority
?
143 Views
Last Modified: 2010-05-18
Hi...
I know that unlink is the command to delete one file.
How can I remove a directory and everything in it?
0
Comment
Question by:jknickelbein
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 5

Expert Comment

by:prakashk021799
ID: 1214169
If there are no subdirectories in $dir:

unlink <$dir/*>;
# remove files starting with a '.', but skip . and ..
unlink grep {m/^\.+$/ ? undef : $_} <$dir/.*>;
rmdir $dir;

If there _are_ subdirectories in $dir, you can do this recursively:

sub remove_directory($)
{
    my $dir = shift;
    chdir $dir;
    for (glob("*"), glob(".*")) {
        next if /^\.+$/;    # skip . and ..
        if (-d $_) {
            remove_subdirectory($_);
        } else {
            unlink $_;
        }
    }

    chdir '..';
    rmdir $dir;
}

You will need to add error checking to the above code.
0
 
LVL 5

Expert Comment

by:prakashk021799
ID: 1214170
Oops! one change.

Change the call to 'remove_subdirectory' to 'remove_directory' in the above code (somewhere in the middle of the subroutine).
0
 
LVL 5

Expert Comment

by:thoellri
ID: 1214171
# or

use File::Path;
rmtree("/the/directory/to/delete");

# tobias
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 5

Expert Comment

by:prakashk021799
ID: 1214172
Yes. Using File::Path is definitely better. I completely forgot about it.
0
 
LVL 8

Expert Comment

by:shlomoy
ID: 1214173
why not just use "rm -R dirname" at the prompt?
0
 
LVL 5

Expert Comment

by:prakashk021799
ID: 1214174
> why not just use "rm -R dirname" at the prompt?

Because its not perl.
0
 

Author Comment

by:jknickelbein
ID: 1214175
Please answer, thoellri, you're answer seems the most sensible, and it works!
Thanks!
0
 
LVL 5

Accepted Solution

by:
thoellri earned 120 total points
ID: 1214176

      use File::Path;
      rmtree("/the/directory/to/delete");

      # tobias
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans

722 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question