How to fix “-bash: /bin/rm: Argument list too long”

Just a few minutes ago my php thumbnail creation script running out of control, causing thousands of thumbnails generated. I’m lucky enough to know this thing fast, a minutes late this script could fill in my server drive to the max.

I FTP-ed to the server, but the FTP software I was using is limiting the number of files I can see. I got this message from the FTP console:
COMMAND:>    LIST -la
150 Accepted data connection
226-Options: -a -l
226 Output truncated to 2000 matches

Well, login via SSH then.

While executing:
rm -fr some_file*

I’m getting:
-bash: /bin/rm: Argument list too long

I just yet to know that rm has such limitation. Correction: its not rm limitation, its limitation inside the kernel Says Google results.

Here is the fix to the problem above:
find . -name 'some_file*' | xargs rm

That should work for the most of you. But not mine, since the thumbnail filename contains spaces. Like: “some_file name 0000000001.jpg”. Thus, I’m getting:
rm: cannot remove `./some_file': No such file or directory
rm: cannot remove `name': No such file or directory
rm: cannot remove `0000000001.jpg': No such file or directory
rm: cannot remove `./some_file': No such file or directory
rm: cannot remove `name': No such file or directory
rm: cannot remove `0000000002.jpg': No such file or directory
rm: cannot remove `./some_file': No such file or directory
rm: cannot remove `name': No such file or directory
rm: cannot remove `0000000003.jpg': No such file or directory
rm: cannot remove `./some_file': No such file or directory
rm: cannot remove `name': No such file or directory
rm: cannot remove `0000000004.jpg': No such file or directory
...

To fix the last problem is:
find . -name 'some_file*' -print0 | xargs -0 rm

Note: please be careful with a script that using loop, do not make it looping indefinitely. This is an example of what could happen.

No Comments

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: