TranslateProject/sources/tech/20200525 How to Remove Files Older than N Days Using Tmpwatch-Tmpreaper on Linux.md
2020-05-29 08:53:26 +08:00

7.7 KiB
Raw Blame History

How to Remove Files Older than N Days Using Tmpwatch/Tmpreaper on Linux

You may have missed deleting files that are no longer needed on your computer in some directory.

This can be “Download” or any other directory.

It may have grown up over a period of time.

If you have enough storage, you should remove them, as this will slow down your system when you list files.

Also, it can be clumsy when you have thousands of files in one directory.

It is very difficult to find a file in a specific directory when you do not know the file name you want to check.

We can do this by using the find command with some combination, and we have written an article about this in the past.

Today we are going to show you how to achieve this using the Tmpwatch utility on Linux.

What is Tmpwatch

Tmpwatch recursively removes files that have not been accessed for a specified period of time in the specified directories.

Typically, it is used to automatically clean directories used for temporary file systems, such as / tmp and /var/tmp.

It only remove empty directories, regular files, and symbolic links.

It doesnt switch to other file systems, and avoids the “lost+found” directory belonging to the root user.

By default, tmpwatch deletes files based on their atime (access time), not their mtime (conversion time).

You can change this behavior by adding other parameters in the tmpwatch command.

WARNING: Please do not run “tmpwatch” or “tmpreaper” in “/” because there is no mechanism in the program to protect against this.

How to Install Tmpwatch on Linux

Tmpwatch can be installed as follows from the distribution official repository.

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 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

Make a note: If you are using Debian-based systems, use “tmpreaper” instead of tmpwatch. All examples will work as expected.

Understanding Key Options and Arguments

  • atime (File Last Access Time) Access time shows the last time the data from a file was accessed by any of the process such as command or script, etc,.
  • mtime (File Last Modify Time) mtime shows when you modify a file contents or save a file. Most of the times ctime and mtime will be the same, unless the file attributes are updated.
  • ctime (File Last Change Time) ctime shows when your file metadata got changed. It means when the file attributes are changed like ownership or group, etc,.
  • dirmtime (Directory Last modification time) dirmtime shows when your directory last modified.

The time parameter defines the threshold for removing files.

  • d for days
  • h for hours
  • m for minutes
  • s for seconds

How to Removes Files That Havent Been Accessed for a Period of Time Using the Tmpwatch Command

As I said at the beginning of the article, Tmpwatch deletes files by default (atime) depending on the time of access to the files. Also, since hours are the default parameter, you do not need to add the suffix to time if the action is performed using the hour unit.

For example, run the command below to recursively remove files that have not been accessed for the past 5 hours.

# tmpwatch 5 /tmp

Run the command below to delete files that have not been modified for the last 10 hours. If you want to delete files using mtime, you need to add the “-m” option with the tmpwatch command.

# tmpwatch -m 10 /home/daygeek/Downloads

How to Delete Files That Havent Been Accessed more than “X” Days Using the Tmpwatch Command

If you want to delete files using days, you need to add the suffix “d”. The example below deletes files older than 30 days.

# tmpwatch 30d /home/daygeek/Downloads

How to Delete All Files That Havent Been Accessed for a Period of Time Using the Tmpwatch Command

The below command removes all file types, not just regular files, symbolic links and directories based on mtime.

# tmpwatch -am 12 /tmp

How to Exclude a Directory with Tmpwatch

The below command will delete all files and excludes directories that havent been modified for the past 10 hours.

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

How to Exclude a Specific Path with Tmpwatch

The command below will delete all files except the directory below which has not been modified for the past 10 hours.

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

How to Exclude Specific Pattern with Tmpwatch

The command below will delete all files except the Pattern below which has not been modified for the past 10 hours.

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

How to Perform Dry Run with Tmpwatch Command

Run the below command if you want to perform dry run.

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

How to Setup a Cronjob to Delete files Periodically Using Tmpwatch

By default it leaves a cronjob file under the “/etc/cron.daily/tmpreaper” directory. This cronjob works according to the configuration file located in “/etc/timereaper.conf”. You can customize the file according to your needs.

It runs once a day and deletes files older than 7 days.

Alternatively, if you would like to perform an action routinely, you can manually add a conjob based on your needs.

# crontab -e

0 10 * * * /usr/sbin/tmpwatch 15d /home/daygeek/Downloads

The above cronjob will delete files that are older than 15 days daily at 10AM.


via: https://www.2daygeek.com/how-to-remove-files-older-than-n-days-using-tmpwatch-tmpreaper-on-linux/

作者:Magesh Maruthamuthu 选题:lujun9972 译者:译者ID 校对:校对者ID

本文由 LCTT 原创编译,Linux中国 荣誉推出