Detailed explanation of tmpwatch command

Time:2021-10-12

We knowLinux systemThere are often some useless files that have not been used for a long time and are really useless. If they are not processed, there will be more and more useless files, wasting a lot of system resources. Without knowing the file name, it is difficult to check which files in a directory have not been accessed for a long time and delete these files at the same time. Today we will introduce a very practical tool to complete this work————tmpwatch

Linux学习

What is tmpwatch

Tmpwatch can cyclically delete files in a specified directory that have not been accessed within a specified time. This command is often used to clean up temporary file directories, such as/tmpperhaps/var/tmpSuch directories.

It only clears the empty directory, ordinary files and symbolic link files in the specified directory, does not affect other directories, and will avoid those key files related to the system belonging to the root user.

By default,tmpwatchThe command is based on the of the fileatime(access time) instead ofmtime(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.

be careful: never run under the root directorytmpwatchperhapstmpreaperCommand, because the system may not have any mechanism to prevent you from running this command in the root directory.

How to install tmpwatch on Linux

The installation commands of various Linux distributions are slightly different, and the specific installation methods are shown below.

If your Linux distribution is rehl / CentOS 6, you can useyumCommand to install tmpwatch, for example:

$ sudo yum install -y tmpwatch

If your Linux distribution is Debian or Ubuntu, you need to useaptperhapsapt-getCommand to install tmcreaper, for example:

$ sudo apt-get install tmpreaper

If you are using openSUSE, you should usezypperCommand to install tmpwatch:

$ sudo zypper install -y tmpwatch

If the Linux distribution is Fedora, you should usednfCommand to install tmpwatch:

$ sudo dnf install -y tmpwatch

explainIn Debian based Linux system, it istmpreaperCommand substitutiontmpwatch, if you are using a Debian based version of Linux, use thetmpwatchReplace withtmpreaperJust.

tmpwatchKey options and parameters of the command

  • atime(file last access time): the time when the file was last accessed;
  • mtime(file last modify time): the time when the file content was last modified;
  • ctime(file last change time): the time when the file metadata was last modified, that is, the time when the 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 that do not involve content are modified, only CTime will be affected;
  • dirmtime(directory last modification time): the time when the directory was last modified.

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

  • d: unit: day;
  • h: unit: hour;
  • m: in minutes;
  • s: in seconds.

usetmpwatchCommand to delete a file that has not been accessed for a period of time

As mentioned earlier,tmpwatchThe default option for the command isatime, and the default unit parameter ishTherefore, if you really want to delete files based on the access time in hours, you can use this command directly without adding any special options or parameter suffixes. As shown in the following example, it is deleted/tmpFiles not accessed in the last 5 hours under the directory:

# tmpwatch 5 /tmp

The following example is delete/home/daygeek/DownloadsFor files in the directory that have not been modified in the past ten hours, note that if you want to pressmtimeTo delete a file, you need to add-mOptions for:

# tmpwatch -m 10 /home/daygeek/Downloads

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

If you want to be in days, you need to adddThe suffix of is as follows to delete files that have not been accessed within 30 days:

# tmpwatch 30d /home/daygeek/Downloads

Delete all files that have not been used for a 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 within a certain period of time, you need to add the option-a, the following is to delete all files that have not been modified for 12 hours in the specified directory:

# tmpwatch -am 12 /tmp

Exclude some directories from deletion

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

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

Exclude specific paths from deletion

The following command deletes/home/daygeek/DownloadsAll files in the directory that have not been modified within ten hours, but/home/daygeek/Downloads/MoviesThe path is not affected, that is, the files that have not been modified within ten hours under the path will not be deleted:

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

Exclude files of a specific format from deletion

The command shown below is to delete all files under the specified file that have not been changed within 10 hours, exceptpdfFile format:

# tmpwatch -am 10 --exclude-pattern='*.pdf' /home/daygeek/Downloads

RehearsaltmpwatchEffect of

The following command is righttmpwatchPreview the functional effects of:

# tmpwatch -t 5h /home/daygeek/Downloads

usetmpwatchSet a scheduled task to perform deletion periodically

To complete this task, you will/etc/cron.daily/tmpreaperLeave one under the directorycronjobFile, this file is in accordance with/etc/timereaper.confYou can set it according to your needs.

The following settings can delete the files in the specified directory that have not been accessed for 15 days at 10 a.m. every day:

# crontab -e0 10 * * * /usr/sbin/tmpwatch 15d /home/daygeek/Downloads

abouttmpwatchThat’s all for the introduction of the command. I hope this article can be helpful to all readers. If you have any questions or other ideas about this command, please leave a message for discussion

This article is composed of blog one article multi posting platformOpenWriterelease!

Recommended Today

The selector returned by ngrx store createselector performs one-step debugging of fetching logic

Test source code: import { Component } from ‘@angular/core’; import { createSelector } from ‘@ngrx/store’; export interface State { counter1: number; counter2: number; } export const selectCounter1 = (state: State) => state.counter1; export const selectCounter2 = (state: State) => state.counter2; export const selectTotal = createSelector( selectCounter1, selectCounter2, (counter1, counter2) => counter1 + counter2 ); // […]