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

Cleanup scripts 1

Status
Not open for further replies.

ddrillich

Technical User
Jun 11, 2003
546
US
Good Day,

We have a daily cron job that runs a cleanup script –

export DIRS="/opt/autonomy/AutonomyMigration/logs \
/opt/autonomy/short_staging/migrate \
/opt/autonomy/long_staging/migrate \
/opt/autonomy/archive_staging/migrate"

for d in $DIRS
do
find $d -ctime +3 -exec rm {} \; >> ~/custom/logs/crontab.log 2>&1
done


Unfortunately, it leaves behind it the empty directories –

autonomy @ sbkj2ksrchidxs01 :: /opt/autonomy/short_staging/migrate

drwxr-xr-x 3 autonomy text 4096 Jun 17 01:01 20100617_010102
drwxr-xr-x 3 autonomy text 4096 Jun 18 01:01 20100618_010101
drwxr-xr-x 3 autonomy text 4096 Jun 19 01:01 20100619_010101
drwxr-xr-x 3 autonomy text 4096 Jun 20 01:01 20100620_010102
drwxr-xr-x 3 autonomy text 4096 Jun 21 01:01 20100621_010101

So the question is, how can the script handle these directories as well?

Regards,
Dan

 
Try this:

Code:
for d in $DIRS
do
   find $d -type f -ctime +3 | xargs rm
   find $d -depth -type d -empty -exec rmdir {} \;
done >> ~/custom/logs/crontab.log 2>&1

I changed the first one to use xargs because it is far more efficient to remove files in batches rather than one at a time. The -type f should prevent any errors relating to trying to rm a directory.

For the second one each rmdir needs to be executed independently in case it is a subdirectory and the parent may be a candidate for removal too, so xargs is unsuitable.

Annihilannic.
 
Many many thanks Annihilannic. Absolutely beautiful.

Regards,
Dan
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top