Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Is there any possibility to pass current date/time to command in my cron job? E.g. I want to make sql dump every night and name the dump file like 'dump-yyyy-mm-dd.sql'.

share|improve this question
up vote 4 down vote accepted

That would be something along the lines of:

mysqldump database > database-$(date +%F).sql

The $() will embed the output of date +%F, which is YYYY-MM-DD into the command line.

share|improve this answer
Thank you. Ideal solution – user874560 Aug 2 '11 at 12:19
You should be careful to escape %s with backslashes in your format string if you're using a POSIX compliant cron, otherwise everything after the first % will be treated as standard input for the command. Check man 5 crontab for details on this surprising yet fun feature. – mark Aug 2 '11 at 12:27

Sgaduuw's answer does work (and I've upvoted it), but I usually encourage people to write scripts and call those from cron, rather than try and do too much in the cron command directly. Firstly, it allows you to easily test / enhance your script without having to mess with cron configuration, and secondly it allows you to be in better control of the environment in which your commands execute.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.