I'd like to create directory with a maximum size of, let's say 500MB. The directory may contain any number and depth of subdirectories.
When the maximum size limit of the directory is surpassed, I would like to purge the directory of the last accessed file(s) until it is back under the size limit. Ideally, empty directories would be cleaned out during this purge operation.
This doesn't need to happen in real time -- i.e., it's okay if the directory goes over the maximum, just as long as it's regularly purged.
I've explored a few options. I could mount the folder as a separate file system and run
quota on that, but that doesn't help with intelligently purging of old files. I looked at (mis)using
logrotate for this purpose, but it doesn't handle limitless recursion very well, nor does it have a facility for purging by starting from the oldest file.
Without a better suggestion, my plan is to brush up on my bash skills and write a script that checks the directory size via
du -s, and if it finds the directory to be larger than the maximum, then it recursively finds all the files in the directory, sorts them by access date oldest first, then deletes them one by one, checking the directory size and exiting when the directory is back under the target. This script could be run as a cron job.
I'm just wondering if I'm overlooking an existing tool or strategy that handles this more easily.
(If you're curious why I'm asking, I'm using s3fs to mount Amazon S3 buckets on a local file system, and I'm investigating using the use_cache option to maintain a local cache. Since S3 buckets can get quite large, I need a way to intelligently manage this cache.)
tmpreaper is another tool I just found out about that's pretty close to doing the job. It only seems able to clean out folders based on access time (not a size limit), but in all other respects it appears to be a good choice.