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

Edward Tanguay picture Edward Tanguay · Feb 6, 2013 · Viewed 187.7k times · Source

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

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

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

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

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

/usr/bin/find: Argument list too long

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

Answer

hd1 picture hd1 · Feb 6, 2013

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 -- {} \;