A shell script to empty older items from the trash

Jun 19, '07 07:30:00AM

Contributed by: JWiegley

After finally getting tired of remembering to "Empty Trash" at regular intervals, I decided to write a script (36KB download; source code on macosxhints) that automatically cleans files out of the trash after they've been there for seven days. It will even use sudo to clean out things that I don't have permission to erase -- if that option is available. There are many options to choose from, just use --help to see them all.

This script is designed to be very efficient to run, so I run it hourly. There is not (yet) any provision for maintaining the Trash at a particular size. I recommend adding a line to your cron file like this:

@hourly python /your/path/to/dirscan.py
This will clean things out after a week, and checks each hour for new items to remove. It pays attention only to top-level entries within .Trash, so any directories you put in the trash will be deleted automatically.

[robg adds: The code download link is from the author's site, and will be the best place to grab the latest version of the script. I mirrored the source as of today on macosxhints.com, just in case the parent site goes away at some point. Note that I haven't tested this one. Finally, for those interested in a size-limited trash can, this hint and the associated comments offer a few solutions.]

Comments (18)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20070613213146961