previous date comman in unix (urgent)

October 3, 2011 at 14:43:08
Specs: N/A
i want when i run my script in UNIX then it should autmoatically get previous day date and append it to a file name monitorBatch.log.

example: monitorBatch.log.2011-10-02

as i am running script on 2011-10-03 then it should append as i mentioned above:


Please help me..its urgent

See More: previous date comman in unix (urgent)

October 3, 2011 at 15:45:03
Manipulating dates in vintage unix without benefit of a scripting languarge is difficult. You can manipulate the timezone variable, TZ, to get yesterday's date and then create and write to a file:


YESTERDAY=$(TZ=aaa24 date +%Y-%m-%d)

echo "some text" > monitorBatch.log.$YESTERDAY

This TZ thing is not portable, but this works on my Solaris 9 box. Your mileage may vary.

Unix date arithmetic has been discussed a lot on this forum. Here is one of many example links in this forum that discusses TZ, the shift_date shell script, and a suite of shell scripts that does date arithmetic:

Report •

October 3, 2011 at 16:35:45
Hi nails...

What does TZ=aaa24 means??

Report •

October 3, 2011 at 17:19:14
AAA24 is an offset from GMT for the TZ variable. Here is a description:

Report •

Related Solutions

July 2, 2012 at 23:28:58

It' not working as when I added the same to a script wherin it runs at 2:00 AM every morning and needs to take in count of the previous days file in a folder.
The date was caluclated for day before yesterday and the count also for the same.

My system timezone is of UAE
Tue Jul 3 10:24:17 GST 2012

Thanks and Regards,
Gowri Shankar

Report •

July 3, 2012 at 06:38:38
As I said, TZ is not portable. Based on your description, I don't know what the problem is.

If I were you, I'd consider using the shift_date shell script. I linked to it in my first reply.

Report •

Ask Question