TMPREAPER(8)

πŸ“¦
version 1.6.18ubuntu1 Β· 2 versions
  • 1.6.18ubuntu1 Β· debian bookworm, debian trixie, ubuntu noble, ubuntu resolute, ubuntu stonking (latest)
  • 1.6.14 Β· ubuntu jammy
, in debian bookworm, debian trixie, ubuntu noble, ubuntu resolute, ubuntu stonking

πŸ’– TMPREAPER(8) πŸ’–

okay bestie, gather round, because we're about to talk about tmpreaper β€” a lil tool from the System Manager's Manual that removes files which haven't been accessed for a period of time. no cap, that's literally the whole NAME section. she's a deleter. she's an icon. let's go 🧹✨


✨ SYNOPSIS ✨

tmpreaper [-htvfmMsaT] [--help] [--test] [--verbose] [--force]
[--delay=x] [--runtime=x] [--showdeleted] [--ctime] [--mtime] [--mtime-
dir] [--symlinks] [--all] [[--protect '<shell_pattern>']...]
<time_spec> <dirs>...

look at her. look at that flag salad. she ate and left no crumbs πŸ’…


🌸 DESCRIPTION 🌸

so here's the vibe: tmpreaper recursively searches for and removes files and empty directories which haven't been accessed for a given number of seconds. normally you'd point her at directories used for temporary holding space, like /tmp. but please, please read the WARNINGS section of this manual first, i am begging you πŸ™

when changing directories, tmpreaper is very sensitive to possible race condition security exploits[1], and will exit with an error if one is detected. she does not follow symbolic links in the directories she's cleaning (even if a symbolic link is given as her argument), she never performs chdir(".."), she will not switch file systems, and she only removes empty directories and regular files. unless your machine is one with lots of relatively untrusted users, like an ISP or a school, you don't actually need this program β€” find ... -exec rm ... works just as well when you don't have to be concerned about people trying to exploit the race condition on you. fr fr, sometimes the girlies are just extra.

tmpreaper will stop herself after almost one minute with an appropriate warning message, because attempts to keep her running long enough to run in parallel with another instance of herself may also lead to possible vulnerabilities. normally she won't need that amount of time. if your system is so slow that she does, try to configure things so that this doesn't happen. as a last resort, the --runtime=x option can be used to set the number of seconds after which the timeout occurs; the default setting is 55 seconds. ⏰

tmpreaper dates files by their atime, not their mtime, unless you select the --mtime option. if files aren't being removed when ls -l implies they should be, use stat(1) or ls --time=access to examine the file's atime and see if that helps to explain the problem. it's giving detective work πŸ”

additionally, tmpreaper can be instructed to also check the ctime (inode change time, which is updated e.g. when the file is created or permissions are changed). this is primarily useful when tmpreaper is used to clean up directories that are accessible as a Samba share; DOS (and Windows) PCs preserve the mtime and the atime when copying to a new file, so that it appears that the newly created file is old. tmpreaper will remove such files if the atime is beyond the removal time, even though they were just created. this is avoided by using the --ctime option. πŸͺŸπŸ’€

as testing the contents of subdirectories will update those directories' atime, empty directories won't be removed. to circumvent this problem you can use the --mtime-dir option, which will switch on mtime checking for directories only. using --mtime-dir in addition to --mtime doesn't do anything useful. (redundant queen πŸŽ€)

the <time_spec> parameter defines the age threshold for removing files. if the file has not been accessed for <time_spec>, it becomes eligible for removal. the <time_spec> should be a number, defaulting to hours, optionally suffixed by one character: d for days, h for hours, m for minutes, or s for seconds. following the time option, one or more directories must be given for tmpreaper to clean up.

on linux ext2/ext3/ext4 filesystems, no errors will be given when trying to remove files marked as immutable. a common situation for this was the ext3 .journal file. however, there may of course be other files marked as such by the system administrator. 🫣


πŸ‘‘ OPTIONS πŸ‘‘

  • <noargs>, -h, --help β€” prints a brief version, copyright, and usage statement on stderr, then exits with error status 1. yes, even help is an error. dramatic much? πŸ’€
  • -t, --test β€” don't actually remove any files, just go through the motions: check through the directory, then pretend to remove the eligible files. fake it till you make it 🫢
  • -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 once. 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). πŸ—£οΈ
  • --showdeleted β€” 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 files). πŸ’…
  • -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. forceful. slay. πŸ”₯
  • --delay=x β€” 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. sneaky random girlie 🌈
  • -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. don't turn it off bestie 😭
  • -m, --mtime β€” base the decision of whether to remove the file on its mtime, rather than on its atime. ⏱️
  • -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! (that exclamation mark is load-bearing, i don't make the rules)
  • -s, --symlinks β€” remove symlinks too, not just regular files and directories. πŸ”—
  • -a, --all β€” remove all file types, not just regular files, symlinks, and directories. everything. the whole buffet. πŸ“
  • --protect '<shell_pattern>' β€” protect the files that match the <shell_pattern> 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 <shell_pattern> 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 <shell_pattern> 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

πŸ’‘ TIPS πŸ’‘

as long as there are files present inside a subdirectory, it won't get removed. you can use a non-writable, self-owned file, perhaps named .tmpreaper, or, if you are su, a file that has the ext2fs immutable attribute set, to keep a subdirectory from being deleted. of course, you could just as easily use use the --protect option to obtain the same result. (yes, "use use" is in the original, i'm keeping it, character for character, no cap πŸŽ€)

because the command line argument processing is implemented with GNU getopt_long(3)[2], you may order the arguments thusly, if it pleases you:

tmpreaper --test --verbose 5h \
 --protect './tmp/{blah?,dir{/blah4,}}' ./tmp \
 --protect '/tmp/.X*' /tmp

... note that if you use --all or --symlinks, it will have global effect. if you only want it turned on for one directory, you must use separate commands. no shortcuts bestie πŸ¦‹


⚠️ WARNINGS ⚠️

please do not ever run tmpreaper on /!!! there are no safeguards against this built into the program, because that would make it difficult to use in a chrooted environment. like. i cannot stress this enough. do not. πŸ™ˆ

πŸ”— SEE ALSO πŸ”—

chattr(1) chdir(2) chroot(8) cron(1) getopt_long(3) ls(1) lsattr(1) rm(1) stat(1)

[1] http://seclists.org/lists/bugtraq/1996/May/0046.html or http://www.security-express.com/archives/bugtraq/1996_2/0054.html

http://linuxgazette.net/18/tmp.html (formerly http://www.linuxgazette.com/issue18/tmp.html)

http://linuxgazette.net/20/followup.html

[2] info:(libc)Long Options


🌟 AUTHOR 🌟

Karl M. Hegbloom <karlheg@debian.org>

mostly based on tmpwatch-1.2/1.4, by: Erik Troan <ewt@redhat.com>

now being maintained for Debian by: Paul Slootman <paul@debian.org>


and that's the whole page bestie, 4th Berkeley Distribution, dated Sat Jan 5 2019</>, TMPREAPER(8) β€” go forth and clean your /tmp responsibly, protect your patterns, do NOT run her on /, and remember: she ate, she slayed, she left no files behind πŸ’–βœ¨πŸŽ€πŸ¦‹πŸ‘‘

πŸ“¦ where this page comes from