【Linux命令】bash: /usr/bin/rm: Argument list too long
问题:bash: /usr/bin/rm: Argument list too long
原因:
参考:bash: /usr/bin/rm: Argument list too long - Solution (haydenjames.io)
Over time, the storage used on Linux systems you manage will grow. As a result, you will, at some point, try to delete, move, search, or otherwise manipulate thousands of files using commands such as rm
, cp
, ls
, mv
, and so on, which are all subject to this limitation. As such, you will eventually come across the “Argument list too long” error detailed below.
“Argument list too long” indicates when a user feeds too many arguments into a single command which hits the ARG_MAX
limit. The ARG_MAX
defines the maximum length of arguments to the exec function.
An argument, also called a command-line argument, can be defined as the input given to a command, to help control that command line process. Arguments are entered into the terminal or console after typing the command. Multiple arguments can be used together; they will be processed in the order they typed, left to right.
This limit for the length of a command is imposed by the operating system. You can check the limit for maximum arguments on your Linux system using this command:
getconf ARG_MAX
Which will return something like this:
hydn@centos:~$ getconf ARG_MAX
2097152
The “argument list too long” error means that you’ve exceeded the maximum command-line length allowed for arguments in a command.
解决方案:
参考:mv argument list too long错误_bisal(Chen Liu)的博客-CSDN博客
解决方案1:
Argument list too long本质是需要处理的长度超过系统的长度,因此无法执行相关命令。
既然参数过长,直观的思路,就是减少参数,分而治之的方式,来解决这问题。
此时就可以借助find找出符合条件的文件,然后拆开执行,mv的指令,有两种执行方式,一个是xargs,另一个是-exec。
xargs指令是给其他指令传递参数的一个过滤器,也是组合多个命令的一个工具,-i会将xargs的内容赋值给{}。
-exec参数后面是指执行其后面的指令,-exec以;为结尾,由于各个系统中分号的意义不同,因此用\进行转义,即\;,{}会被find指令的结果替换。
我们采用-exec,拼接指令如下,find首先找出符合条件的文件,然后{}会替换find的结果,依次执行mv,