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!

6 month directory deletion

Status
Not open for further replies.

ariell

Programmer
Jul 3, 2002
12
US
Thanks everyone. I have tried to get these commands to work;however, the -mtime is not recognized. The commands deletes the entire directory, which I want to keep 6 months. (see commands used below) Any other scripts out there created to do this function, or ideas.


I am using AIX and need to create a script that deletes sub directories under a directory. These sub-directories are over a year an a half old or more. I need to keep only five months and need it to run monthly to only keep five months from this point. I have tried the -mtime commands, and nothing seems to really work with deleting directories. Anyone with any suggestions?

Supplied function:

find <dirname> -type d -mtime -180 -exec rm -rf {} \;

 
I use a case function. The directories are called Jan02, Feb02, Mar02 etc. if the current month is July then rm -rf the directory which is 6 months old i.e. Jan02

a bit like

MONTH=`date | cut -f2 -d&quot; &quot;`
case $MONTH in
Jan) rm -rf /u04/ib_arch/Sep*
rm -rf /u04/out_sent/sep*
;;
Feb) rm -rf /u04/ib_arch/Oct*
rm -rf /u04/out_sent/oct*
;;
Mar) rm -rf /u04/ib_arch/Nov*
rm -rf /u04/out_sent/nov*
;;
Apr) rm -rf /u04/ib_arch/Dec*
rm -rf /u04/out_sent/dec*

etc, etc.....

Alex


 
Hi,

Firstly , your find command try with +180 rather -180
+180 means files that have not been modified for the past 180 days.
You have to be very careful when you are deleting directories for example if you have directory structure

/dir1/dir2/dir3/dir4

dir1 has time stamp say 6th aug
dir2 has time stamp say 2nd jul
dir3 has time stamp say 1st jul
dir4 has time stamp say 6th jul

and you find any dirs have not been modified for 3 days
it will remove dir2 and everything under it , so you'll be left with /dir1 , even though say dir4 has todays files in it.

You can test this if you want by creating a directory structure , and changing the time stamp of the the directory
using the touch command i.e. mkdir /tmp/dir1
touch -am 0701100102 /tmp/dir1
[mmddhhmmyy]
then use find command .

It may even be better to delete files that have not been accessed/modified for a specfied time.

HTH
 
Possibly better just to use the -type f option with find so that only plain files are removed, avoiding the risk of deleting directories at all. Cheers.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top