I have the following command in an init-script on my machine.
This removes both directories and files older than 1 day from the /tmp directory.
There's one problem with this code, namely that when it removes a file or subdirectory from a directory, the mtime of the directory would change, so it doesn't get deleted anymore even if it was old before this. So, such directories would disappear only after multiple iterations of this code. This is no problem, as deep directory structures aren't really common in /tmp, don't take up much space anyway, and it's not a great problem that these are deleted only after multiple reboots. (What causes more problems are in fact temporary files that are not in /tmp but somewhere else.)
perl -we 'use File::Find; finddepth(sub { 1 < -M() and -d() ? rmdir :
+unlink; }, "/tmp")'
The usual disclaimers apply: use only for your own responsability.
Update: see also Automatically Deleting Files Periodically. |