Delete Directories

Hello All,

My shell script runs everyday to update certain database. Everytime the script runs it creates a directory, with "current date". These directories contain log and data files:

 
HOME_DIR/database_name/20120417/*
HOME_DIR/database_name/20120416/*
HOME_DIR/database_name/20120415/*

Now, when I run the update on "20120418", I want to delete "20120415" --- therefore keeping only 3 updates at a time. When it updates on 20120419 - i should only have 20120416 & 20120417 directories.

Is there a way to incorporate this in my script ???

Thanks in advance !

Hi,

try this:

rm -rf /backupdir/"$(date -d "-2 day" +"%Y%m%d")"*

Thanks! I made a typo in my previous message:

"I want to delete "20120415" and not "20120416". Therefore keeping the last 3 latest directories"

Please post what Operating System and version you are running and what Shell you use.

Does this GNU date command work on your system?

date -d "-2 day" +"%Y%m%d"

At exact time of day will this deletion process run? (This question is important).

opensuse 11.4 on i686, bash 4.1.10
You can use this command in cron.

crontab -e

and put a line like

30 23 * * * /root/myscriptDelete.sh 

for execute the command at 11:30 pm

I am using the following version

 
SUSE Linux Enterprise Server 10 (x86_64)
VERSION = 10

Yes, the command works on my system.

date -d "-2 day" +"%Y%m%d"

Also, this deletion will occur everyday at around 6pm...

Any suggestions?