Help with Variable

If I am naming all my files with current date, so how do I get fine names older than 2 months.

For example file name are

MYDATE=`date +"%Y_%m_%d"`
Myfile=D$MYDATE_file.txt

So I will be creating files on a daily basis. So if I have to delete files older than 60 days. How do I do it using the variable? ( I don't want to delete using mtime or atime).

I did something like this:

month1=`date +"%m"`
month1=`expr $month1 - 2`
echo "Month1 :" $month1
myfile2=D`date +"%Y"`_$month1`date +"_%d"`
echo "2 old month date: " $myfile2

But I get month as 9 instead of 09. So is there an easier way to do it all in one line??

month1=$( printf "%02d" `expr $month1 - 2` )

...and what exactly will happen in January?

Thanks vgersh99. Yes that's a good question.

Is there a way to format Date to go back to 60 days?

If you have GNU date you can use:

date -d today-60days +"%Y_%m_%d"

Have you considered using the find command

find . -mtime +60 -print

And to delete what you find:

find . -mtime +60 -print | xargs rm

We don't have GNU date. We have AIX as OS.

deleting on mtime or atime would have been easiest, but there are some files which may have been copied today for some processing but their date created will be older, so they also need to be deleted.

:confused:

Why won't mtime work on those?

Futher to Chubler_XL

We can build on this and produce a testable construct:

find . -type f -mtime +62 -print | while read filename
do
       echo rm "${filename}"
done

Remove echo if you are happy with which files will be deleted.