89

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?

Mike
  • 397
  • 2
  • 16
Edward Tanguay
  • 189,012
  • 314
  • 712
  • 1,047

3 Answers3

123

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 -- {} \;
Chris W.
  • 1,680
  • 16
  • 35
hd1
  • 33,938
  • 5
  • 80
  • 91
61

Can also use:

find . -mindepth 1 -mtime +3 -delete

To not delete target directory

vangheem
  • 3,293
  • 17
  • 18
  • 4
    Best answer, much cleaner than calling rm (and probably safer). Works for subdirectories as well. – basic6 May 25 '15 at 16:34
17

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

62mkv
  • 1,444
  • 1
  • 16
  • 28