删除超过3年的文件

时间:2021-12-30 22:51:01

I need to remove any file in the directory that is older than 2 years old. It is very important that I keep the newest files and delete the old files.

我需要删除目录中超过2年的任何文件。保留最新文件并删除旧文件非常重要。

I have searched and found this.

我搜索过并找到了这个。

find /path/to/files* -mtime +365 -exec rm {} \;

Can I just multiply the number?

我可以加倍吗?

find /path/to/files* -mtime +1095 -exec rm {} \;

Is there a way to add a switch that will print the file name to the screen as it removes it? To make sure it is doing what I am expecting?

有没有办法添加一个开关,将文件名打印到屏幕上,因为它删除它?确保它正在做我期待的事情?

I have also found this:

我也发现了这个:

find /rec -mtime +365 -print0 | xargs -0 rm -f

Is there a major difference between the two? Is one better than the other? What I have read says that xargs is faster. Would I be able to multiply the mtime number out to a 2nd or 3rd year?

这两者之间有很大的不同吗?这个比那个好吗?我读过的内容表明xargs更快。我可以将mtime数量乘以第2年或第3年吗?

And finally would would I be able to place the code as it is into a cron job that can run daily?

最后,我能将代码放入可以每天运行的cron作业中吗?

Thank you!

3 个解决方案

#1


5  

Can I just multiply the number?

我可以加倍吗?

find /path/to/files -mtime +1095 -exec rm {} \;

Yes. And to "echo" before you remove

是。并且在你移除之前“回声”

find /path/to/files -mtime +1095 -print

Then the version with -exec rm {} \; to remove the files (when you are ready).

然后用-exec rm {} \的版本;删除文件(准备好后)。

#2


1  

find /path/to/files* -mtime +1095 -exec rm {} \;

That should work fine, you can run a dry a run of this by simply listing the files that are found by the command:

这应该可以正常工作,只需列出命令找到的文件,就可以运行一个干的运行:

find /path/to/files* -mtime +1095 -exec ls {} \;

To be safe though I would also add in a -type to ensure that other things dont get deleted:

为了安全,我还要添加一个-type以确保其他东西不被删除:

find /path/to/files* -type f -mtime +1095 -exec rm {} \;

#3


1  

To answer the second part of your question.

回答你问题的第二部分。

Yes there is a major difference in using -exec or xargs. -exec starts a new process of rm for every file found. This creates a lot of overhead and can seriously slow down Systems if you delete a lot of files.

是的,使用-exec或xargs有很大的不同。 -exec为找到的每个文件启动rm的新进程。这会产生大量开销,如果删除大量文件,可能会严重降低系统速度。

xargs creates only as much rm processes as needed, as it creates a command line containing as much files as possible. So only a few rm processes are created.

xargs根据需要创建尽可能多的rm进程,因为它创建了一个包含尽可能多的文件的命令行。因此,只创建了几个rm进程。

But both are better than -delete, because delete is unsave

但两者都比-delete更好,因为删除是不成功的

#1


5  

Can I just multiply the number?

我可以加倍吗?

find /path/to/files -mtime +1095 -exec rm {} \;

Yes. And to "echo" before you remove

是。并且在你移除之前“回声”

find /path/to/files -mtime +1095 -print

Then the version with -exec rm {} \; to remove the files (when you are ready).

然后用-exec rm {} \的版本;删除文件(准备好后)。

#2


1  

find /path/to/files* -mtime +1095 -exec rm {} \;

That should work fine, you can run a dry a run of this by simply listing the files that are found by the command:

这应该可以正常工作,只需列出命令找到的文件,就可以运行一个干的运行:

find /path/to/files* -mtime +1095 -exec ls {} \;

To be safe though I would also add in a -type to ensure that other things dont get deleted:

为了安全,我还要添加一个-type以确保其他东西不被删除:

find /path/to/files* -type f -mtime +1095 -exec rm {} \;

#3


1  

To answer the second part of your question.

回答你问题的第二部分。

Yes there is a major difference in using -exec or xargs. -exec starts a new process of rm for every file found. This creates a lot of overhead and can seriously slow down Systems if you delete a lot of files.

是的,使用-exec或xargs有很大的不同。 -exec为找到的每个文件启动rm的新进程。这会产生大量开销,如果删除大量文件,可能会严重降低系统速度。

xargs creates only as much rm processes as needed, as it creates a command line containing as much files as possible. So only a few rm processes are created.

xargs根据需要创建尽可能多的rm进程,因为它创建了一个包含尽可能多的文件的命令行。因此,只创建了几个rm进程。

But both are better than -delete, because delete is unsave

但两者都比-delete更好,因为删除是不成功的