Home > Linux > clean linux tmp

clean linux tmp

November 2nd, 2010

Doing some security check on my laptop I found that some files are kept on disk even I do not want to – /tmp directory.

Temp is kind of places in system that should not be kept on encrypted partition, so I have decided to use tmpfs.

First define an fstab entry to keep /tmp on tmpfs:

tmpfs                /tmp                 tmpfs      rw,mode=1777          0 0

Clean all data and mount it:

rm -rf /tmp/*
mount /tmp

Now your data is kept only till yours computer is restarted.

But wait it keeps growing, I restart my laptop once few months, what then ?

No problem a handy script might be useful, save it as /root/bin/clean_tmp.sh

#!/bin/bash

diff <( lsof +D /tmp/ | awk '{print $9}' | grep -v '^NAME$' )  / {print $2}' | grep -ve '\.X' | xargs rm -fv

What it does ? – remove all files older then 60 minutes and not used by any application.

Now connect script in root cron:

5 * * * * /root/bin/clean_tmp.sh

Now your temp is secure and clean.

You will get list of removed files on your local mail account, to prevent this email remove just “v” from “xargs rm -fv” from the script.

Categories: Linux Tags: , , , , , , ,
Comments are closed.
Get Adobe Flash player