Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to delete old files that have not been used for a long time under Linux

2025-03-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly shows you "how to delete old files that have not been used for a long time under Linux". The content is simple and clear. I hope it can help you solve your doubts. Let me lead you to study and learn this article "how to delete old files that have not been used for a long time under Linux".

What is tmpwatch?

Tmpwatch can cyclically delete files in a specified directory that have not been accessed for a specified period of time, and this command is often used to clean up temporary file directories, such as / tmp or / var/tmp.

It only clears empty directories, normal files, and symbolic link files under the specified directory, does not affect other directories, and avoids system-related critical files that belong to root users.

By default, the tmpwatch command deletes a file based on its atime (access time) rather than mtime (modify time). If you want to change its deletion basis, you can add the parameters you want to modify when using this command.

Note: never run the tmpwatch or tmpreaper command in the root directory, 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 various Linux distributions are slightly different, and the specific installation method is 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 you are using a Linux distribution of Debian or Ubuntu, you need to use the apt or apt-get command to install Tmpreaper, as shown below:

$sudo apt-get install tmpreaper

If you are using openSUSE, install tmpwatch using the zypper command:

$sudo zypper install-y tmpwatch

If the Linux distribution is Fedora, install tmpwatch using the dnf command:

$sudo dnf install-y tmpwatch

Note: in the Debian-based Linux system, the tmpreaper command is used instead of tmpwatch. If the system you are using is a Debian-based version of Linux, then change the tmpwatch in the following example to tmpreaper.

Key options and parameters of the tmpwatch command

Atime (File Last Access Time): when the file was last accessed

Mtime (File Last Modify Time): when the contents of the file were last modified

Ctime (File Last Change Time): the time when the file metadata was last modified, that is, the time when the relevant attributes of the file were modified, in most cases, the mtime and ctime values are the same, but when the attributes that do not involve content, such as file owner, permission, group, are modified, it will only affect ctime.

Dirmtime (Directory Last modification time): when the directory was last modified.

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

D: in days

H: in hours

M: in minutes

S: in seconds.

Delete files that have not been accessed for a period of time with the tmpwatch command

As mentioned earlier, the default option for the tmpwatch command is atime, and the default unit parameter is h, so if you do want to delete files by the access time calculated in hours, then you do not have to add any special options or parameter suffixes, you can use this command directly. As shown in the following example, files in the / tmp directory that have not been accessed in the past 5 hours are deleted:

# tmpwatch 5 / tmp

The following example is to delete files in the / home/daygeek/Downloads directory that have not been modified in the past ten hours. Note that if you want to press mtime to delete files, you need to add the option of-m to the command:

# tmpwatch-M10 / home/daygeek/Downloads

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

If you want to use days as a unit, you need to add the suffix d, as follows: 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 under the specified directory that have not been accessed for a certain period of time, you need to add the option-a, as follows: delete all files under the specified directory that have not been modified for 12 hours:

# tmpwatch-am 12 / tmp

Exclude some directories from the delete operation

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

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

Exclude a specific path from the delete operation

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

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

Exclude files of a specific format from the delete operation

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

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

Rehearse the effect of tmpwatch

The following command is a rehearsal of the functional effect of tmpwatch:

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

Use tmpwatch to set a scheduled task to perform delete operations periodically

To accomplish this task, you will leave a cronjob file in the / etc/cron.daily/tmpreaper directory, which works according to the settings of / etc/timereaper.conf, and you can set it according to your needs.

The following settings delete files in the specified directory that have not been accessed for 15 days at 10:00 every morning:

# crontab-e 010 * / usr/sbin/tmpwatch 15d / home/daygeek/Downloads above are all the contents of the article "how to delete old files that have not been used for a long time under Linux". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report