Gracefully deleting files older than 30 days
I like to use tmpwatch
for these things, this is for the last time the file was modifiyed. It's simple and works well in many cases:
tmpwatch -m 720 /path/to/cache
For Ubuntu, check tmpreaper
instead.
If you want to check the last time the file was accessed than you use the following:
tmpwatch -a 720 /path/to/cache
You are not able to use tmpwatch -a on file systems mounted with noatime. you can still use -m
You could avoid the spawning of a new process for each file by using
find cache* -mtime +30 -delete
Try running the above with nice:
nice -n 39 find cache* -mtime +30 -exec rm -f {} ';'
That way the huge load will only appear if nothing else needs to run, otherwise the other processes will take precedence (if their niceness is lower than 19 i.e. the maximum).
Note that the argument to the -n option is added to the default niceness which varies between -20 and 19. I used 39 so that it will be very nice regardless of what original niceness there was.