1830242 Members
2172 Online
109999 Solutions
New Discussion

Re: Need a script !

 
SOLVED
Go to solution
Silver_1
Regular Advisor

Need a script !

All,

We have a backup job which creates a directory everyday with the name of the day. I would like to delete the directory 2days old every day at 6:00 sothat it will not fillup the filesystems.

root@hp01:/backup# ll
total 0
drwxr-x--- 2 root sys 96 Aug 30 12:35 20060830
drwxr-x--- 2 root sys 96 Aug 31 12:35 20060831
drwxr-x--- 2 root sys 96 Sep 1 12:35 20060901
root@hp01:/backup#

How can i do this ?

5 REPLIES 5
IT_2007
Honored Contributor

Re: Need a script !

you can use find command and delete them files under two days.

find /backup -xdev -ctime +2 -type f -exec rm {} \;
IT_2007
Honored Contributor

Re: Need a script !

you can add it in cronjob to run daily like this:

Invoke command: crontab -e

and add following line which will run at midnight 12:00am everyday.

0 0 * * * find /backup -xdev -ctime +2 -type f -exec rm {} \;
Patrick Wallek
Honored Contributor

Re: Need a script !

The command above will only delete the files, not the directory.

If you want to delete the directory:

# find /backup -type d -mtime +2 -exec rm -rf {} \;

A. Clay Stephenson
Acclaimed Contributor
Solution

Re: Need a script !

Create a script that is called by cron daily at 0600.
--------------------------------------
#!/usr/bin/sh

PATH=${PATH}:/usr/bin:/usr/local/bin
export PATH

typeset DTMINUS2=""
typeset BDIR=/backup
typeset DNAME=""
typeset -i STAT=0

DTMINUS2=$(caljd.sh -y -s $(caljd.sh -p 2))
DNAME=${BDIR}/${DTMINUS2}
if [[ -d "${DNAME}" ]]
then
rm -rf "${DNAME}"
STAT=${?}
fi
exit ${STAT}

--------------------------------

Now download the attached caljd.sh and install it in /usr/local/bin and make it executable.

Invoke as caljd.sh -u for usage and example to see what it is doing.
If it ain't broke, I can fix that.
James R. Ferguson
Acclaimed Contributor

Re: Need a script !

Hi Silver:

You could also use this:

# cd /backup && perl -MPOSIX=strftime -e '$name=strftime("%Y%m%d",localtime(time-(86400*2)));system("rm -r $name")'

Regards!

...JRF...