How to use tmpwatch to Auto Clean Up Files and Logs in Linux

Tmpwatch is tools that recursively removes files which have not been used or accessed for certain period of time. In this post, i will describe how we can automate the clean up process on log directory on linux CentOS 6.6. You can use tmpwatch to safely remove unwanted files for a given time.

1. How to install tmpwatch package :

[root@centos66 ~]# yum install tmpwatch -y

2. As example, delete all logs in /var/log/nginx for more than 30d(30 days).

[root@centos66 ~]# /usr/sbin/tmpwatch 30d /var/log/nginx

3. Add the tmpwatch into the cronjob so that we can automate the removal process. As example let the removal task start 10PM every night.

0 22 * * * /usr/sbin/tmpwatch 30d /var/log/nginx

Thats All.