How to cheat 'Argument list too long' error while using wildcards

The problem is that the linux terminal shell has a limit to the length of the command line arguments.

And this can vary from kernel to kernel basis, depends on the kernel configuration during kernel compilation.

So most of the time this will not be an issue but if your trying to manipulate multiple files in Linux console using the ‘*’ expansion you might run it to this problem. Especially if you are using older Linux systems, or Linux running on old hardware as the argument length might have been shortened during kernel because of lack of memory.

An example command which might generate this error.

$rm *
bash: /usr/bin/rm: Argument list too long

this command should remove all files in the current director, but if the number of files are huge, you will run into argument list too long error.

To make this work u can use one of these two methods.

1. Use find command

To use find you have to make find parse though the file list and execute rm on each file one by one.

To do this the command would be.

$ find ./ -type f -exec rm {} \;

here rm is the command and {} is the parameter to the command, in find {} means the current file name.

2. Use for loop

The syntax of for can vary form shell to shell. But a generic sh syntax would be.

$ for file in $(ls -1);  do  {  rm $file } ; done

In this the $file variable contains the file name taken from the $(ls -1 ) list which the for loop loops though, and this $file variable is passed to the rm to remove.

Hope that this helps all of you who encounter this issue a lot.

  1. very Helpful but i still need u there when i do this….

Leave a Comment

NOTE - You can use these HTML tags and attributes:

