Friday, May 3, 2013

Argument list too long error for rm (linux remove command)

...Via StackOverflow...
    thanks to Dennis and j0k

The reason this occurs is because bash actually expands the asterisk to every matching file, producing a very long command line.

Try this:
    find . -name "*.pdf" -print0 | xargs -0 rm

Warning: this is a recursive search and will find (and delete) files in subdirectories as well. Tack on -f to the rm command only if you sure you don't want confirmation.

If you're on Linux, you can do the following to make the command non-recursive:
find . -name "*.pdf" -maxdepth 1 -print0 | xargs -0 rm