Append current date to the filename via Cron?

Emil Devantie Brockdorff picture Emil Devantie Brockdorff · Feb 2, 2012 · Viewed 50k times · Source

I've created a Cron task at my webhost to daily backup my database and I would like it to append the current date to the filename.

My Cron job looks like this

mysqldump -u username -pPassword db_name > www/db_backup/db_backup+date%d%m%y.sql

But the file I get is this: db_backup+date no file extension or date.

I've also tried this command

mysqldump -u username -pPassword db_name > www/db_backup/db_backup_'date +%d%m%y'.sql 

but that doesn't even give an file output.

What is the right syntax for getting the date appended to my file??

Answer

ethanneff picture ethanneff · Aug 16, 2013
* * * * * echo "hello" > /tmp/helloFile_$(date +\%Y\%m\%d\%H\%M\%S).txt

You just need to escape the parentheses.

Other date formats: http://www.cyberciti.biz/faq/linux-unix-formatting-dates-for-display/