Help with date in file name

Hi All,

I have a cron job running every night and it sends the output to a log file. The entry in cron is this:

logname_`date +"%y%m%d"`.log 2>&1

However, when I check the log, the name is as this:

logname_Fri Jan 15 14:54:00 EST 2010

Can someone tell me why the log name is being created like that and how can I fix it so that it looks like:

logname_20100115.log

Thanks.

first thing
change %y to %Y

also chk the path of date cmd

try putting just an echo in cron to check if it works fine

Eg:-

echo logname_`date +"%Y%m%d"`.log


or which date

and echo logname_`/bin/date +"%Y%m%d"`.log

HTH,
PL

Thanks. I will try what you suggested.

The crazy thing is, if I run it manually, it works as I expect. But in the cron, nada. So I will put the full path of the "date" command in the cron and see how that goes.

Thanks again.

On another note, I did not know that %y would give 10 and %Y would give 2010. Learned something new today. Always a plus! :smiley:

The problem is an old favourite with cron.
The "%" character has special meaning to cron and gets replaced with a newline. See "man crontab". This truncated your "date" command and gave you the default date format.

You can escape the "%" sign but it is much more readable to put the cron line into a script and call the script from cron.
In the script put your favoured shell in the shebang line ... for example:

#!/bin/ksh

methyl is right !:slight_smile:

Ok. Thanks. Will give that a try.