In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces linux batch operation prompt Argument list too long how to do, the article is very detailed, has a certain reference value, interested friends must read it!
The first situation:
In the process of actual use, if there are too many files or folders in a directory, Argument list too long will be prompted when executing the "*" command, such as under the rm-rf / tmp/* command.
Solution:
Ls | xargs n 10 rm rf ls
SSH executes the above command to output all file names (separated by spaces) xargs is to output ls, every 10 as a group (separated by spaces), as a parameter of rm rf, that is to say, all file names are grouped into a group and deleted by rm rf.
The second situation:
Rm, cp and mv are commonly used file processing instructions under unix. When we need to delete a large number of log files, this message will appear if there are too many files [/ bin/rm: Argument list too long]
The solution is as follows:
For example, to delete / tmp/*.log
Then execute [ls / tmp/*.log | xargs rm-f] so that you can play ^ _ ^, and other cp,mv will do the same!
The third situation:
Today, you need to delete all the files in the / tmp directory. There are a large number of files.
Ls-lt / tmp | wc-l 385412
After using rm *, the system prompts an error Argument list too long
The reason is that under linux, trying to pass too many parameters to a system command (ls *; cp *; rm *; cat *; etc..) An Argument list too long error occurs.
The solution is as follows:
Use find-exec to traverse, and then perform the deletion.
Sudo find / tmp-type f-exec rm {}\
When deleting a large number of files in linux, using rm directly will cause the error of:-bash: / bin/rm: the parameter list is too long.
At this point, you can use the find command in combination.
Example:
1. Change rm *-rf to:
Find. -name "*" | xargs rm-rf'*'is fine.
2. Change rm test*-rf to:
Find. -name "test*" | xargs rm-rf "test*"
Mv Times parameter list is too long
For i in * .mtredo mv $I ${I% .m}; done
So turn to google, and the exploration process is omitted, so go straight to the solution:
Ls dir1 | xargs-t-I {} mv {} dir2/ {}
The pair of curly braces here is used in the example given in the original text. Later, I looked at the use of parameters. In fact, the pair of curly braces can be replaced with any string, such as:
Ls dir1 | xargs-t-I asdf mv asdf dir2/asdf
The effect is exactly the same as the curly braces version, except that it looks a little unserious.
It is important to note that the second parameter of xargs above is the uppercase I, the letter pronounced "love", not the lowercase L. As for the meaning of the parameters, I forgot.
Linux reported an error "command parameter list is too long" and 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 central idea of the error prompt is: "you have too many TM parameters."
According to LZ's idea, you can probably do this: find / dir1/-maxdepth 1 | xargs-I mv {} / dir2/
If the parameters are too long, it is easier to use tar.
Tar-C / dir1/-cf -. | | tar-xf-C / dir2/ |
So turn to google, and the exploration process is omitted, so go straight to the solution:
Ls dir1 | xargs-t-I {} mv {} dir2/ {}
The pair of curly braces here is used in the example given in the original text. Later, I looked at the use of parameters. In fact, the pair of curly braces can be replaced with any string, such as:
Ls dir1 | xargs-t-I asdf mv asdf dir2/asdf
The effect is exactly the same as the curly braces version, except that it looks a little unserious.
It is important to note that the second parameter of xargs above is the uppercase I, the letter pronounced "love", not the lowercase L. As for the meaning of the parameters, I forgot.
The other four methods
As a linux user / system administrator, you will sometimes encounter the following error prompt:
[user@localhost foo] $mv *.. / foo2
Bash: / bin/mv: Argument list too long
Too long "Argument list too long" parameter list errors often occur when the user provides too many parameters in a simple command, often in ls *, cp *, rm *, and so on.
According to the cause of the problem, here are four methods that you can choose according to your own situation.
Method 1: manually divide the file group into smaller combinations
E.g 1:
[user@localhost foo] $mv [Amurl] *.. / foo2
[user@localhost foo] $mv [Mmurz] *.. / foo2
This is the most basic method, but simply to make the number of parameters meet the requirements. This method has a limited scope of application and is only suitable for the uniform distribution of names in the file list. In addition, this is a solution that beginners can consider. However, it requires a lot of repetitive commands and observations and guesses about the distribution of file names.
Method 2: use the find command
E.g 2:
[user@localhost foo] $find $foo-type f-name'*'- exec mv {} $foo2/. \
Method 2 outputs the file list to the mv command through the find command to process one at a time, which completely avoids the existence of excessive parameters. In addition, through different parameters, you can specify matching patterns such as timestamps, permissions, and inode in addition to the name.
The disadvantage of method 2 is that it is more time-consuming.
Method 3: create the shell function
E.g 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, which is more flexible than method 1 and method 2.
Let's expand example 3.1:
E.g 3.2:
Function huge_mv () {whileread line1; domd5sum foo/$line1 > > ~ / md5sumsls-l foo/$line1 > > ~ / backup_listmv foo/$line1.. / foo2done} ls-1 foo/ | huge_mv
In contrast to 3.1, example 3.2 generates a md checkhash file and name backup of the file, which is in line with the philosophy of leaving a way back for yourself.
In addition, you can expand the function indefinitely according to your own needs.
Method 4: the ultimate solution, recompile the kernel
First of all, you should be careful before using this solution, because when it comes to modifying the kernel source code, you should consider and test it in a production environment.
In addition, this method is fundamentally solved once and for all.
This is also one of the benefits of open source.
First, find the include/linux/binfmts.h 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
Change the MAX_ARG_PAGES value to 64 or higher to solve the problem of limited parameters.
Then recompile and enable the new kernel.
The above is all the contents of the article "prompt Argument list too long what to do when linux operates in batches". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 212
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.