tmpwatch Command in Linux



The tmpwatch command in Linux removes the unused files from the system. This utility on Linux systems (primarily Red Hat-based distributions) is used to recursively remove files that have not been accessed in a specified period of time. It is commonly used to clean up temporary directories like /tmp or /var/tmp.

Table of Contents

Here is a comprehensive guide to the options available with the tmpwatch command −

Installation of tmpwatch Command

To use tmpwatch, install it on the Linux system.

On CentOS, RHEL, run the following command −

sudo yum install tmpwatch

On Fedora, use −

sudo dnf install tmpwatch

On Debian, Ubuntu, or their derivatives, run the following command −

sudo apt install tmpreaper

On Debian-based systems, tmpreaper is a functional equivalent to tmpwatch with slightly different usage.

To verify, check the binary using the which command −

which tmpreaper
tmpwatch Command in Linux1

Syntax of tmpwatch Command

The syntax of the tmpwatch command in Linux is as follows −

tmpwatch [options] <time> <directory>

The syntax of tmpreaper is given below −

tmpreaper [options] <delay> <directory>

tmpwatch Command Options

The common options for the Linux tmpwatch and tmpreaper are listed below −

Short Option Long Options Description
-u --atime Use access time (default) to decide file deletion.
-m --mtime Use modification time instead of access time.
-c --ctime Use change time instead of access time (ignored if --mtime is set).
-M --mtime-dir Use modification time for directories instead of access time.
-a --all Remove all file types (not just regular files, symlinks, and directories).
-d --nodirs Do not remove directories, even if empty.
-f --force Force deletion even without write access (like rm -f).
-l --nosymlinks Do not remove symbolic links.
-s --symlinks Explicitly remove symbolic links.
-q --quiet Show only fatal errors.
-t --test Simulate deletion without removing files (implies --verbose).
-v --verbose Show detailed output; use twice (-vv) for maximum verbosity.
-U user --exclude-user=user Skip files owned by the specified user (name or UID).
-x path --exclude=path Skip the specific path and its contents.
-X pattern --exclude-pattern=pattern Skip files matching a glob pattern (must be absolute, no symlinks).
--showdeleted Output simulated rm/rmdir commands for files that would be deleted.
--protect 'pattern' Prevent deletion of files matching the shell pattern (use single quotes).
--delay[=x] Wait randomly up to x seconds before starting (default max: 256s).
-T x --runtime=x Abort after x seconds (default: 55s) to prevent abuse or overload.
-h --help Display help and exit.

Examples of tmpwatch Command in Linux

This section demonstrates how to use the tmpwatch and tmpreaper commands in Linux with examples −

Testing which Files would be Removed

To test which files would be removed from the specific directory without actually removing them, use the -t or --test option −

tmpwatch --test 240 /tmp

In the above command, 240 is the number of hours, which is equal to 10 days. And for tmpreaper −

tmpreaper --test 10d /tmp
tmpwatch Command in Linux2

Removing Files from a Directory Not Accessed in the Last 5 Days

To remove files from a directory not accessed in the last 5 days, use the following commands −

tmpwatch 120 /tmp

And for tmpreaper

tmpreaper 5d /tmp

Removing Files Not Modified in the Last 10 Days

To remove files that have not been modified in the last 10 days, use the -m or --mtime option −

tmpwatch --mtime 240 /tmp

The equivalent tmpreaper command is as follows −

tmpreaper --mtime 10d /tmp

Excluding Specific Files

To exclude a specific file while removing, use the -x or --exclude option followed by the path −

tmpwatch -x tmp/mydir 168 /var/tmp

And for tmpreaper, use the --protect option followed by a pattern −

tmpreaper --protect '*.log'  7d /var/tmp

Removing Files Based on Change Time

To remove the files based on change time, use the -c or --ctime option −

tmpwatch --ctime 168 /tmp

For the tmpreaper

tmpreaper --ctime 7d /tmp

Removing All Files Including the Symbolic Links

To remove all the files, including the symbolic links, use the following commands −

tmpwatch --all 168 /tmp

For tmpreaper, use −

tmpreaper --symlinks 7d /tmp
tmpwatch Command in Linux3

Conclusion

The tmpwatch command in Linux is a useful tool for automatically deleting unused files that have not been accessed, modified, or changed within a specified time. It is mainly used to clean temporary directories like /tmp and /var/tmp, helping to manage disk space and system hygiene.

On Debian-based systems, the equivalent utility is tmpreaper, which works similarly with slightly different options. Both commands offer flexibility through various flags to control deletion criteria, test outcomes, exclude files, or include symbolic links.

Advertisements