CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Need a Custom or Prewritten Perl Program?: I need a program that...:
Perl script that recursively deletes all subdirectories but leaving one

 



javedakbar
New User

Aug 5, 2014, 12:13 PM

Post #1 of 6 (4581 views)
Perl script that recursively deletes all subdirectories but leaving one Can't Post

Hi guys,
I am fairly new with perl. Would really appreciate some help. Here is what I am trying to have the script do:

1. change directory to my desired directory: /glb/home/usjak8/myperl

2. There are 4 directories under that directory: dir1, dir2, dir3, dir4.

3. I would like to recursively delete dir1, dir2 and dir4 with all the files under them removed as well.

4. I want to leave dir3 alone.

5. Once dir1, dir2 and dir4 are deleted I want to log the message in a file saying that these are the files that are deleted.

6. File names will change every week except for dir3.

7. No need to print anything as this script will run via cron on every Monday morning at 5am. The only way administrator would know that the directories are deleted is by looking at the output file showing the deleted directories or cd'ing to the /glb/home/usjak8/myperl directory and doing an ls there.

Thanks in advance for your help.

Javed


Zhris
Enthusiast

Aug 6, 2014, 12:41 PM

Post #2 of 6 (4560 views)
Re: [javedakbar] Perl script that recursively deletes all subdirectories but leaving one [In reply to] Can't Post

cross posted on Perl Monks.


Laurent_R
Veteran / Moderator

Aug 9, 2014, 2:48 PM

Post #3 of 6 (4366 views)
Re: [javedakbar] Perl script that recursively deletes all subdirectories but leaving one [In reply to] Can't Post

Do you have subdirectories in the dir1, dir2 and dir4 directories from which you want to delete the content? Or just plain files?


javedakbar
New User

Aug 9, 2014, 3:05 PM

Post #4 of 6 (4364 views)
Re: [Laurent_R] Perl script that recursively deletes all subdirectories but leaving one [In reply to] Can't Post

Yes I do have subdirectories and I do want to do delete all of them.


Laurent_R
Veteran / Moderator

Aug 10, 2014, 8:03 AM

Post #5 of 6 (4328 views)
Re: [javedakbar] Perl script that recursively deletes all subdirectories but leaving one [In reply to] Can't Post

Then you probably want to use the standard File::Find (http://search.cpan.org/~rjbs/perl-5.20.0/ext/File-Find/lib/File/Find.pm) module to walk through the directory.

If you want to code it yourself, you could do a recursive function traversing the directory tree. Something like like this:


Code
use strict; 
use warnings;

die "Need to pass the directory to be deleted as argument\n" unless @ARGV;
my $start_dir = $ARGV[0];

traverse_dir ($start_dir);

sub traverse_dir {
my $path = shift;
my @dir_entries = glob("$path/*");
foreach my $entry (@dir_entries) {
next if $entry eq "dir3" or $entry eq "." or $entry eq "..";
print "File to be deleted = $entry\n" if -f $entry;
traverse_dir($entry) if -d $entry;
print "Directory to be deleted = $entry\n" if -d $entry;
}
}


The above program just prints out the files and directories to be deleted, without actually deleting them, for the purpose of carefully testing what the actual program would do. Once you are happy that it is picking up what you want to delete (and only that), then you can change the two print code lines as follows:

Replace:

Code
print "File to be deleted = $entry\n" if -f $entry;

with:

Code
print "deleting file $entry\n"  if -f $entry; 
unlink $entry if -f $entry;

and:

Code
print "Directory to be deleted = $entry\n" if -d $entry;

with:

Code
print "deleting directory $entry \n" if -d $entry; 
rmdir $entry if -d $entry;


Whenever the program finds a directory, it first goes into that directory (by calling recursively traverse_dir) and deletes everything under it, so that the directory is empty (and can be deleted) when it tries to delete the directory.

You don't need to cd to the directory for it to work, just pass a relative or absolute path as an argument to the program.

Please be VERY careful using the modified program, you might eradicate an entire file system if you pass the wrong parameter. Also note that I have tested the file and directory printing version at the top of this post, I haven't tried a version actually deleting the entries.


javedakbar
New User

Aug 10, 2014, 8:42 PM

Post #6 of 6 (4275 views)
Re: [Laurent_R] Perl script that recursively deletes all subdirectories but leaving one [In reply to] Can't Post

Thank you so much for your help.

Javed

In Reply To

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives