How to Remove files older than ‘N’ days using Tmpwatch/Tmpreaper on Linux

You may have missed to delete some files that are no longer needed on your computer in some directory.

It could be either in “Download” or any other directory that has been used frequently.

It might have grown up, over a period of time.

Even though if you have enough storage, you should remove the list of unused files or files which are no longer in use from the Directory, otherwise it will slow down your system performance.

It is very difficult to search a file in such a clumsy directory which is having thousands of files, when you are not sure on the specific file names or extensions.

This can be performed by using the ‘find‘ command with some combination, Please refer the below articles which were written in the past.

Today, we will show you how to achieve this, by using the Tmpwatch utility on Linux.

What is Tmpwatch

Tmpwatch recursively removes files that has not been accessed for a specific period of time in the specified directories.

Typically, it is used to clean directories automatically in the temporary file systems, such as / tmp or /var/tmp.

Ideally, it will remove the empty directories, regular files, and symbolic links.

This command will not execute on other file systems, and excludes the “lost+found” directory of the root user.

By default, tmpwatch deletes files based on their atime (access time), not their mtime (conversion time).

You can change this behavior by adding other parameters in the tmpwatch command.

WARNING: Please do not run “tmpwatch” or “tmpreaper” in “/” (root directory) because there is no mechanism in the program to protect against this.

How to install Tmpwatch on Linux

Tmpwatch can be installed as follows, from the distribution official repository.

For RHEL/CentOS 6 systems, use the yum command to install Tmpwatch.

$ sudo yum install -y tmpwatch

For Debian and Ubuntu systems, use the apt command or apt-get command to install Tmpreaper.

$ sudo apt-get install tmpreaper

For openSUSE systems, use the zypper command to install Tmpwatch.

$ sudo zypper install -y tmpwatch

For Fedora systems, use the dnf command to install Tmpwatch.

$ sudo dnf install -y tmpwatch

Make a note: If you are using Debian-based systems, use “tmpreaper” instead of tmpwatch. All examples will work as expected.

Understanding key options and arguments

  • atime (File Last Access Time) – Access time shows the last time the data was read from a file by any of the process such as command or script, etc, leaving the data unmodified.
  • mtime (File Last Modify Time) – mtime shows when you modify, append or update a file content. Most of the times ctime and mtime will be the same, unless the file attributes are updated.
  • ctime (File Last Change Time) – ctime shows when your file metadata got changed. It means when the file attributes are changed like ownership or group, etc.,
  • dirmtime (Directory Last modification time) – dirmtime shows the last modified time of the directory.

The time parameter defines the threshold for removing files.

  • d – for days
  • h – for hours
  • m – for minutes
  • s – for seconds

Remove files that haven’t been accessed for a Period of Time

As mentioned in the beginning of the article, Tmpwatch delete files, by default, referring the access time of the files. No need to add the hours parameter as suffix to time, since the action is performed by default using the hours unit.

For example, run the command below to recursively remove files, that have not been accessed for the past 5 hours.

# tmpwatch 5 /tmp

Run the command below to delete files that have not been modified for the last 10 hours. If you want to delete files based on their modification time (mtime), you need to add the “-m” option with the tmpwatch command.

# tmpwatch -m 10 /home/daygeek/Downloads

Delete files that haven’t been accessed more than “X” days

If you want to delete files using days, you need to add the suffix “d”. The example below deletes files older than 30 days.

# tmpwatch 30d /home/daygeek/Downloads

Delete all files that haven’t been accessed for a Period of Time

The below command removes all file types, not just regular files, symbolic links and directories based on the file modification time.

# tmpwatch -am 12 /tmp

Exclude a directory

The below command will delete all files and excludes directories that haven’t been modified for the past 10 hours.

# tmpwatch -am 10 --nodirs /home/daygeek/Downloads

Exclude a specific path

The below command will delete all files except the directory below which has not been modified for the past 10 hours.

# tmpwatch -am 10 --exclude=/home/daygeek/Downloads/Movies /home/daygeek/Downloads

Exclude specific pattern

The below command will delete all files except the Pattern below which has not been modified for the past 10 hours.

# tmpwatch -am 10 --exclude-pattern='*.pdf' /home/daygeek/Downloads

Perform a dry run

If you want to perform a test run without actually delete anything on the system, use the below command.

# tmpwatch -t 5h /home/daygeek/Downloads

Setup a cronjob to delete files periodically

By default, it leaves a cronjob file under the “/etc/cron.daily/tmpreaper” directory. This cronjob works according to the configuration file located in “/etc/timereaper.conf”. You can customize the file according to your requirement.

It runs once a day and deletes files older than 7 days.

Alternatively, if you would like to perform an action periodically, you can manually add a cronjob based on your needs.

# crontab -e

0 10 * * * /usr/sbin/tmpwatch 15d /home/daygeek/Downloads

The above cronjob will delete files that are older than 15 days daily at 10AM.

About Magesh Maruthamuthu

Love to play with all Linux distribution

View all posts by Magesh Maruthamuthu

Leave a Reply

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