Master Linux Cleanup: How to Use tmpwatch to Auto-Delete Stale Files
This guide explains what the tmpwatch utility does, how to install it on various Linux distributions, details its key options and time‑unit suffixes, provides step‑by‑step examples for deleting unused files, shows how to exclude paths or patterns, preview deletions, and schedule regular clean‑ups with cron.
Many systems accumulate forgotten, unused files that waste resources; identifying and removing such files without knowing their names can be difficult.
What is tmpwatch
tmpwatchrepeatedly deletes files in a specified directory that have not been accessed within a given time frame. It is commonly used on temporary directories such as /tmp or /var/tmp. The command removes empty directories, regular files, and symbolic links while avoiding root‑owned system files.
By default it bases its decision on atime (last access time) rather than mtime (last modify time); the criterion can be changed with appropriate options.
Warning: Do not run tmpwatch or tmpreaper on the root directory, as there is no safeguard against deleting critical system files.
Installation
Installation commands differ by Linux distribution:
RHEL/CentOS 6: $ sudo yum install -y tmpwatch Debian/Ubuntu (command is tmpreaper): $ sudo apt-get install tmpreaper openSUSE: $ sudo zypper install -y tmpwatch Fedora:
$ sudo dnf install -y tmpwatchKey options and parameters
atime– file last access time. mtime – file last modify time. ctime – file metadata change time. dirmtime – directory last modification time.
Time suffixes used to specify the age threshold: d – days. h – hours. m – minutes. s – seconds.
Basic usage examples
Delete files in /tmp not accessed in the last 5 hours (default atime, hour unit): # tmpwatch 5 /tmp Delete files in /home/daygeek/Downloads not modified in the last 10 hours (using mtime): # tmpwatch -m 10 /home/daygeek/Downloads Delete files older than 30 days: # tmpwatch 30d /home/daygeek/Downloads Delete all files (including directories) not accessed for 12 hours:
# tmpwatch -am 12 /tmpExcluding paths and patterns
Exclude directories from deletion:
# tmpwatch -am 10 --nodirs /home/daygeek/DownloadsExclude a specific subdirectory:
# tmpwatch -am 10 --exclude=/home/daygeek/Downloads/Movies /home/daygeek/DownloadsExclude files matching a pattern (e.g., PDFs):
# tmpwatch -am 10 --exclude-pattern='*.pdf' /home/daygeek/DownloadsPreview mode
Show what would be deleted without actually removing files:
# tmpwatch -t 5h /home/daygeek/DownloadsScheduling with cron
Place a script in /etc/cron.daily/tmpreaper or add a crontab entry. Example to run daily at 10 AM and delete files not accessed for 15 days:
# crontab -e
0 10 * * * /usr/sbin/tmpwatch 15d /home/daygeek/DownloadsSigned-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
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.)
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
