1. 程式人生 > 其它 >【Linux命令】bash: /usr/bin/rm: Argument list too long

【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

cplsmv, 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,