I was not able to find the answer for this myself.

As cron uses /bin/sh, im getting Syntax error's. I have a cron job, which uses the following command in it

2,17,32,47 * * * * tar -czvf /opt/SOMEFILES /var/lib/$(date +"%Y/%m/%d")


2,17,32,47 * * * * tar -czvf /opt/$(date +"%Y/%m/%d")/somefiles /var/lib/$(date +"%Y/%m/%d")

Basically, this is just an example, the real cron job is a little different. Tried to give some type of an example.

$(date +"%Y/%m/%d") This is the part i am not sure how to use in Cron.

How could i use it in cron? the curdate could even be a folder where it goes.

  • @muru as i mentioned in my question, i had no idea how to google this perfectly. – TheSebM8 Apr 6 '18 at 7:57
  • So I googled it for you. – muru Apr 6 '18 at 7:59
  • @muru well thankyou, but i forgot to add another way i have to use my cron. As a folder indicator. As the link you posted does not say. – TheSebM8 Apr 6 '18 at 8:01
  • 1
    I don't see how it makes any difference what the output of date is used as. – muru Apr 6 '18 at 8:02
  • Aight, i took a closer look, tested and noticed i had my own type error. Thanks for this google search. Gave me what i needed. – TheSebM8 Apr 6 '18 at 8:08

I don't have the exact errors you are getting, but my guess would be this is an escaped characters issue (you are using forward slashes rather than back slashes for date)

The "sixth" field (the rest of the line) specifies the command to be run. The entire command portion of the line, up to a newline or % character, will be executed by /bin/sh or by the shell specified in the SHELL variable of the cronfile. Percent-signs (%) in the command, unless escaped with backslash (), will be changed into newline charac- ters, and all data after the first % will be sent to the command as standard input.

Try more like this format

`date +\%Y\%m\%d\`
  • The thing is, i also need to use this as a folder field. I have a program that generates data and makes folders as 2018/04/05 and sometimes i have to move inside the curdate folder, so i cant use \ – TheSebM8 Apr 6 '18 at 7:56
  • Nvm, thanks. this also helped. Just my bad understanding was the reason i failed to get it working at first. – TheSebM8 Apr 6 '18 at 8:09

Solution 1: Put backslash \ with date

2,17,32,47 * * * * tar -czvf /opt/SOMEFILES >> /var/lib/`date +\%Y\%m\%d\`

Solution 2:

You can also put your commands into a shell file and then execute the shell file with cron.


 tar -czvf /opt/SOMEFILES >> /var/lib/`date +\%Y\%m\%d\` 


2,17,32,47 * * * * jobs.sh
  • I'v edited my comment as i forgot to mention that i have to use it also with folder indicator. – TheSebM8 Apr 6 '18 at 7:59
  • A bare % in the command field is interpreted by cron as separatring the command and input, so this is incorrect. – muru Apr 6 '18 at 8:00
  • The jobs.sh file now uses the escaping needed for cron, which is unnecessary for sh, and forgets command substitution. – muru Apr 6 '18 at 8:22
  • @muru now I edited my answer. – Dipankar Nalui Apr 6 '18 at 8:25

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