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.

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:

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.