SoFunction
Updated on 2024-10-28

Four solutions for "Argument list too long".

Four solutions to "Argument list too long".

When deleting a large number of files in linux, using rm directly results in the error: -bash: /bin/rm: argument list too long.

This can be combined with the find command.

Example:
1. rm * -rf should read.

find . -name "*" | xargs rm -rf '*' That's it.

2. rm test* -rf Change to.

find . -name "test*" | xargs rm -rf "test*"

The list of parameters for the mv times is too long.

for i in *.m;do mv $i ${i%.m};done

So resorting to google, the exploration process is omitted, let's go straight to the solution:

ls dir1 | xargs -t -I {} mv {} dir2/{}

The pair of curly braces here is the one used in the example given in the original article, and when I looked at the usage of the parameter later, the pair of curly braces can actually be replaced with any string, for example:

ls dir1 | xargs -t -I asdf mv asdf dir2/asdf

The effect is exactly the same as the version with the curly brackets, it just looks a little less serious.

It should be noted that the second parameter of the above xargs is an uppercase i, pronounced as "love", not a lowercase L. As for the meaning of the parameter, I forget.

Linux Error "Command argument list is too long", failed to move more than 30,000 files at a time with the mv command, the original command looked like this: "mv $(ls dir1) dir2". The error message was centered on the idea that "you're taking too many arguments".

Following LZ's idea you can probably do this: find /dir1/ -maxdepth 1 | xargs -i mv {} /dir2/
If the parameter is too long, it is simpler to use tar

tar -C /dir1/ -cf - . | tar -xf - -C /dir2/

So resorting to google, the exploration process is omitted, let's go straight to the solution:

ls dir1 | xargs -t -I {} mv {} dir2/{}

The pair of curly braces here is the one used in the example given in the original article, and when I looked at the usage of the parameter later, the pair of curly braces can actually be replaced with any string, for example:

ls dir1 | xargs -t -I asdf mv asdf dir2/asdf

The effect is exactly the same as the version with the curly brackets, it just looks a little less serious.

It should be noted that the second parameter of the above xargs is an uppercase i, pronounced as "love", not a lowercase L. As for the meaning of the parameter, I forget.

The other 4 methods

As a linux user/system administrator, there are times when you may encounter the following error message.

[user@localhost foo]$ mv * ../foo2

bash: /bin/mv: Argument list too long

The "Argument list too long" error often occurs when the user provides too many arguments in a simple command line, often in ls *, cp *, rm *, etc.
Depending on the cause of the problem four methods are provided below, which can be used at your own discretion

Method 1 : Manually divide file groups into smaller combinations
1:

[user@localhost foo]$ mv [a-l]* ../foo2

[user@localhost foo]$ mv [m-z]* ../foo2

This is the most basic method, simply make the number of parameters to meet the requirements, this method has a limited scope of application, only applies to the list of files in the name of the distribution of a more uniform, in addition, this is a beginner can consider the solution, but it requires a lot of repetition of the command and the distribution of the file name of the observation and guessing.

Method 2 : Use the find command
2:

[user@localhost foo]$ find $foo -type f -name '*' -exec mv {}$foo2/. \;

Method 2 outputs a list of files to the mv command through the find command so that it processes them one at a time, which completely avoids the existence of excessive parameters. In addition, with different parameters, it is possible to specify matching patterns such as timestamps, permissions, and inodes in addition to names.
The disadvantage of method 2 is that it is more time consuming.

Method 3 : Creating a shell function
3.1:

function huge_mv ()
{whileread line1; do
mv foo/$line1 ../foo2
done
}
ls -1 foo/ | huge_mv

Writing a shell function does not involve some degree of complexity, and this approach is more flexible than methods 1 and 2.
Let's expand on Example 3.1 .
3.2:

function huge_mv ()
{whileread line1; do
md5sum foo/$line1 >> ~/md5sums
ls -l foo/$line1 >> ~/backup_list
mv foo/$line1 ../foo2
done
}
ls -1 foo/ | huge_mv

Compared to Example 3.1, Example 3.2 generates an md checksum hash file and a name backup of the file, in line with the philosophy of leaving yourself a way out.
In addition, you can extend the functionality without limitations according to your needs.

Method 4 : Ultimate solution, recompile the kernel

First of all, be careful before using this solution, because it involves modifying the kernel source code, you still need to exercise discretion and test it well in a production environment.
Plus, this method gets to the root of the problem, once and for all
This is one of the benefits of open source
First find the include/linux/ file in the kernel source code and search for the following fields.

/*
* MAX_ARG_PAGES defines the number of pages allocated for arguments
* and envelope for the new program. 32 should suffice, this gives
* a maximum env+arg of 128kB w/4KB pages!
*/
#define MAX_ARG_PAGES 32

Changing the value of MAX_ARG_PAGES to 64 or higher is a good solution to the parameter limitation problem.
Then just recompile and enable the new kernel.

forward from

/cpuramdisk/item/5aa49ce00c0757aecf2d4f24

Supplementary: Linux "Argument list too long" solution

When I try to execute the following command, it reports an error:

mv train2014/* coco_train2014/
-bash: /bin/mv: Argument list too long

This is because the parameters of these commands are too long, i.e. the number of files is too high. Where the train2014 folder is full of .jpg format image files The solution is as follows:

find train2014/ -name "*.jpg" | xargs -i mv {} coco_train2014/

The same can be done with cp, ls, etc. in the same way:

Copy:

find train2014/ -name "*.jpg" | xargs -i cp {} coco_train2014/

Delete:

find train2014/ -name "*.jpg" | xargs -i rm {} 

Reference:

/2017/11/23/2017-11-23-Linux%E4%B8%AD%E2%80%9CArgument%20list%20too%20long%E2%80%9D%E8%A7%A3%E5%86%B3%E6%96%B9%E6%B3%95/

to this article on the "Argument list too long" parameter list too long four solutions to this article, more related Argument list too long content please search for my previous articles or continue to browse the following related articles I hope that you will support me in the future!