The files and directories in the tmp directory are cleaned only during a reboot. This can be problematic if you have applications that write a lot to the tmp directory and you do not want to reboot your Linux system. A good solution for periodic cleanup of the tmp directory is the tmpreaper tool.
$ sudo apt install tmpreaper
On installation, tmpreader adds a cron job that runs once a day. This can be seen in
/etc/cron.daily/tmpreaper. It calls the tmpreaper program with the options you set in
The tmpreaper tool will work on any directory passed to it, including the root directory. Since deleting the root directory is catastrophic, tmpreaper shows a warning all the time. You can disable this warning by setting
When tmpreaper is run once a day by cron, it uses the
TMPTIME value set for the tmp directory init scripts as described here to decide which files and directories to delete. For example, if
TMPTIME=1, then tmpreaper will delete everything in tmp directory that is older than a day.
If you want to apply tmpreaper on directories other than
/tmp, then add them to the
TMPREAPER_DIRS value in
You can call the tmpreaper program directly at the shell to reap your own directories. For example, to reap all files and directories older than 2 days from the tmp directory:
$ tmpreaper 2d /tmp
Note that you might need to run that command as sudo to be able to delete files created by other users.
--testoption to do a dry run:
$ tmpreaper --test 2d /tmp
Tried with: tmpreaper 1.6.13 and Ubuntu 16.04