Rain WikiWindows · Linux · Android

/tmp and temporary files

System folderAll Linux distributionsSafe to delete: Partly

/tmp is the directory for temporary files that programs need only for a short time. /var/tmp is for temporary files that should survive a reboot. On many distributions /tmp is cleared at boot or is a tmpfs in memory, and old files in both are removed by systemd-tmpfiles.

1/tmp versus /var/tmp

The Filesystem Hierarchy Standard says programs must not assume files in /tmp are kept between program runs, while /var/tmp is preserved across reboots. Both are writable by all users and have the sticky bit, so users can delete only their own files.

2Automatic cleaning

systemd-tmpfiles reads rules from tmpfiles.d directories, for example /usr/lib/tmpfiles.d/tmp.conf, which commonly sets an age of 10 days for /tmp and 30 days for /var/tmp. The cleaning runs daily through systemd-tmpfiles-clean.timer.

3tmpfs

When /tmp is mounted as tmpfs, its files live in RAM and swap and disappear at every reboot. findmnt /tmp shows whether it is a tmpfs.

How to check and clean /tmp

  1. Check its size: sudo du -sh /tmp /var/tmp
  2. See whether it is in memory: findmnt /tmp
  3. Run the configured cleanup now: sudo systemd-tmpfiles --clean
  4. List old files before deleting anything by hand: sudo find /tmp -type f -atime +10

Cautions

  • Deleting files in /tmp that a running program still uses can make it fail; prefer the systemd-tmpfiles cleanup or a reboot.
  • Do not delete sockets and lock files of running services.

Sources

  1. tmpfiles.d(5) manual page man7.org
  2. Filesystem Hierarchy Standard 3.0: /tmp refspecs.linuxfoundation.org

Note: RainServer offers monitoring and safe cleanup for Linux servers; it shows what will change and asks first.

Last reviewed: