crontab/date: timestamp in log file name

A cronjob shall run a script and write it’s output to a new logfile every day. Easy, right?

0 2 * * * echo test > test.`date +%Y-%m-%d`

This will create a file called “test.“, that will be overwritten every day …and here is why (from man crontab):

“Percent-signs (%) in the command, unless escaped with backslash (\), will be changed into newline characters, and all data after the first % will be sent to the command as standard input.”

This works…

0 2 * * * echo test > test.`date +\%Y-\%m-\%d`
  • email
  • PDF
  • Google Bookmarks
  • Yahoo! Bookmarks
  • del.icio.us
  • Twitter
  • Reddit
  • Digg
  • Ping.fm
  • Slashdot
  • Facebook
  • MySpace
  • Technorati
  • NewsVine
  • Tumblr
  • StumbleUpon

Leave a Reply