Linux “参数列表太长”时如何删除所有超过 3 天的文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14731133/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 18:57:00  来源:igfitidea点击:

How to delete all files older than 3 days when "Argument list too long"?

linuxfile-ioresource-cleanup

提问by Edward Tanguay

I've got a log file directory that has 82000 files and directories in it (about half and half).

我有一个日志文件目录,里面有 82000 个文件和目录(大约一半)。

I need to delete all the file and directories which are older than 3 days.

我需要删除所有超过 3 天的文件和目录。

In a directory that has 37000 files in it, I was able to do this with:

在一个包含 37000 个文件的目录中,我能够做到这一点:

find * -mtime +3 -exec rm {} \;

But with 82000 files/directories, I get the error:

但是对于 82000 个文件/目录,我收到错误消息:

/usr/bin/find: Argument list too long

/usr/bin/find: 参数列表太长

How can I get around this error so that I can delete all files/directories that are older than 3 days?

我怎样才能解决这个错误,以便我可以删除所有早于 3 天的文件/目录?

采纳答案by hd1

To delete all files anddirectories within the currentdirectory:

要删除所有文件,并在目录内的当前目录:

find . -mtime +3 | xargs rm -Rf

Or alternatively, more in line with the OP's original command:

或者,更符合 OP 的原始命令:

find . -mtime +3 -exec rm -Rf -- {} \;

回答by vangheem

Can also use:

还可以使用:

find . -mindepth 1 -mtime +3 -delete

To not delete target directory

不删除目标目录

回答by 62mkv

Another solution for the original question, esp. useful if you want to remove only SOME of the older files in a folder, would be smth like this:

原始问题的另一个解决方案,尤其是。如果您只想删除文件夹中的一些旧文件,这很有用,就像这样:

find . -name "*.sess" -mtime +100 

and so on.. Quotes block shell wildcards, thus allowing you to "find" millions of files :)

等等.. 引用阻止 shell 通配符,从而允许您“找到”数百万个文件:)