# Help with Variable

**URL:** <https://community.unix.com/t/help-with-variable/276535>\
**Category:** Shell Programming and Scripting\
**Created:** [November 1, 2010, 5:49pm UTC](https://community.unix.com/t/help-with-variable/276535 "2010-11-01T17:49:19Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Hangman2](https://community.unix.com/letter_avatar/hangman2/32/5_5575768a8748004e209b776fc1b2916d.png) [@Hangman2](https://community.unix.com/u/Hangman2)\
**Post date:** [November 1, 2010, 5:49pm UTC](https://community.unix.com/t/help-with-variable/276535/1 "2010-11-01T17:49:19Z")

</div>

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

```nohighlight
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:

```nohighlight
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??

---

<div class="post-metadata">

**Author:** ![anbu23](https://community.unix.com/user_avatar/community.unix.com/anbu23/32/351_2.png) [@anbu23](https://community.unix.com/u/anbu23)\
**Post date:** [November 1, 2010, 6:25pm UTC](https://community.unix.com/t/help-with-variable/276535/2 "2010-11-01T18:25:28Z")

</div>

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

```

---

<div class="post-metadata">

**Author:** ![vgersh99](https://community.unix.com/user_avatar/community.unix.com/vgersh99/32/14851_2.png) [@vgersh99](https://community.unix.com/u/vgersh99)\
**Post date:** [November 1, 2010, 6:55pm UTC](https://community.unix.com/t/help-with-variable/276535/3 "2010-11-01T18:55:33Z")

</div>

> [@anbu23](#):
>
> ```plaintext
> month1=$( printf "%02d" `expr $month1 - 2` )
> 
> ```

...and what exactly will happen in January?

---

<div class="post-metadata">

**Author:** ![Hangman2](https://community.unix.com/letter_avatar/hangman2/32/5_5575768a8748004e209b776fc1b2916d.png) [@Hangman2](https://community.unix.com/u/Hangman2)\
**Post date:** [November 1, 2010, 7:56pm UTC](https://community.unix.com/t/help-with-variable/276535/4 "2010-11-01T19:56:26Z")

</div>

Thanks vgersh99. Yes that's a good question.

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

---

<div class="post-metadata">

**Author:** ![Chubler\_XL](https://community.unix.com/user_avatar/community.unix.com/chubler_xl/32/2077_2.png) [@Chubler\_XL](https://community.unix.com/u/Chubler_XL)\
**Post date:** [November 1, 2010, 8:09pm UTC](https://community.unix.com/t/help-with-variable/276535/5 "2010-11-01T20:09:18Z")

</div>

If you have GNU date you can use:

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

```

Have you considered using the find command

```nohighlight
find . -mtime +60 -print

```

And to delete what you find:

```nohighlight
find . -mtime +60 -print | xargs rm

```

---

<div class="post-metadata">

**Author:** ![Hangman2](https://community.unix.com/letter_avatar/hangman2/32/5_5575768a8748004e209b776fc1b2916d.png) [@Hangman2](https://community.unix.com/u/Hangman2)\
**Post date:** [November 1, 2010, 8:23pm UTC](https://community.unix.com/t/help-with-variable/276535/6 "2010-11-01T20:23:16Z")

</div>

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.

😕

---

<div class="post-metadata">

**Author:** ![Corona688](https://community.unix.com/letter_avatar/corona688/32/5_5575768a8748004e209b776fc1b2916d.png) [@Corona688](https://community.unix.com/u/Corona688)\
**Post date:** [November 1, 2010, 8:43pm UTC](https://community.unix.com/t/help-with-variable/276535/7 "2010-11-01T20:43:59Z")

</div>

Why won't mtime work on those?

---

<div class="post-metadata">

**Author:** ![methyl](https://community.unix.com/letter_avatar/methyl/32/5_5575768a8748004e209b776fc1b2916d.png) [@methyl](https://community.unix.com/u/methyl)\
**Post date:** [November 1, 2010, 9:22pm UTC](https://community.unix.com/t/help-with-variable/276535/8 "2010-11-01T21:22:59Z")

</div>

> [@](#):
>
> If I am naming all my files with current date, so how do I get fine names older than 2 months.

Futher to Chubler\_XL

> [@](#):
>
> find . -mtime +60 -print

We can build on this and produce a testable construct:

```plaintext
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.
