Confused by this `find` command in a Bash script?

The find command will delete any regular file in or below the /home/_backups/pokebrawl directory that is more than seven days old (or more precisely, not modified within the last week).

It should probably read

find "$DESDIR" -mtime +6 -type f -delete

or

find "${DESDIR:?Not set correctly}" -mtime +6 -type f -delete

as there is a perfectly good variable holding that directory name already. The second variation would cause an error if DESDIR for some reason was empty or unset.

I'm assuming this is a way of keeping only the last week's worth of backups. I would suggest using something like borgbackup or restic instead, as these would be able to keep backups for a lot longer without using much more space (even hourly backups for a year wouldn't take much space at all if the data wasn't changing much). They do deduplication of data and borgbackup additionally supports compression (both support remote backups and encryption too).

To run the script every 24 hours (at midnight), use a cron job.

First, issue the command crontab -e. This would open an editor with the current crontab for the active user (this may or may not be an empty file).

Then add

@daily /path/to/the/script.sh

(where path/to/the/script.sh is the pathname of the script).

Save and exit the editor.

The script would now be called at midnight, even night.

Would you want to run the script at, say, 13:30 every afternoon, add the following as the crontab entry instead:

30 13 * * * /path/to/the/script.sh

See the crontab manual on your system for details on how to write a crontab schedule (man 5 crontab).


find '/home/_backups/pokebrawl' -mtime +6 -type f -delete

This finds all the files in the directory /home/_backups/pokebrawl that haven't been modified in the last +6 days and deletes them. NOTE: +6 equates to 7 days.

Look in man find to see how these switches work:

$ man find
...
   -mtime n
          File's data was last modified n*24 hours ago.  See the comments 
          for -atime to understand how rounding affects the interpretation 
          of file modification times.

On Redhat distros you can simply put this script in the directory /etc/crondaily and it'll run every day. You can see that there are other scripts there already which you can use as examples:

$ sudo ls -l /etc/cron.daily/
total 12
-rwx------. 1 root root 219 Aug  1  2017 logrotate
-rwxr-xr-x. 1 root root 618 Mar 17  2014 man-db.cron
-rwx------. 1 root root 208 Nov  4  2016 mlocate