2020/10/23: The solution to "Argument list too long" encountered when executing the zip command in Linux

Problems encountered:

   `执行zip命令碰到"Argument list too long"`

Intuitive problem description:

When using cp, mv, rm, zip and other commands under Linux, the "Argument list too long" error is often encountered. This is mainly because the parameters of these commands are too long, that is, there are too many files.

Solution:

# 方法一:
find dir/ -name "*.bin" | xargs -i zip aa.zip {
    
    }
# 方法二:
find dir/ -name "*.bin" -exec zip aa.zip {
    
    } \;

Where dir/is the source folder address;

"*.bin"Is the source file name;

xargsA filter for passing parameters to other commands in the command in method one is also a tool for combining multiple commands; the content -iwill xargsbe assigned to {}.

Method II -execlater parameter is performed behind the command -execto ;ending, since each system different meanings semicolon, hence the \escape, i.e. ;\, in addition, during the operation {}will be findthe result of the replacement command.

Similarly, the commands for rm and cp are as follows:

# 删除文件夹下以bin结尾的文件
# 方法一:
find dir/ -name "*.bin" | xargs -i rm {
    
    }
# 方法二:
find dir/ -name "*.bin" -exec rm {
    
    } \;
# 拷贝文件夹下以bin结尾的文件到目标文件目录下
# 方法一:
find dir/ -name "*.bin" | xargs -i cp {
    
    } dir1
# 方法二:
find dir/ -name "*.bin" -exec cp {
    
    } dir1 \;

problem

The above method does solve the problem, but the processing speed is still slow for a large number of files

Reference for this solution: "Argument list too long" when executing zip, cp, rm and other commands in Linux

Guess you like

Origin blog.csdn.net/weixin_43624728/article/details/109239694