/tmp and temporary files
/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
- Check its size:
sudo du -sh /tmp /var/tmp - See whether it is in memory:
findmnt /tmp - Run the configured cleanup now:
sudo systemd-tmpfiles --clean - List old files before deleting anything by hand:
sudo find /tmp -type f -atime +10
Cautions
- Deleting files in
/tmpthat 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.
Related articles
- Finding large and old files with findHow to use find to list big or old files on Linux, preview them safely and delete only what you checked.
- Disk usage with du and ncduHow to find what fills a Linux disk with du and the interactive ncdu, with options that stay on one file system.
- File permissions: chmod and chownHow Linux read, write and execute permissions work, what 755 and 644 mean, and how to change owner and mode safely.
Sources
- tmpfiles.d(5) manual page man7.org
- 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: