Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 686
Edward Tanguay Avatar asked Feb 06 '13 14:02

Edward Tanguay


People also ask

How do I delete files older than 10 days?

To delete files older than 10 days in Windows 11 or Windows 10, you can use the ForFiles command. First, open the Command Prompt with administrator rights. Then, enter this command: ForFiles /p “folder-path” /s /d -10 /c “cmd /c del /q @file”. It will remove all the files older than 10 days only.

How do you delete files from older than 30 days?

To delete files older than 30 days on Windows 10, you can use the ForFiles tool. Use this command: ForFiles /p “C:\path\to\folder” /s /d -30 /c “cmd /c del /q @file”. Change “30” for the number of days you want and the folder path.


2 Answers

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 -- {} \; 
like image 182
hd1 Avatar answered Sep 25 '22 02:09

hd1


Can also use:

find . -mindepth 1 -mtime +3 -delete 

To not delete target directory

like image 45
vangheem Avatar answered Sep 24 '22 02:09

vangheem