I am trying to use cron on my debian11 VM to create a folder with todays date on system reboot if it does not already exist, this is the line I have used.
@reboot /usr/bin/mkdir -p /home/bin/data/"$(date '+%Y%m%d')"
This work from the terminal, and after looking online I understand the issue is that cron is not ran like a shell, and that I could solve this problem by packaging this line inside a .sh file. I'm ok with this solution but in the interest of simplicity and learning, how would I rewrite this line so it would run from cron successfully?
Escape percent-signs (%) in your command with backslash (\) character:
@reboot /usr/bin/mkdir -p /home/bin/data/"$(date '+\%Y\%m\%d')"
Refer to crontab(5):
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 characters, and all data after the first % will be sent to the command as standard input.