Keeping temp folders clean

I noticed that my home directory has been getting rather large and got me started on a cleaning process of all of the files within it. After some looking around, I discovered that one area has been building up temp files for quite some time all on its own.

I noticed that my home directory has been getting rather large and got me started on a cleaning process of all of the files within it. After some looking around, I discovered that one area has been building up temp files for quite some time all on its own. It's the "temp" folder used by Wine, which resides as : ~/.wine/drive_c/windows/temp/. I found almost 1 GB of temp files sitting in there, just hanging around for no apparent reason. Yes, even using Wine itself, you will continue to have Windows problems laying around as well. In this case, I looked in to using "tmpwatch" to clean out the Wine Windows temp folder. Well, in case you did not know, the "tmpwatch" cron job uses the "tmpwatch" utility to keep the /tmp folder clean on GNU/Linux systems. It looks at the age of files and deletes files over a certain age. So on I go to reading the "tmpwatch" man page to get more info on this utility.

I found that the tmpwatch utility is quite useful, and is mainly used for purging files based on age. It can purge based on modified time, accessed time, and other attributes. It can be set to delete only files (leaving folders in tact), or to delete both files and folders. Perfect for what I needed to accomplish.

So, I whipped up a script and placed it in the /etc/cron.hourly directory, so that Cron runs it every hour. The contents of the script are:

/usr/bin/tmpwatch --mtime 7d /home/myaccount/.wine/drive_c/windows/temp

Yep, that's it. This command will look for files over 7 days old (by modified time), and delete them as well as folders and subfolders. You could use the "--atime" parameter to specify to look by accessed time, instead. Check out the man page for tmpwatch, there are a lot of useful options. I can see many uses for this utility to purge files based on age, rather than trying to write a script to do the same thing which is possible but a lot more work.

The example above could also be set up using the "crontab" utility as well and scheduled directly to run with Cron, but I like to use the Cron hourly folder (/etc/cron.hourly) which is an easy way to view system jobs and modify them. Plus, jobs placed in the Cron hourly folder will run as root which is a plus to me, avoiding any possible permissions issues.

This could be set up in Windows too by using a script, or a scheduled task using the Disk Cleanup Utility. This page has more details on setting this up: http://support.microsoft.com/kb/253597 . In Windows, by default there is nothing scheduled to run that will examine and maintain temp folders; there is some user intervention required to set this up. Keeping temp folders clean is important, as it not only saves disk space but more importantly can alleviate problems with running applications when fragments of previous installations and runs are left hanging around.