How to Delete Files from Directory When Memory is Exhausted

directory

This question is a logical continuation of How can I delete all files from a directory when it reports "Argument list too long"

I have

drwxr-xr-x  2 doreshkin doreshkin 198291456 Apr  6 21:35 session_data

I tried

find session_data -type f -delete
find session_data -type f | xargs rm -f
find session_data -maxdepth 1 -type f -print0 | xargs -r0 rm -f

The result is the same:

find: memory exhausted

What can I do to remove this directory?

Best Answer

It sounds like a problem with find. I noticed a few bug reports of people getting that error with a specific version of GNU findutils.

You can try replacing "find" with "ls" and "grep". Something like this:

cd somedir
\ls -f | grep "something" | xargs -d "\n" rm

The backslash on \ls instead ls tells bash to ignore any aliases that will affect your output format. You could also say /bin/ls if you forget the backslash trick. The -f option tells it to disable sorting (which saves time/memory) and include hidden files. The -d "\n" argument to xargs tells it split on newlines instead of spaces. Note that -d isn't supported on all versions of xargs, which is a shame.

Note that ls something* won't work, since the something* is expanded in bash, not by ls, and will result in an "argument list too long" error. Thats why you pipe the result through grep.