Argument list too long

If you are not making a living by teaching Linux in school, you’ll definitely encounter this error sooner or later in your daily work. The error is due to the limited command line length.i.e, the * in your command expands to a large number of files which makes the command line exceed the length limitation. You can easily find a solution to this, like this one:

Unfortunately, the command does not work because the first line of the output of the find command is the folder2 itself so the mv command will move the whole folder to folder.  I bet the author did not even try it himself. The correct command to mv a large number of files is:

The command looks a little strange. Let me explain it to you.  The find command looks for “*.*” files in folder2 . For each file it finds, call the mv command replacing {} with the file name. Note that there is no space in between {}. “-exec”, “mv”, “{}”, “folder”, “;” are all parameters of the find command like “folder2″ ,”-name”, “*.*” , “-type” and “f”.  The “;” parameter tells the find command to take the stuff between -exec and “;” as a command to execute. Without “;” and if there are other parameters for the find following, it won’t tell if they are the parameters of the find, or the parameters of the command to be executed.

Other solutions for the “Argument list too long” problem would use the xargs command like this which can delete a large number of files.

As usual, they do not tell you the exact meaning of each option. To understand what the command means, you should master the usage of xargs first.  The xargs command reads from stdin then executes a command  and takes what it reads from the input as the parameter(s) of that command. If there’re too many characters read from the input considering the maximum command line length, xargs will separate the input and construct multiple command lines each of which is less than the maximum length. The spaces or newlines in the input would delimit the input to multiple parameters of the command to be executed.  Knowing that, we can understand what the above command means. The find command prints the file names to the stand output from which xargs reads as stand input. By default, xargs uses blank and newline to delimit the input to parameters. If the file name contains space or newlines, it will be converted to several parameters to feed the rm command, which is not what we want.   The “-0″ option tell xargs to delimit the input using null character, while “-print0″ option tells the find command to separate the output with null character instead of normal newline character. The “-0″ and the “-print0″ corporate with each other to handle with special characters in file names.



Posted in tips of hosting