Operations 9 min read

Master tmpwatch: Safely Clean Up Unused Files on Linux

This guide explains how to install, configure, and use the Linux tmpwatch/tmpreaper commands—including safety warnings, option details, example usages, and cron automation—to reliably delete files that haven't been accessed or modified within a specified time frame.

Liangxu Linux
Liangxu Linux
Liangxu Linux
Master tmpwatch: Safely Clean Up Unused Files on Linux

Warning

Never run tmpwatch (or tmpreaper) on the root directory /. The root contains essential system files and the tool has no built‑in protection, so accidental deletion can render the system unusable.

Installation

Most Linux distributions provide the utility in their default repositories:

Fedora: sudo dnf install tmpwatch CentOS: sudo yum install tmpwatch openSUSE: sudo zypper install tmpwatch Debian/Ubuntu (package name tmpreaper):

sudo apt install tmpreaper

Basic usage

tmpwatch

and tmpreaper share the same syntax. Replace the command name with the one installed on your system.

Delete files not accessed for a given period tmpwatch 10d /var/log/ Deletes files and empty directories in /var/log that have not been accessed for 10 days.

Delete files based on modification time tmpwatch -m 10d /var/log/ Uses the modification timestamp instead of the access timestamp.

Delete symbolic links only tmpwatch -s 10 /var/log/ Delete all file types (regular files, links, directories) tmpwatch -a 10 /var/log/ Exclude directories from deletion tmpwatch -am 10 --nodirs /var/log/ Test mode (dry‑run) tmpwatch -t 30 /var/log/ The command prints the files it would delete without removing them.

Force deletion of read‑only files tmpwatch -f 10h /var/log/ Protect specific files (whitelist) tmpreaper --protect '*.txt' -t 10h /var/log/ Files matching the pattern are skipped.

Time suffixes can be d (days), h (hours), m (minutes) or s (seconds). The default unit is hours.

Cron automation

To run the cleanup daily at 1 AM and delete files older than 30 days, add a crontab entry:

# crontab -e
0 1 * * * /usr/sbin/tmpwatch 30d /var/log/

On Debian/Ubuntu, installing tmpreaper creates a daily cron job ( /etc/cron.daily/Tmpreaper) that by default removes files older than 7 days. The interval can be changed by editing TMPREAPER_TIME in /etc/timereaper.conf.

Reference

For a complete list of options, consult the manual pages:

man tmpwatch
man tmpreaper
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

cronSystem AdministrationFile Cleanuptmpwatchtmpreaper
Liangxu Linux
Written by

Liangxu Linux

Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.