[[linux_wiki:tmpwatch]]

Tmpwatch

General Information

tmpwatch is a utility that recursively removes files that haven't been accessed for a specified period of time.

It is normally used to clean up directories such as /tmp.

Checklist

  • Distro(s): Enterprise Linux 6

Install

Tmpwatch does not come installed on a CentOS 6 minimal installation.

yum install tmpwatch

After installation, there will now be a tmpwatch cron script in: /etc/cron.daily/tmpwatch

See examples below for contents and explanation.


Tmpwatch Usage

Format

tmpwatch -[options] <time> <directories>

time

  • Threshold for removing files.
  • Number with optional single character suffix
    • m = minutes
    • h = hours (default if not specified)
    • d = days

directories

  • One or more directories specified for clean up. (Space separated)

Common Options

  • -u, –atime ⇒ Delete files based on the file's access time. (default setting)
    • Note: the “updatedb” file system scans keep the atime of directories recent.
  • -m, –mtime ⇒ Delete files based on the file's modification time.
  • -c, –ctime ⇒ Delete files based on the file's inode change time. For directories, delete based on modification time.
  • -a, –all ⇒ Remove all file types, not just regular files, symbolic links and directories.
  • -f, –force ⇒ Remove files even if root doesn't have write. (same as rm -f)
  • -t, –test ⇒ Don't actually remove anything.
  • -v, –verbose ⇒ Print display.
  • -x ⇒ Skip specified path. (if directory, skip all files inside)
  • -X ⇒ Skip paths matching specified pattern.

  • Clean up an application's tmp directory at /opt/app/tmp
  • Check access time (-u), modification time (-m), and change time (-c).
  • Delete if any of the three are older than 30 days (30d).
tmpwatch -umc 30d /opt/app/tmp

First tmpwatch (with all the “-x”)

  • Clean up /tmp
  • Check access time (-u), modification time (-m), and change time (-c).
  • Exclude all directories specified after each “-x”
  • Exclude any directory that matches the pattern after the “-X”
  • Delete if any of the -umc options are older than 10 days (10d).

Second tmpwatch “$flags” 30d /var/tmp

  • Clean up /var/tmp
  • Check access time (-u), modification time (-m), and change time (-c).
  • Delete if any of the -umc options are older than 30 days (30d).

Third /usr/sbin/tmpwatch “$flags” -f 30d “$d”

  • Clean up each directory ($d) in the for loop pattern.
  • Check access time (-u), modification time (-m), and change time (-c).
  • Force deletion (-f)
  • Delete if any of the -umc options are older than 30 days (30d).

/etc/cron.daily/tmpwatch

#! /bin/sh
flags=-umc
/usr/sbin/tmpwatch "$flags" -x /tmp/.X11-unix -x /tmp/.XIM-unix \
	-x /tmp/.font-unix -x /tmp/.ICE-unix -x /tmp/.Test-unix \
	-X '/tmp/hsperfdata_*' 10d /tmp
/usr/sbin/tmpwatch "$flags" 30d /var/tmp
for d in /var/{cache/man,catman}/{cat?,X11R6/cat?,local/cat?}; do
    if [ -d "$d" ]; then
	/usr/sbin/tmpwatch "$flags" -f 30d "$d"
    fi
done

  • linux_wiki/tmpwatch.txt
  • Last modified: 2019/05/26 03:50
  • (external edit)