News, info, and opinion by Mac users, for Mac users.

August 6, 2006

tips

A self emptying downloads folder

Posted Aug. 6, ’06, 4:51 PM PT by Derik DeLong
Category | Tips

Trash Can In Macworld’s forums, a member asked how to create a downloads folder that would delete its contents given time. Windows has a temporary folder that will do just that. Rob “Tip Wizard” Griffiths suggested using /tmp, a special directory that’s emptied on restarts. I wasn’t satisfied with that. There’s clutter in there and given how long one might go without a restart (I regularly go weeks with my Powerbook), it just wasn’t optimal, in my opinion.

I decided to go a different route that requires no additional software. It’s quite a bit more involved, but worth it if you’d like to have a download folder. I’ll steal liberally from my response.

Here is what we do. We use the find command to select all the files not accessed within x minutes of the find. For this example, we’ll use an hour (60 minutes).

find /download/directory/path -mindepth 1 -amin +60

The -mindepth argument tells the command not to select the directory being searched (we don’t want to delete it by accident). The -amin says if the access time was 60 minutes or more ago, then it’s a match. We want to delete all the matching files and directories, so we need to add another argument.

find /download/directory/path -mindepth 1 -amin +60 -delete

The last argument does exactly what it says, deletes all matches files and directories. Now, most likely, this directory is going to be in your own home directory, so we can just use the ~ alias to make the path shorter. Let’s pretend you name your folder Download.

find ~/Download -mindepth 1 -amin +60 -delete

Finally, we need a way to run this on a regular basis, so you don’t have to. For that, we use cron. The easy way to add entries to it is to use Cronnix. The stuff you want to put in is basically the same, but you don’t have to use vi to edit the crontab (which is hard to do for the uninitiated). If you want to use vi, use the *crontab -e” command. What we want to put in is:

*/30 * * * * find ~/Download -mindepth 1 -amin +60 -delete

That basically tells cron to run that command every 30 minutes.

That should do it. It’s a little bit of setup, but it’ll work great. Keep in mind that you can change access time (in case you want it to be 2 hours before it deletes, you can change it from 60 to 120). Also, the frequency that this happens can be changed. For example, say you wanted it done every 10 minutes, then you’d change */30 to */10.


Leave a comment

 




Visit other IDG sites: