1

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?

codeholic
  • 134

6 Answers6

2

Piping find results through head seems to work for me (I have a similar problem where security camera shots from 6 cameras got uploaded once a minute)

find . -type f | head -1000 | xargs rm

If it does, loop it:

for i in {1..999}
do
   find . -type f | head -1000 | xargs rm
done

Replace 999 with how many thousands of files are in there (if you know).

fsckin
  • 583
2

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.

tylerl
  • 15,155
0

Try using ls instead of find (ls -1 | xargs rm).

Or

Use a for loop over the output of ls

#!/bin/sh
for i in `ls -1`; do
  rm $i
done

(in both those cases if you're still having trouble the shell may be hitting a memory ceiling: try piping ls through head to shorten the list)

Or

Write it in perl/C/etc. (iterating over readdir's output & garbage-collecting as you go)

...there are probably more "Or" cases, but these spring immediately to mind.

voretaq7
  • 80,391
  • See also the answers to your original question (which detail a whole bunch of other great alternatives besides the ones I list above) – voretaq7 Apr 06 '10 at 18:40
0

I wonder what a recent version of rsync would do with a empty directory as the source and the --delete option...

Kyle Brandt
  • 84,369
0
find . -type f -print | awk '{ print "rm \"" $1 "\""} | sh
0

Just run rm as part of your find command. No need for pipes/xargs/fancy printing.

find . -exec rm -f {} +