Argument list too long" error solution summary

  
 

When Linux tries to pass too many arguments to a system command (ls *; cp *; rm *; cat *; etc…), an "Argument list too long" error occurs. This article will provide four solutions, arranged in order of complexity from low to high.

Method #1: Manually divide command line parameters into smaller parts

Example 1mv [al]* ../directory2

mv [mz]* ../Directory2

This is the simplest of the four methods, but far from ideal. You must have a way to split the file evenly, and for a very large number of files, you need to enter the N-pass command.

Method #2: Use the find command

Example 2 Method 2 Use the find command to filter the file list and pass the required files to a series of commands.

The advantage is that the find command has a very powerful filtering function, and, perhaps most importantly, this method requires only one line of commands.

The only downside is that Method 2 needs to traverse the file, so it takes a lot of time.

Method #3: Create function

Example3

#!/bin/bash

# Set the folder to be deleted RM_DIR='/Data/files'

cd $RM_DIRfor I in `ls`dorm -f $Idone

Method #4: Recompile the Linux kernel

The last method requires 2 words : Be cautious, this method is very advanced, so it is best not to try linux users without experience. In addition, be fully tested in the system environment before permanent use.

Method 4 only needs to manually increase the number of pages in the kernel that are assigned to command line arguments. Open the include/linux/binfmts.h file and have the following lines near the beginning of the file:

/*

* MAX_ARG_PAGES defines the number of pages allocated for arguments

* a maximum env+arg of 128kB w/4KB pages!

*/

#define MAX_ARG_PAGES 32

In order to increase the memory of the allocation line command line parameters, you only need to assign a larger value to MAX_ARG_PAGES, save, recompile, install, restart, get

in my system I increased the value of MAX_ARG_PAGES to 64, which solved all the problems. After changing this value, I have not encountered any problems. This is understandable. When MAX_ARG_PAGES is changed to 64, the longest parameter line occupies only 256KB of system memory – it is nothing for the current hardware standard.

The advantages of Method 4 are obvious, now you just have to run the command as usual. The disadvantage is also obvious. If the memory allocated to the command line is larger than the available system memory, it may cause a DoS attack on the system itself, causing the system to crash. Especially for multi-user systems, even a small increase in memory allocation can have a big impact because each user is allocated extra memory. So be sure to test it thoroughly to determine if your system can use Method 4.

Copyright © Windows knowledge All Rights Reserved