Gentoo Wiki


This article is part of the HOWTO series.
Installation Kernel & Hardware Networks Portage Software System X Server Gaming Non-x86 Emulators Misc



Sometimes programs don't clean temporary files in /tmp. Cleaning it manually is a hard task, but there are some utilities to do that for you.


Add this line, to have /tmp wiped during bootup:

File: /etc/conf.d/bootmisc


Tmpwatch is a simple program which removes files which haven't been accessed for a period of time.

# emerge tmpwatch

Uncomment all examples from /etc/cron.daily/tmpwatch. The file should look like this:

File: /etc/cron.daily/tmpwatch
PORTAGE_TMPDIR="$(portageq envvar PORTAGE_TMPDIR)/portage"
DISTDIR="$(portageq distdir)"

if [[ -d /tmp ]]; then
  ${TMPWATCH} --atime 168 /tmp

if [[ -d ${PORTAGE_TMPDIR:-/var/tmp/portage} ]]; then
  ${TMPWATCH} --mtime --all 336 ${PORTAGE_TMPDIR:-/var/tmp/portage}

if [[ -d ${DISTDIR:-/usr/portage/distfiles} ]]; then
  ${TMPWATCH} --atime --fuser 4320 ${DISTDIR:-/usr/portage/distfiles}

The script is run everyday by cron and performs the following tasks:

Warning: On filesystems mounted with the noatime flag, access times are usually equal to the creation time of the file. Make sure you add --ctime flags if this is the case with your tmp directories.


This Debian utility cleans temporary files depending on atime, mtime or ctime (you can configure that). First emerge it: emerge tmpreaper.

Cron job

The easiest method is to use bundled script:

# bzcat /usr/share/doc/tmpreaper-1.6.5/cron.daily.bz2 > /etc/cron.daily/tmpreaper.cron
# chmod +x /etc/cron.daily/tmpreaper.cron

Configuration (optional)

tmpreaper's configuration file is /etc/tmpreaper.conf. In this file you setup env variables, which then are sourced by the cron script. If you don't set some variable, tmpreaper will use default value. You don't even have to create /etc/tmpreaper.conf at all, as defaults are suitable for most systems.

  • TMPREAPER_TIME - max. age of files before they're removed.
  • TMPREAPER_PROTECT_EXTRA - extra patterns (like in bash) that you may want to protect
  • TMPREAPER_DIRS - list of directories to clean up, separated by space
  • TMPREAPER_ADDITIONALOPTIONS - additional options passed to tmpreaper

Alternative: Using temporary disk space

Compile your kernel with tmpfs (temporary file system) enabled and mount your /tmp directory using it. The useful bit here is that nothing will be written to your hard drive on this mount point as it will act like a RAM disk (however nothing will be saved either). The advantage of tmpfs over the more traditional ramfs is that it lives in the kernel internal cache and grows and shrinks to accommodate the files placed there.

If you don't specify a maximum size, it will default to a ceiling limit of half your available memory. An example /etc/fstab with 100MB temporary ram file mounted on /tmp would look like:

tmpfs	/tmp	tmpfs	size=100m,mode=1777	0 0

The use of the /tmp cleaning tools above is still encouraged since they'll clean up tmpfs memory usage during a long uptime, as well as make the space reusable after it has been filled - this can be critical on servers. Using atime (last file access time) for cleanup is also a good idea since atime updates don't have any overhead with tmpfs.

For more information read: /usr/src/linux/Documentation/filesystems/tmpfs.txt

Last modified: Fri, 05 Sep 2008 23:23:00 +0000 Hits: 27,016