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

我有一个日志文件目录,其中包含82000个文件和目录(大约一半和一半)。

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

在一个有37000个文件的目录中,我可以使用:

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

但是对于82000个文件/目录,我得到的错误是:

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

我如何避免这个错误,以便我可以删除所有的文件/目录超过3天?

192477 次浏览

To delete all files and directories within the current directory:

find . -mtime +3 | xargs rm -Rf

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

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

Can also use:

find . -mindepth 1 -mtime +3 -delete

To not delete target directory

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