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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using find to delete a specific directory and its contents 1

Status
Not open for further replies.

ITGoddess

IS-IT--Management
Dec 13, 2001
8
US
Greetings-

Having some trouble with syntax for the "find" command. Using HP_UX 10.20.

I place a new directory "todaysdate" containing copied files in a specific directory "/db2/bkrpts" daily. This is done with the following script executed from crontab.

#Get the files, make the directory
/bin/cp -pR `find /tmp /users/download/XXX -type f -user osi -mtime 0` /db2/bkrpts/`date +"%m%d%ym`

This results in:
/db2/bkrpts/040202
/db2/bkrpts/040302
and so on....

I want to delete the directories and their contents after 25 days have passed. I do NOT want to delete the /db2/bkrpts directory. Ideally directory 04/02/02 and contents would be removed from /db2/bkrpts on 04/28/02.

This is what I have thus far. I tested with a test directory called 040302m:

find /db2/bkrpts/ -type d -name `??????m` -mtime +25 -exec rm -r {} \;

Unfortunately results in sh: 040302m: not found.

Sorry for what is likely a very mundane question. I would greatly appreciate any help offered.

Thanks,
-Michelle
 
find /db2/bkrpts/ -type d -mtime +25 -exec rm -rf {} \;

the -name should't be necessary, -type
and -mtime should suffice.

I also change the rm -r to rm -rf
which overrides the file permissions if necessary.

Robert Robert G. Jordan

Robert@JORDAN2000.com
Unix Sys Admin
Chicago, Illinois U.S.A.
[lightsaber]
 
Robert,
This will also delete the bkrpts directory!
A safer solution:
Code:
DIR=/db2/bkrpts
NAME=`basename $DIR`
find $DIR -type d ! -name $NAME -mtime +25 -exec rm -rf {} \;
Cheers, Neil
 
<grin> scary stuff, I chickened out from answering this one! Mike
&quot;Experience is the comb that Nature gives us after we are bald.&quot;

Is that a haiku?
I never could get the hang
of writing those things.
 
Yes, I was trying to exclude the parent directory /db2/bkrpts, hence the `??????m`.

Scary stuff indeed!! Absolutely!! I test the 'heck' out of crontab scripts, particularly ones involving automated file removals.

Many thanks for your suggestions/comments. I very much appreciate it.

Regards &
-Michelle
[bigcheeks]
 
Just an aside....

I once worked for a very nice guy who I won't name; worked for and with him for ten years until he got a job in Holland. He was, however, sometimes impulsive. He managed to delete the entire contents of every filesystem on a Sequent machine using a recursive delete command I won't reproduce here. Twice, he did it twice....

He now claims it doesn't count because it was a development machine :) Mike
&quot;Experience is the comb that Nature gives us after we are bald.&quot;

Is that a haiku?
I never could get the hang
of writing those things.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top