In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces linux how to use Tmpwatch/Tmpreaper to delete old files, the article is very detailed, has a certain reference value, interested friends must read it!
What is tmpwatch?
Tmpwatch recursively deletes files that have not been accessed within the specified period of time in the specified directory. Typically, it is used to automatically clear temporary file system directories, such as / tmp and / var/tmp.
It only deletes empty directories, regular files, and symbolic links. It does not switch to another file system and avoids the lost+found directory that belongs to the root user.
By default, tmpwatch deletes a file based on its atime (access time) rather than mtime (modification time).
You can change these behaviors by adding additional parameters to the tmpwatch command.
Warning: do not run tmpwatch or tmpreaper in /, as there is no mechanism in the program to prevent this.
How to install tmpwatch on Linux
You can install tmpwatch in the official warehouse as follows.
For RHEL/CentOS 6 systems, use the yum command to install tmpwatch.
$sudo yum install-y tmpwatch
For Debian and Ubuntu systems, use the apt command or the apt-get command to install tmpreaper.
$sudo apt-get install tmpreaper
For openSUSE systems, use the zypper command to install tmpwatch.
$sudo zypper install-y tmpwatch
For Fedora systems, use the dnf command to install tmpwatch.
$sudo dnf install-y tmpwatch
Please note: if you are using a Debian-based system, use tmpreaper instead of tmpwatch. All the examples work as expected.
Understand key options and parameters
Atime (file last access time): displays the last time any process, such as a command or script, accessed the data in the file.
Mtime (time when the file was last modified): displays the time when the file content was modified or saved. Unless you are changing file properties, ctime and mtime will be the same in most cases.
Ctime (when the file was last changed): displays the time when the file metadata was changed. This means when the file properties were changed (such as ownership or groups, etc.).
Dirmtime (last modified time of the directory): displays the last modified time of the directory.
The time parameter defines the threshold for deleting files.
D-day
H-hour
M-minute
S-second
How to use the tmpwatch command to delete files that have not been accessed for a period of time
As I said at the beginning of this article, tmpwatch deletes files based on file access time (atime) by default. In addition, because hours are the default parameter, if you use hour units, there is no need to add a suffix to the time.
For example, run the following command to recursively delete files that have not been accessed in the past 5 hours.
# tmpwatch 5 / tmp
Run the following command to delete files that have not been modified in the last 10 hours. If you want to use the modification time (mtime) to delete the file, you need to add the-m option to the tmpwatch command.
# tmpwatch-m 10 / home/daygeek/Downloads how to use the tmpwatch command to delete files that have not been accessed for more than "X" days
If you want to use days to delete files, you need to add the suffix d. The following example deletes files for more than 30 days.
# tmpwatch 30d / home/daygeek/Downloads how to use the tmpwatch command to delete all files that have not been accessed for a period of time
The following command deletes all types of files based on modification time (mtime), not just regular files, symbolic links, and directories.
# tmpwatch-am 12 / tmp how to exclude directories in tmpwatch
The following command deletes all files that have not been modified in the past 10 hours and excludes directories.
# tmpwatch-am 10-how nodirs / home/daygeek/Downloads excludes specific paths in tmpwatch
The following command deletes all files that have not been modified in the past 10 hours, except for the folders excluded below.
# tmpwatch-am 10-how exclude=/home/daygeek/Downloads/Movies / home/daygeek/Downloads excludes specific modes in tmpwatch
The following command deletes all files that have not been modified in the past 10 hours, except those that meet the patterns listed below.
# tmpwatch-am 10-- how does exclude-pattern='*.pdf' / home/daygeek/Downloads make tmpwatch commands run empty
If you want to run empty, run the following command.
# tmpwatch-t 5h / home/daygeek/Downloads how to set up cronjob to delete files periodically using tmpwatch
By default, it has a cronjob file in the / etc/cron.daily/tmpreaper directory. The cronjob works according to the configuration file located in / etc/timereaper.conf. You can customize the file as needed.
It runs once a day and deletes files from 7 days ago.
In addition, if you want to perform an operation regularly, you can add a cronjob manually as needed.
# crontab-e 0 10 * / usr/sbin/tmpwatch 15d / home/daygeek/Downloads
The above cronjob will delete files earlier than 15 days at 10:00 every morning.
The above is all the contents of the article "how linux uses Tmpwatch/Tmpreaper to delete old files". Thank you for reading! Hope to share the content to help you, more related 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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.