Cron : How to delete directory via cron

9,349

The manpage (man 5 crontab) says:

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 crontab file. 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.

So you have to replace every % in your date format with \%:

1 0 * * * rm -R /path/to/my/folder/$(date +'\%Y-\%m-\%d' -d "yesterday")
Share:
9,349

Related videos on Youtube

Aramayis  Mkrtchyan
Author by

Aramayis Mkrtchyan

Updated on September 18, 2022

Comments

  • Aramayis  Mkrtchyan
    Aramayis Mkrtchyan over 1 year

    I have allready installed cron and here it is

    1 0 * * * rm -R /path/to/my/folder/$(date +'%Y-%m-%d' -d "yesterday")

    I want to delete every day folder with "Yesterday name" LIKE "2014-12-14"

    Here is my ubuntu /var/log/syslog when I execute "grep CRON" command

    Dec 16 00:01:01 {SERVERNAME} CRON[21031]: (root) CMD (rm -R /path/to/my/folder/$(date +') Dec 16 00:01:01 {SERVERNAME} CRON[21030]: (CRON) info (No MTA installed, discarding output)