GICT knowledge base
tmpwatch Command in Linux
Published
tmpwatch Command in Linux
The use of “tmpwatch ” command in linux is to removes files which haven’t been accessed for a period of time. The tmpwatch recursively removes files which haven’t been accessed for a given time. Normally, it’s used to clean up directories which are used for temporary holding space such as /tmp. When changing directories, tmpwatch is very sensitive to possible race conditions and will exit with an error if one is detected. It does not follow symbolic links in the directories it’s cleaning (even if a symbolic link is given as its argument), does not switch file systems (including non-trivial bind mounts), skips lost found directories owned by the root user, and only removes empty directories, regular files, symbolic links, and on some systems also unused sockets. By default, tmpwatch dates files by their atime (access time), not their mtime (modification time). If files aren’t being removed when ls -l implies they should be, use ls -u to examine their atime to see if that explains the problem. If the –atime, –ctime or –mtime options are used in combination, the decision about deleting a file will be based on the maximum of these times. The –dirmtime option implies ignoring atime of directories, even if the –atime option is used. The time parameter defines the threshold for removing files. If the file has not been accessed for time, the file is removed. The time argument is a number with an optional single-character suffix specifying the units: m for minutes, h for hours, d for days. If no suffix is specified, time is in hours.
Install tmpwatch
The tmpwatch command will not be there by default in Linux so we need to install it manually.
1) Install tmpwatch in Ubuntu:
# sudo apt-get install tmpreaper
2) Install tmpwatch CentOS/Fedora/RHCE:
# yum install tmpwatch -y
3) To locate the location of tmpwatch installation
# whereis tmpwatch
tmpwatch: /usr/bin/tmpwatch /usr/sbin/tmpwatch
/usr/share/man/man8/tmpwatch.8.gz
Options
- -u To delete files based on the access time.
- -t To operate in test mode.
- -m To delete a file based on the modification time
- -c To delete files based on the inode change time
- -a To remove all file type
- -f To forcefully remove files even if root doesn’t have write access
- -d To discard directories from removing
- -s To check for open files before deleting them
Removing the files
To delete files more than 30d old from apache log.
# tmpwatch 30d /etc/apache2/logs/domlogs/
List all the files except directories that haven’t been accessed for at least 30 hours
#tmpwatch –mtime 30 –nodirs /tmp –test
Delete all the files except directories that haven’t been accessed for at least 30 hours
# tmpwatch -am 30 –nodirs /tmp
Schedule Cron Job
Tmpwatch can be configured to remove files older than X hours, and has a daily cron job that kicks off from /etc/cron.daily to purge a select few files in /tmp and /var/:
# cat /etc/cron.daily/tmpwatch
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 240 /tmp
/usr/sbin/tmpwatch “$flags” 720 /var/tmp
for d in /var/{cache/man,catman}/{cat?,X11R6/cat?,local/cat?}; do
if [ -d “$d” ]; then
/usr/sbin/tmpwatch “$flags” -f 720 “$d”
fi
done
Related guides
How to Restrict an SSH Key to One Command
Restrict an SSH key to one command with authorized_keys options. Protect the command and check that unwanted shell and forwarding access are denied.
Linux and CommandsSelf-Hosted Video Processing With FFmpeg: A Practical Guide
Learn how to self-host video processing on a VPS using FFmpeg, with practical commands for conversion, compression, thumbnails, and automated workflows.
Linux and CommandsHow to use nohup: Commands in Linux That Survive Terminal Closes
Keep commands running after you close the terminal. nohup examples with wget, Python, rsync, and Docker on any VPS.
Linux and CommandsHow to Fix “Too Many Open Files” Error on Linux Servers
Learn how to fix the "Too Many Open Files" error on Linux servers by understanding file descriptors, adjusting limits, and diagnosing leaks effectively.
