April 21, 2008

Auto-remove Old Items from The Trash in Mac OS X

Drew Stephens @ 1:37 pm — Tags:

Having a purgatory for files that are on their way to deletion, such as the trash can in Mac OS or the recycle bin in Windows, is a great idea, for even the most careful users occasionally delete something only to find that they later need it. Unfortunately, the two aforementioned implementations, as well as those in Gnome and KDE, only allow you to empty the trash all at once. Much more useful is to have a timeout where files that are sent to the trash are automatically removed after a period of time. I finally got around to implementing this myself in Mac OS by putting the following in my crontab:

0 5 * * * /usr/bin/find /Users//.Trash -mindepth 1 -maxdepth 1 -mtime +14 -exec rm -rf {} \;

Every day at 5 minutes after midnight any item more than 14 days old is delete from the trash can. To install it, read the linked article above or, if you know the command line, open a terminal, type crontab -e, paste the above (substituting your username) and save the file.

Related Posts:

2 Comments »

  1. Just googled exactly this problem, thanks for the command.

    But is there any reason why you didn’t use -atime instead of -mtime? I mean, access time would be much more useful, wouldn’t it? Modification time is not necessarily the time you moved it to trash right?

    [Reply]

    Comment by nils — November 11, 2008 @ 4:16 am

  2. Glad this helped!

    As for mtime v. atime, I actually hadn’t even thought about using atime but you’re right, it’s much more appropriate. Looking through the find(1) man page, ctime (last metadata change time) would work, too.

    [Reply]

    Comment by Drew Stephens — November 11, 2008 @ 12:32 pm

RSS feed for comments on this post. TrackBack URL

Leave a comment

Powered by WordPress