March 13, 2014    


 

 


There are a number of temporary (tmp) directories on any Linux system, /tmp not being the only one.

tmp directories are good places to store things temporarily and many programs do so. Every time one logs in to a GUI session, the X Window System stores data about that session in /tmp. I typically store downloaded files there or tarballs and zip files I need to unpack.

Over time these files can grow in number and your temporary directories can fill up if you or the applications do not clean them up properly.

There is a program called tmpwatch that you can run manually, from the command line that will clean up old files. But it is best when run as a cron job.

On CentOS and RHEL systems, a tmpwatch script is located in /etc/cron.daily and will run once per day. The tmpwatch script calls the tmpwatch program to delete files of varying age in several temporary directories.

The /etc/cron.daily/tmpwatch script used by CentOS and RHEL is shown below.

#! /bin/sh
flags=-umc
/usr/sbin/tmpwatch "$flags" -x /tmp/.X11-unix -x /tmp/.XIM-unix \
        -x /tmp/.font-unix -x /tmp/.ICE-unix -x /tmp/.Test-unix \
        -X '/tmp/hsperfdata_*' 10d /tmp
/usr/sbin/tmpwatch "$flags" 30d /var/tmp
for d in /var/{cache/man,catman}/{cat?,X11R6/cat?,local/cat?}; do
    if [ -d "$d" ]; then
        /usr/sbin/tmpwatch "$flags" -f 30d "$d"
    fi
done

The preceding BASH script removes files from several temporary directories, including /tmp, that have not been accesses or modified in the previous ten (10) days. It also removes some cache files in a number of subdirectories of /var that have not been accessed or modified in the previous thirty (30) days.

Fedora Linux does install the tmpwatch program but does not install a script in cron.daily so the tmpwatch program does not get run automatically. If you wish to have the tmpwatch program run once each day, you can copy the BASH script from above and save it as tmpwatch in /etc/cron.daily and make it executable with root.root ownership.