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.

How to Install and use Tmpreaper

Instaling it is very easy:

sudo apt install tmpreaper

Note that you might need to run that command as sudo to be able to delete files created by other users.

  • To be sure that you are not deleting important file, make sure you use the --test option to do a dry run:
tmpreaper --test 2d /tmp

if you sure to delete files can do this.

tmpreaper 2d /tmp


, -h, --help
              Print  a  brief  version,  copyright, and usage statement on stderr, then exit with
              error status 1.

       -t, --test
              Don't actually remove any files, but go through the motions, checking  through  the
              directory, then pretend to remove the eligible files.

       -v, --verbose
              Print  a  verbose  display. Two levels of verbosity are available---use this option
              twice to get the most verbose output.  The --test option automaticly sets --verbose
              Higher numbers mean more output (max. is 3).
              To  force  normal  verbosity  after --test, use "--verbose=0".  This will generally
              only show  error  messages.  Use  "--test  --verbose=0  --showdeleted"  to  give  a
              shellscript-like  list  of actions that would have been done (see the --showdeleted
              description below).

              Show what files and directories are deleted. The output is in  the  form  of  shell
              commands, i.e. "rm /dir/dir2/file" and "rmdir /dir/dir2".
              When  used  together with --test, this option will still cause the "shell commands"
              to be printed, although nothing is really done. Note that this may show  more  than
              without  --test,  as  problems  removing the file won't be detected (e.g. immutable

       -f, --force
              Remove files even if EUID doesn't have write access (akin  to  rm  -f).   Normally,
              files owned by the current EUID, with no write bit set are not removed.

              Delay  execution  at  the  start for a random time, up to x seconds; if no value is
              specified, the default maximum time to delay is 256 seconds.   This  is  an  option
              useful  in  cron  scripts to make the execution of tmpreaper less predictable, thus
              making things a little harder for those who  would  attempt  to  use  tmpreaper  to
              thwart security.

       -T x, --runtime=x
              Execution  of  tmpreaper  will  aborted after x seconds; this is to prevent attacks
              that create many, many files.  By default the timeout is  set  to  55  seconds.   A
              value of 0 will disable this feature, which is not advised as this feature prevents
              possible race-conditions between different instances of tmpreaper.

       -m, --mtime
              Base the decision of whether to remove the file on its mtime, rather  than  on  its

       -M, --mtime-dir
              Base  the  decision of whether to remove the directory on its mtime, rather than on
              its atime.

       -c, --ctime
              Base the decision of whether to remove the file on its ctime, in  addition  to  its
              atime.  Only applicable if the --mtime options is not given!

       -s, --symlinks
              Remove symlinks too, not just regular files and directories.

       -a, --all
              Remove all file types, not just regular files, symlinks, and directories.

       --protect ''
              Protect the files that match the  from deletion.  This option may be
              used more than once.  It has no one letter abbreviation, you  must  spell  out  the
              full word "protect".

              If  you do not enclose the  in single quotes, the shell will perform
              the expansion before tmpreaper reads its argument  array.   The  program  does  not
              support that syntax, so you must use single quotes around the glob pattern.

              tmpreaper  will chdir(2) into each of the directories you've specified for cleanup,
              and check for files matching the  there.  It then builds a  list  of
              them, and uses that to protect them from removal.  For example:

              tmpreaper --test --verbose --protect \
               '.X*-{lock,unix,unix/*}' --protect '.ICE-{unix{/*,}}' \
               5d /tmp  # 5 day grace period
0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *