By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,695 Members | 1,956 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,695 IT Pros & Developers. It's quick & easy.

Removing directory using perl script

P: 89
Hi I tried a simple script to remove the directory as below:

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl -w
  2. use File::Find;
  3. #finddepth(sub{rmdir},'.')
  4. system('rm -rf test3');
1. finddepth(sub{rmdir},'.') removed all the empty directories.
2. But I wanted to remove all the directories which are not empty. Hence I tried
Expand|Select|Wrap|Line Numbers
  1. system('rm -rf test3');
to remove all directories inside test3 directory and it works.

Please let me know whether there are any other elegant way of removing directories? Removing the directories takes more time (approximately 20 minutes) and hence is it advisable to fork the process and do?Thanks.
Sep 11 '08 #1
Share this Question
Share on Google+
6 Replies


numberwhun
Expert Mod 2.5K+
P: 3,503
Well, instead of invoking the system 'rm' command, you could use the rmdir function provided by Perl.

Regards,

Jeff
Sep 11 '08 #2

KevinADC
Expert 2.5K+
P: 4,059
I think File::Path will delete empty and non-empty folders. How elegant it is though, I have no idea. You could traverse the directories and sub directories and delete all the files then delete the folders.
Sep 11 '08 #3

P: 3
Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2. deldir("test"); # or deldir($ARGV[0]) to make it commandline
  3.  
  4. sub deldir {
  5.   my $dirtodel = pop;
  6.   my $sep = '\\'; #change this line to "/" on linux.
  7.   opendir(DIR, $dirtodel);
  8.   my @files = readdir(DIR);
  9.   closedir(DIR);
  10.  
  11.   @files = grep { !/^\.{1,2}/ } @files;
  12.   @files = map { $_ = "$dirtodel$sep$_"} @files;
  13.   @files = map { (-d $_)?deldir($_):unlink($_) } @files;
  14.  
  15.   rmdir($dirtodel);
  16. }
From URL REMOVED PER SITE POLICY
Nov 26 '09 #4

Expert Mod 100+
P: 589
That's a poor example, which you should not use.

Here's a cleaner, shorter, and more efficient method.
Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2.  
  3. use strict;
  4. use warnings;
  5. use File::Path qw(remove_tree);
  6.  
  7. remove_tree('test');
Or, if you're on a *nix system, you could do this:
Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2.  
  3. use strict;
  4. use warnings;
  5.  
  6. system('rm -rf test');
Nov 27 '09 #5

numberwhun
Expert Mod 2.5K+
P: 3,503
Sweetypie,

First, I have removed your URL that you put into your post. We do not allow personally promotional URLs in the forums, thus, why I have removed it.

Second, please keep in mind the age of the posts that you are replying to. This one was over a year old.


Regards,

Jeff
Nov 27 '09 #6

P: 3
Using system command is the worst way of doing it.
Although the use File::Path qw(remove_tree) is the best.

@jeff : oops ...
Dec 3 '09 #7

Post your reply

Sign in to post your reply or Sign up for a free account.