How to get previous month files

Hi,

My task to to delete files which are of previous months.

I have files named as follows *CCYYMMDD.xls. on a particular day i have delete previous months files

i.e in Dec i have delete all nov files which look like 200511DD.XLS

in Jan i have to delete all Dec files 200512DD.xls

Any ideas

Use datecalc by Perderabo to get the date exactly 30 days ago. This is a fast date calculation script.

Just one question though, how are you going to be deleting files of dates like 30th/31st January seeing that February would never have those many days?

Thank you but i don't want to use any script again...

My main aim is to get a string called "200511" into variable when the its dec 2005
and "200512" when its jan 2006 and '200601' when its feb 2006.

once i get it i can issue mdelete *pervmonth*.xls ( i have to logon to a FTP site and delete files).

In this case i don't think there is an issue of feb not having 30th and 31. becasue on feb 1st all jan files will be deleted.

Try using this snippet:

#!/bin/ksh

year=$(date +%Y)
month=$(date +%m)

month=$(($month-1))
if [ $month -lt 1 ]; then
        year=$(($year-1))
        month=12
fi
echo $year$month

This should work. If you can, test the script by setting your system date to Jan 2006.

Thank you it worked... but i have one more doubt

how can i set systemdate to Jan2006? once i set i can recvert it back right???

also now since its dec 2005 i am getting "200511" for previous month string.

when its feb to oct previous months will be single digit how can i append 0 to it

i.e i should ge 200601, 200602,200603 when its feb2006,march2006 and april 2006 respectively

Am adding to blowtorch's solution.

Since it is ksh, use typeset.

#!/bin/ksh
typeset -Z2 month=00

year=$(date +%Y)
month=$(date +%m)

month=$(($month-1))
if [ $month -lt 1 ]; then
        year=$(($year-1))
        month=12
fi
echo $year$month

We receive files daily and archive them in a directory... I want my script to check and delete all teh files that older than 3 months though my script... I have naming convention for the files... How can i do this???

change line
echo $year$month
to
printf '%s%2s' $year $month | tr ' ' '0'

you might find this useful too
datecalc