0
votes

I am in a situation where I have the /tmp directory having atleast 25,000 - 50,000 directories in it. I am trying to use the following command to delete the directories which are older than 2 days in that directory.

find /path/to/tmp/* -type d -ctime +2 -delete

But I keep running into the error that the argument list is too long. How can I specifically limit the number of directories being deleted? I tried using the maxdepth 1 option as well and that didn't seem to work.

1

1 Answers

-1
votes

The * glob causes the shell to expand all of the directory names out and pass them to find. Get rid of it and let find do the work rather than the shell.

find /path/to/tmp/ -mindepth 1 -type d -ctime +2 -delete

-mindepth 1 ensures that find will only process subdirectories and not /path/to/tmp itself.

If these directories aren't empty you'll need to switch -delete to rm -r.

find /path/to/tmp/ -mindepth 1 -type d -ctime +2 -exec rm -r {} +