Argument list too long

Any of you encountered “Argument list too long” problem when trying to mass delete files in a folder? Today I want to erase more than a million files in a folder. So I ran “rm -rf *.jpg” and the “Argument list too long” error was returned. What the hell???

So I quickly google this Argument bla bla bla thinggy.

This is what I understand so far:

The reason for this annoyance is that Linux kernel has a limitation of bytes it can process through as arguments in exec() commands. [http://netweblogic.com/linux/linux-commands-argument-list-too-long/]

The system could not handle the number of arguments given to a command or program when it combined those arguments with the environment’s exported shell variables. The argument list limit is the size of the argument list plus the size of the environment’s exported shell variables.

The easiest solution is to reduce the size of the parent process environment by unsetting extraneous environment variables. (See the man page for the shell you’re using to find out how to list and change your environment variables.) Then run the program again.

An argument list longer than ARG_MAX bytes was presented to a member of the exec() family of system calls.

Moral of the story: Everythings has its own limitation. 😉

How to solve this?

According to Alessandre S. Naro in “Argument list too long”: Beyond Arguments and Limitations, he points out 4 ways to overcome this problem.

Method #1: Manually split the command line arguments into smaller bunches.
Method #2: Use the find command.
Method #3: Create a function. and
Method #4: Recompile the Linux kernel.

So I choose method #2. Easier and faster. You can run this in single command. Example:

find /opt/library/images/bookcovers_tiny/ -name ‘*.jpg’ -print0 | xargs -0 rm -f

Done. All files deleted. Yeay!!

 

Leave a Reply

Your email address will not be published. Required fields are marked *

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