Linux: Cron command for deleting old files (older than n days)

PHOTO EMBED

Sun Aug 28 2022 02:08:03 GMT+0000 (UTC)

Saved by @marcopinero #bash

$> sudo crontab -e

#then add a line like this:

* * * * * find /path/to/files/ -type f -mtime +<n> -exec rm -rf {} \;

#Ex:
#Delete "*.txt" files older than 1 day from /tmp folder every day at 2:00am:

0 2 * * * find /tmp/* -type f -mtime +1 -exec rm {} \;       #files
0 2 * * * find /tmp/* -type d -mtime +1 -exec rm -rf {} \;   #folders
content_copyCOPY

Add lines like these to crontab in order to delete older-than-n-days files from specific folder.