Clear your /tmp

April 2nd, 2005

I 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.

6 Responses to 'Clear your /tmp'

  1. Gravatar 1 Carol Sanders Says:

    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

  2. Gravatar 2 PG Says:

    Try replacing “-exec rm {} ;” with “-delete”.

  3. Gravatar 3 2006 November 16 « A r j u n a Says:

    [...] Solusi yang gampang bagi gue ya tinggal ngikutin petunjuk di sini. Tinggal pakai perintah find. [...]

  4. Gravatar 4 Simon_c Says:

    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

  5. Gravatar 5 chronic_masturbator Says:

    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

  6. Gravatar 6 Adam Says:

    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.

Leave a Reply

XHTML: Permitted tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>