Argument list too long error when passing parameters under Linux

Source: Internet
Author: User
Tags command line requires advantage

This article is a few days ago in the forum a friend raised the question, today is free, sorted out, released for your reference!

When Linux tries to pass too many parameters to a system command (LS *; CP *; RM *; cat *; etc) , the "Argument list too long" error appears. This article provides 4 solutions that are arranged in a low to high degree of complexity.

Method #: Manually dividing command line arguments into smaller parts

Example 1

MV [a-l]*. /directory2

MV [m-z]*. /directory2

This is the simplest of the 4 methods, but far from ideal. You must have the means to split the file evenly, and for a large number of files, you need to enter the n command.

Method: Using the Find command

Example 2

Method 2 through the Find command to filter the list of files, to pass the required files to a series of commands.

The advantage is that the find command has very powerful filtering capabilities, and, perhaps most importantly, this method requires only 1 lines of command.

The only drawback is that method 2 requires traversing the file, and therefore takes more time.

Method #3: Creating a function

Example 3

#!/bin/bash

# Set the folder you want to delete

Rm_dir= '/data/files '

CD $RM _dir

For I in ' ls '

Todo

Rm-f $I

Done

Method #4: Recompile the Linux kernel

The last method requires 2 words: cautious, this method is very advanced, so inexperienced Linux users are best not to try. In addition, be sure to test thoroughly in your system environment before you use it permanently.

Method 4 only needs to manually increase the number of pages assigned to command-line arguments in the kernel. Open the Include/linux/binfmts.h file, with the following lines in place near the beginning of the file:

/*

* Max_arg_pages defines the number of PAGES allocated for arguments

* and envelope for the new program. Should suffice, this gives

* A maximum env+arg of 128kB w/4kb pages!

*/

#define Max_arg_pages 32

In order to increase the allocation of the command line parameters of the memory, only need to assign to max_arg_pages a larger value, save, recompile, install, reboot, fix

In my system, I've increased the value of Max_arg_pages to 64, which solves all the problems. After changing this value, I have not encountered any problems. This is understandable when the max_arg_pages is changed to 64 and the longest parameter line takes up only 256KB of system memory – nothing for the current hardware standard.

The advantage of Method 4 is obvious, now you just run the command as usual. It is also obvious that if the memory allocated to the command line is greater than the available system memory, it can cause a denial of service attack (DoS attack) on the system itself, causing the system to crash. Especially for multi-user systems, even a small increase in memory allocations can have a significant impact because each user is allocated additional memory. So be sure to fully test to determine if your system can use Method 4.

This article is from the "Technology Achievement Dream" blog, please be sure to keep this source http://ixdba.blog.51cto.com/2895551/526428

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.