Shell programming tmpwatch
As a system administrator, you often need to regularly clear files with certain rules, such as expired logs, expired archives, and backed up files.
If you use certain matching rules to find these files and pass them to the rm command, it is actually a little troublesome. At this time, you can try tmpwatch.
Tmpwatch
Purpose:
Delete files that have not been accessed for a certain period of time.
Parameters:
-U is based on the last access time of the file, that is, the last access time. You can view it through ls-lu.
-M: Based on the last modified time of the file, that is, the last modification time is the reference. You can view it through ls-l.
-C provides reference based on the file-ctime. The ctime update condition is write, change owner, and permission. You can view it through ls-lc.
-X/PATH: exclude specific directories, that is, do not delete files in this subdirectory.
-U user_name: exclude files belonging to a specific user, that is, the user's files are not deleted.
-V shows the deletion process. By default, objects deleted are not displayed.
-- T is used for testing. The process of deleting a file is displayed instead of deleting the file.
-D does not delete the subdirectories in the file, but the files in the subdirectories will still be deleted.
-F force delete the files whose root has no write permission. For example, the root readonly File
The parameter is followed by time. The default value is hours. Some articles say that 20 D can be used to represent 20 days. I have also seen this, but in rhel5.8, only hours is supported. You need to check the version.
The directory to be checked after the time. Multiple directories can be separated by spaces.
After understanding the parameter commands, let's take a typical example.
Tmpwatch-m-x/tmp/ceshi1-U oracle-v-t 2/tmp
The preceding command deletes files in the/tmp directory that have not been changed for more than two hours and are not belong to oracle users. Files in the/tmp/test1 subdirectory are excluded. The entire deletion process is displayed, and only tests are performed.
Linux uses this command to regularly clean up the/tmp directory.
Next, let's take a look at the/etc/cron. daily/tmpwatch file in rhel5.8.
[root@localhost tmp]# 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 \ -X '/tmp/hsperfdata_*' 240 /tmp/usr/sbin/tmpwatch "$flags" 720 /var/tmpfor d in /var/{cache/man,catman}/{cat?,X11R6/cat?,local/cat?}; do if [ -d "$d" ]; then /usr/sbin/tmpwatch "$flags" -f 720 "$d" fidone[root@localhost tmp]#
This is the clearing rule. If you understand the syntax, you can learn it by yourself.