使用Cron删除超过30分钟的文件夹

时间:2021-02-14 01:44:10

I would like to know what job should I insert in Cron so that every folder that is older than 30 minutes gets deleted.

我想知道我应该在Cron中插入什么作业,以便删除超过30分钟的每个文件夹。

I tried that:

我试过了:

# 30 * * * * find /path/to/my/folder -type f -mmin +30 -exec rm -fr -maxdepth 0 {} \;

But it doesn't seem to do anything.

但它似乎没有做任何事情。

There are other posts here on *, but they all talk about removing files, which is cool but I also want to remove the folder in itself, as well as its content.

*上还有其他帖子,但他们都谈论删除文件,这很酷但我也想删除文件夹本身及其内容。

4 个解决方案

#1


0  

I assume that you think about the directory. Then change the -type f options to -type d

我假设您考虑目录。然后将-type f选项更改为-type d

#2


0  

tmpwatch (most RedHat distro) package will do.

tmpwatch(大多数RedHat发行版)包都可以。

DESCRIPTION tmpwatch recursively removes files which haven’t been accessed for a given time. Normally, it’s used to clean up directories which are used for temporary holding space such as /tmp.

描述tmpwatch以递归方式删除在给定时间内未访问过的文件。通常,它用于清理用于临时存放空间的目录,例如/ tmp。

tmpwatch -umc 30m --nodirs notThisDir /path/to/directory/ThatIwantToClean

More in man tmpwatch. This package provides a cron example in /etc/cron.daily/tmpwatch. You can modify the script to fit to your needs and put the cron in /etc/cron.hourly/. Something like the following:

更多男人tmpwatch。这个包在/etc/cron.daily/tmpwatch中提供了一个cron示例。您可以修改脚本以满足您的需要,并将cron放在/etc/cron.hourly/中。类似于以下内容:

 $ cat /usr/local/bin/cleanfolder
 #!/bin/bash

 flags="-umcvv"
 /usr/sbin/tmpwatch "${flags}" 30m /my/folder

Change permission:

$ chmod a+x /usr/local/bin/cleanfolder

In /etc/cron.d/cleanfolder-cron, put it like this:

在/etc/cron.d/cleanfolder-cron中,把它放在这样:

30 * * * * root /usr/local/bin/cleanfolder

Reload your cron service.

重新加载您的cron服务。

#3


0  

I actually found out that the best method is to divide the commands in Cron in 2 parts, and use the -delete argument

我实际上发现最好的方法是将Cron中的命令分成两部分,并使用-delete参数

Code

30 * * * * sudo find /my/folder/* -type f -mmin +30 -delete && sudo find /my/folder/* -type d -empty -mmin +30 -delete 

Explanations

30 * * * *: execute every 30mn all the time
sudo find /my/folder/* -type f -mmin +45 -delete : delete all files and subfiles that are older than 45 minutes
&& : do only if first command has successfully run
sudo find /my/folder/* -type d -empty -mmin +45 -delete : delete all empty folders that are older than 45 minutes

30 * * * *:每次执行30m sudo find / my / folder / * -type f -mmin +45 -delete:删除超过45分钟的所有文件和子文件&&:仅当第一个命令成功时才执行运行sudo find / my / folder / * -type d -empty -mmin +45 -delete:删除超过45分钟的所有空文件夹

Working on Ubuntu 16.04

在Ubuntu 16.04上工作

#4


-1  

From man pages ... find . -type d -empty -delete - Delete all empty directories. rm - Remove files (rm -rf will recursively remove folders and their contents).

从手册页...找到。 -type d -empty -delete - 删除所有空目录。 rm - 删除文件(rm -rf将递归删除文件夹及其内容)。

...should do it.

......应该这样做。

#1


0  

I assume that you think about the directory. Then change the -type f options to -type d

我假设您考虑目录。然后将-type f选项更改为-type d

#2


0  

tmpwatch (most RedHat distro) package will do.

tmpwatch(大多数RedHat发行版)包都可以。

DESCRIPTION tmpwatch recursively removes files which haven’t been accessed for a given time. Normally, it’s used to clean up directories which are used for temporary holding space such as /tmp.

描述tmpwatch以递归方式删除在给定时间内未访问过的文件。通常,它用于清理用于临时存放空间的目录,例如/ tmp。

tmpwatch -umc 30m --nodirs notThisDir /path/to/directory/ThatIwantToClean

More in man tmpwatch. This package provides a cron example in /etc/cron.daily/tmpwatch. You can modify the script to fit to your needs and put the cron in /etc/cron.hourly/. Something like the following:

更多男人tmpwatch。这个包在/etc/cron.daily/tmpwatch中提供了一个cron示例。您可以修改脚本以满足您的需要,并将cron放在/etc/cron.hourly/中。类似于以下内容:

 $ cat /usr/local/bin/cleanfolder
 #!/bin/bash

 flags="-umcvv"
 /usr/sbin/tmpwatch "${flags}" 30m /my/folder

Change permission:

$ chmod a+x /usr/local/bin/cleanfolder

In /etc/cron.d/cleanfolder-cron, put it like this:

在/etc/cron.d/cleanfolder-cron中,把它放在这样:

30 * * * * root /usr/local/bin/cleanfolder

Reload your cron service.

重新加载您的cron服务。

#3


0  

I actually found out that the best method is to divide the commands in Cron in 2 parts, and use the -delete argument

我实际上发现最好的方法是将Cron中的命令分成两部分,并使用-delete参数

Code

30 * * * * sudo find /my/folder/* -type f -mmin +30 -delete && sudo find /my/folder/* -type d -empty -mmin +30 -delete 

Explanations

30 * * * *: execute every 30mn all the time
sudo find /my/folder/* -type f -mmin +45 -delete : delete all files and subfiles that are older than 45 minutes
&& : do only if first command has successfully run
sudo find /my/folder/* -type d -empty -mmin +45 -delete : delete all empty folders that are older than 45 minutes

30 * * * *:每次执行30m sudo find / my / folder / * -type f -mmin +45 -delete:删除超过45分钟的所有文件和子文件&&:仅当第一个命令成功时才执行运行sudo find / my / folder / * -type d -empty -mmin +45 -delete:删除超过45分钟的所有空文件夹

Working on Ubuntu 16.04

在Ubuntu 16.04上工作

#4


-1  

From man pages ... find . -type d -empty -delete - Delete all empty directories. rm - Remove files (rm -rf will recursively remove folders and their contents).

从手册页...找到。 -type d -empty -delete - 删除所有空目录。 rm - 删除文件(rm -rf将递归删除文件夹及其内容)。

...should do it.

......应该这样做。