Clear your /tmp
April 2nd, 2005I would hazard a guess that 95% of Linux users know what their /tmp directory is for. For those that don’t, your chosen Linux distribution probably already uses tmpwatch in order to achieve the following.
As an avid Slackware user, I needed a quick way of checking the contents of my /tmp and /var/tmp and pruning files which haven’t been used in a given period of time. The following few lines of bash code achieve just that:
#!/bin/bash
find /tmp -type f -atime +5 -exec rm {} \\;
find /var/tmp -type f -atime +30 -exec rm {} \\;
The second line will clear all files located in /tmp that haven’t been accessed in five days or more, while the third line clears any files in /var/tmp that haven’t been accessed in thirty days or more.
Just throw this in /etc/cron.daily and you will have yourself a clean and up-to-date /tmp directory.
This entry was posted on Saturday, April 2nd, 2005 at 7:09 pm and is filed under Linux. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.
September 28th, 2006 at 5:17 am
I attempted to use this script but received the following error: find: missing argument to `-exec’. Can you suggest what might be the issue? I read the manpage for exec and tried adding -cl and -a.
Thanks
October 19th, 2006 at 5:49 pm
Try replacing “-exec rm {} ;” with “-delete”.
November 16th, 2006 at 7:25 pm
[...] Solusi yang gampang bagi gue ya tinggal ngikutin petunjuk di sini. Tinggal pakai perintah find. [...]
March 31st, 2007 at 1:23 am
The major problem with this script on a system that has any kind of access from the outside world on it, is what happens if some evil user puts a symlink into /tmp say ../usr/bin
May 3rd, 2007 at 4:19 pm
actually change it to
#!/bin/bash
find /tmp -type f -atime +5 -exec rm {} \;
find /var/tmp -type f -atime +30 -exec rm {} \;
and it will work fine
August 22nd, 2007 at 6:48 pm
I just noticed that wordpress for some reason is filtering my backslashes. Turns out the backslash is interpretted as an escape, so I had to escape the escape (ie. \\;) and everything looks good again.
Sorry to the guys that couldn’t get this thing to work.