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;
xargs
A filter for passing parameters to other commands in the command in method one is also a tool for combining multiple commands; the content -i
will xargs
be assigned to {}
.
Method II -exec
later parameter is performed behind the command -exec
to ;
ending, since each system different meanings semicolon, hence the \
escape, i.e. ;\
, in addition, during the operation {}
will be find
the 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