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

LinuxFile IoResource Cleanup

Linux Problem Overview


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?

Linux Solutions


Solution 1 - Linux

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

Solution 2 - Linux

Can also use:

find . -mindepth 1 -mtime +3 -delete

To not delete target directory

Solution 3 - Linux

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionEdward TanguayView Question on Stackoverflow
Solution 1 - Linuxhd1View Answer on Stackoverflow
Solution 2 - LinuxvangheemView Answer on Stackoverflow
Solution 3 - Linux62mkvView Answer on Stackoverflow