Method for tmpwatch Command to Clear Old Files

  • 2021-08-31 09:55:14
  • OfStack

We know that there are often 1 forgotten to delete a long time not used and really useless files, if not to deal with, these useless files will be more and more, wasting a lot of system resources. Without knowing the file name, it is difficult to check which files in a 1 directory have not been accessed for a long time and delete these files at the same time. Today, we introduce a very practical tool to do this-tmpwatch

What is tmpwatch

tmpwatch can cyclically delete files in a specified directory that have not been accessed for a specified time. This 1 command is often used to clean up temporary file directories, such as /tmp Or /var/tmp This kind of catalogue.

It only clears empty directories, normal files, and symbolic link files in specified directories, does not affect other directories, and avoids system-related critical files belonging to root users.

By default, tmpwatch Command is based on the file's atime (access time) instead of mtime (modify time) to delete the file. If you want to change its deletion basis, you can add the parameters you want to modify when using this command.

Note: Never run under the root directory tmpwatch Or tmpreaper Command, because there is no mechanism to prevent you from running this command in the root directory.

How to Install tmpwatch on Linux

The installation commands for the various Linux distributions differ slightly, as shown below.

If your Linux distribution is REHL/CentOS 6, you can use the yum Command to install tmpwatch, as shown below:


$ sudo yum install -y tmpwatch

If the Linux distribution you are using is Debian or Ubuntu, you need to use apt Or apt-get Command to install Tmpreaper, as shown below:


$ sudo apt-get install tmpreaper

If you are using openSUSE, you should use /var/tmp0 Command to install tmpwatch:


$ sudo zypper install -y tmpwatch

If the Linux distribution is Fedora, you should use the dnf Command to install tmpwatch:


$ sudo dnf install -y tmpwatch

Explanation: In Linux system based on Debian, it is used tmpreaper Command substitution tmpwatch If the system you are using is Linux based on Debian, then the following example tmpwatch For tmpreaper That's enough.

tmpwatch Key options and parameters of the command

atime (File Last Access Time): The last time the file was accessed; mtime (File Last Modify Time): When the contents of the file were last modified; ctime (File Last Change Time): The last time file metadata was modified, that is, the time when file-related attributes were modified. In most cases, mtime and ctime values are the same, but when attributes such as file owner, permission and group are modified, they will only affect ctime; dirmtime (Directory Last modification time): The last time the directory was modified.

These time parameters are used to set conditional thresholds for deleting files:

d : The unit is days; h : In hours; m : In minutes; s : In seconds.

Use tmpwatch Command to delete files that have not been accessed for 1 period of time

As mentioned earlier, tmpwatch The default option for the command is atime And the default unit parameter is h So if you do want to delete files in hours of access time, you don't need to add any special options or parameter suffixes, you can just use this command. As shown in the following example, it is deleted /tmp Files in the directory that have not been accessed in the past 5 hours:


# tmpwatch 5 /tmp

The following example is to delete the atime0 Directory has not been modified in the past 10 hours, note that if you want to press mtime To delete the file, you need to add in the command atime2 Options for:


# tmpwatch -m 10 /home/daygeek/Downloads

Delete files that have not been accessed in other units for a certain period of time

If you want to be in days, you need to add d The following is the suffix for deleting files that have not been accessed within 30 days:


# tmpwatch 30d /home/daygeek/Downloads

Delete all files that have not been used in 1 period of time

If you want to delete not only ordinary files, symbolic link files and empty directory files, but also all files in the specified directory that have not been accessed for a certain period of time, you need to add options -a To delete all files in the specified directory that have not been modified for 12 hours:


# tmpwatch -am 12 /tmp

Exclude certain directories from deletion operations

The following command can keep directories that have not been modified within 10 hours from being deleted:


# tmpwatch -am 10 --nodirs /home/daygeek/Downloads

Exclude specific paths from deletion operations

The following command deletes atime0 All files in the directory that have not been modified within 10 hours, but /home/daygeek/Downloads/Movies Path is not affected, that is, files that have not been modified within 10 hours under this path will not be deleted:


# tmpwatch -am 10 --exclude=/home/daygeek/Downloads/Movies /home/daygeek/Downloads

Exclude files in specific formats from deletion operations

The command shown below deletes all files under the specified file that have not been changed within 10 hours, except for pdf Format file:


$ sudo apt-get install tmpreaper
0

Preview tmpwatch The effect of

The following command is the tmpwatch Preview the functional effects of:


$ sudo apt-get install tmpreaper
1

Use tmpwatch Set 1 timed task to perform deletion operation periodically

To accomplish this task, you will /etc/cron.daily/tmpreaper Leave one in the directory cronjob File, this file is based on /etc/timereaper.conf You can set it according to your own needs.

The following settings can delete files in the specified directory that have not been accessed for 105 days at 10 am every day:


$ sudo apt-get install tmpreaper
2

Related articles: