Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need a script that will delete all folders that are older than 2 month

Status
Not open for further replies.

jewel464g

Technical User
Jul 18, 2001
198
US
I have a huge array of folders that are automatically generated by my IDS software. I need a script that will search through these folders and delete the ones that are over 2 months old. I have written some small shell scripts, but I just don't know how to write this one. Can someone be of some assistance?? Linux platform. I know that I can manually delete with "rm -Rf folder_name" But this would take about 3 days of continuous work to accomplish.

thanks,
Jewel When faced with a decision, always ask, 'Which would be the most fun?'
 
Jewel,

I use something similar to what you're requesting to remove files that are more than 15 days old. following is the script I use. It's fairly commented so I know what I was thinking when I wrote it.
Some of the guru's here at tek-tips helped me write it so it's only right that I should share.
I haven't attempted to make any changes for you're app. but I'm sure you can get it to work for you.

#======================================================

#!/usr/bin/perl -w

$^T = time; # the Time when the script starts running
# because stat() and -M uses this to compare age
# of the file from now
$dir = "o:\\data\\dpboxes\\store";

opendir (DIR, $dir) || die "Shucks $!";
@files = readdir (DIR);
closedir (DIR);
open (Store, ">>e:\\perl\\code\\rts-store.txt") || die "Shucks $!";

foreach $file (@files)
{
next if($file =~ /^\./); # skip if it's the '.' or '..' directories
@data = stat("$dir\\$file"); # get info from file - like timestamp
next unless -M ("$dir\\$file") > 15; # only get files that are more than
# 15 days old
# the $dir makes sure it's looking
# in the correct directory
$write_secs = $data[9]; # the [9] gets $mtime from stat()
$write_secs += 1900; # add 1900 to get into year 2000 +
print "\n$file - ", scalar localtime($write_secs);
# print the file name and the timestamp
print Store "\n$file - ", scalar localtime($write_secs);
unlink "$dir\\$file"; # delete the file
$cnt++; # keep track of how many were done
}
close (Store);
print "\n$cnt";

#===============================================================================

Hope this helps!
tgus

____________________________________________________
A father's calling is eternal, and its importance transcends time.
 
You could do this with a script if you really want to, but there's a simple command that can do exactly what you're looking for.

find directory -mtime 60 -exec rm -rf {} \;

directory would be the top of your array of directories.

This command will remove any file or directory in the search tree that was modified more than 60 days ago.

You could speed it up a little if you just want to check the time on the directory, and not all the files under it, by adding one more parameter.

find directory -type d -mtime 60 -exec rm -rf {} \;

Adding the type clause makes the command only act on directories (-type d) that were modified more than 60 days ago (-mtime 60).

see the man page for find, it's got lots of interesting tricks.

Richard
 
I use a shell script in cron:

0 0 * * * find /dir_name -type f -ctime +60 -exec rm {} \;


--donovan
 
I would use the find command too.

Just 2 comments:

to rnorton:
Do not forget the + before 60:
Code:
-mtime 60
is true if the entry (file or directory) was modified exactly 60 days ago while
Code:
-mtime +60
is true if the entry was modified 60 or more days ago.

to all: beware that a directory (folder) is modified only when entries (files or subdirectories it contains) are added, deleted or renamed but not if its entries are themself just modified.
So:
Code:
find /myTopSearchDir -type d -mtime +60 -exec rm -rf {} \;
could remove your whole
Code:
/myTopSearchDir
if no subdirectories or files have been added, deleted or renamed at the top level (I mean directly in
Code:
/myTopSearchDir
).

A safer way to do this, is to list all the directories to check as first arguments to find and use -prune to not go down the tree:
Code:
find /myTopSearchDir/*/myIDSDirs* -type d -prune -mtime +60 -exec rm {} \;
means: "for all arguments /myTopSearchDir/*/myIDSDirs* of type directory, do not go down the tree, check if modified then delete".

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top