tmpwatch tool is analyze and recursively delete files

tempwatch command

tmpwatch tool is analyze and recursively delete files which have not been used certain period of time, especially clean up unwanted backup or /tmp directory or unused files.


How to Install tmpwatch in Ubuntu :

~$  sudo apt-get install tmpreaper

How to Install tmpwatch command in CentoOS / Fedora / RHEL :

By default, this package not being installed in Linux so that we can install using yum command,

~]#  yum install tmpwatch -y

To know tmpwatch Installed Location, run below command

~]# whereis tmpwatch
tmpwatch: /usr/bin/tmpwatch /usr/sbin/tmpwatch /usr/share/man/man8/tmpwatch.8.gz

How to cleanup /tmp directory files that haven’t been accessed for at least 30 days

~]# tmpwatch 30d /tmp/

all the files that haven’t been accessed for at least 30 days

~]# tmpwatch --test 60d  ~/download/
removing file /root/download/tmpwatch_1
removing file /root/download/tmpwatch_2
removing file /root/download/tmpwatch_3
removing file /root/download/tmpwatch_4

Removing all the files that haven’t been accessed for at least 30 days,

# tmpwatch  --all 30d  /tmp/

Removing all the files that haven’t been accessed for at least 8 hours,

~]# tmpwatch --all -mf 8 /tmp

Remove all the files except directories even if they are empty

First command is do not delete but listing out what would be deleted and the second command is delete all the files only  that haven’t been accessed for at least 30 hours

~]# tmpwatch --mtime 30 --nodirs /tmp --test
~]# tmpwatch -am  30 --nodirs /tmp

Schedule Cron Job every 5 Hours

~]# crontab -e
~]# 0  */5  *   *   *   /usr/bin/tmpwatch   -am  30  --nodirs /tmp
-d (or) --nodirs     Do not attempt to remove directories, even if they are empty.
-m (or) --mtime     Deleting  files based on  modification time
-a,(or)  --all           Remove all file types
-t (or)  --test         Don't remove files, displaying what would be deleted

Here is my crontab entry for cleaning /tmp directory:
bash> crontab -l
 
# clean /tmp directory every two hours
0 */2 * * * /usr/sbin/tmpwatch -maf 8 /tmp

This crontab entry will delete all files from the /tmp directory older than 8 hours. Cron will run tmpwatch every two hours. Parameters for the tmpwatch are:

    m – make the decision about deleting a file based on the file’s mtime
    a – remove all file types, not just regular files, symbolic links and directories
    f – remove files even if root doesn’t have write access

I also use tmpwatch to delete old MySQL backup files. Every morning, cron starts mysqldump to save file in a /backup directory. After a month, /backup directory will contain about 30 MySQL backup files. With tmpwatch, the number of files can be limited (tmpwatch -maf 240 /backup – will keep the last 10 backup files)

I firstly used tmpwatch with -caf parameters and that works. In the meantime, we implemented NetBackup system. The result was: no left space on device /dev/sda5 Why? Because tmpwatch didn’t find any file to delete. And why? Because of NetBackup! 🙂 I thought c parameter means the file creation time, but man tmpwatch says:

c – make the decision about deleting a file based on the file’s ctime (inode change time)

What is inode change time?
The inode change time represents the time when the file’s meta-information last changed. One common example of this is when the permissions of a file change. Changing the permissions doesn’t access the file, so the atime doesn’t change, nor does it modify the file, so the mtime doesn’t change. Yet, something about the file itself has changed, and this must be noted somewhere. This is the job of the ctime field.

To display a file's mtime run:
~]# ls -l <filename>
To display a file's atime run:
~]# ls -lu <filename>
To display a file's ctime run:
~]# ls -lc <filename>

 

Thanks for your wonderful Support and Encouragement

Ankam Ravi Kumar

Working as Linux / Storage Administrator L3. Interested in sharing the knowledge.