473,399 Members | 2,858 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

Removing directory using perl script

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
6 10917
numberwhun
3,509 Expert Mod 2GB
Well, instead of invoking the system 'rm' command, you could use the rmdir function provided by Perl.

Regards,

Jeff
Sep 11 '08 #2
KevinADC
4,059 Expert 2GB
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
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
RonB
589 Expert Mod 512MB
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
3,509 Expert Mod 2GB
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
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

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

Similar topics

1
by: R D | last post by:
Thanks for quick reply. Cwd didn't work because it gives what directory you are currently in not the directory the script is in! Can't get FindBin to work..any idea what's wrong with the...
6
by: Joe Cosby | last post by:
This seems like a simple thing, but I can't find anything in the "Perl Cookbook" or the online Perl docs to answer this. When a script runs from my host's server, the working directory doesn't...
8
by: Shabam | last post by:
I have a command script that backs up a user account. This involves moving files from different directories into an archive. Now, I need that script to back up all user accounts on the system,...
3
by: Alexandre H. Guerra | last post by:
Hello I need to process a SQL monitoring log stored in a table to group the statements that change just the constants in it. Ex: select a,b,c from table where (a = 'xyz' and b = 123 and c !=...
3
by: kevin | last post by:
Is that even possible? I am creating a web service in .NET to expose some already created .NET programs to other groups. One group is writing the client in PERL, and thus wishes the wsdl schema...
1
by: dkmarni | last post by:
Hi, I am trying to do this perl script, but not able to complete it successfully. Here is the description what the script has to do.. Accept two and only two command line arguments. Again,...
1
by: kernelsanders | last post by:
Hi, I'm still new to perl and have to figure out how to feed a complete directory listing as parameters into another perl script. I've looked around online and found command examples that use...
0
by: phil469 | last post by:
I'm having an issue when trying to read a file in a user's homedir from a cgi script. I have a virtual host section in my httpd.conf file which I'll include. The cgi script is a very basic script...
6
by: kinedesigner | last post by:
We're using a perl script to fetch images from the inhouse server & pump them to a remote server with much higher bandwidth. I want to remove the Everyone account from the target folder on the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.