文章目录
error info解决方案原因参考链接
error info
通常在使用cp 或者mv 命令时, cp *.jpg some_folder/ 或者 mv *.jpg some_folder/
bash: /usr/bin/cp: Argument list too long
解决方案
使用 内置命令find ,包含以下方式 find . -name "*" -exec cp {} folder \; (可能比较慢 , 注意 \; 将当前目录文件 复制到folder) find . -exec cp {} + find . -print0 | xargs -0 cp {} folder find . -print | xargs cp {} folder (参数不含空格) 使用 for 循环
for i in folder1/*; do cp "$i" folder2/ ; done
鲁棒,但是可能比较慢, 将 folder1 中的 所有文件复制到 folder2 中。注意分号 ;, “$i” 为获取变量 i的值
printf '%s\0' *.json | xargs -0 -I {} cp {} folder2/
将当前目录中后缀为 .json的文件 移动到 folder2中,其中 -0 为数字0,处理文件名包含空格的文件, -I 为大写的i
直接拷贝文件夹,而不是具体的文件。如
cp -rnv folder1 folder2/
-rnv 为 复制文件夹,忽略已存在的文件,输出复制文件的名称。
快速解决方案 ,增加正则表达式的通配符匹配限制,过滤掉不满足要求的文件。 如:
cp *.png somefolder # 复制后缀为 .png文件, *号为匹配所有
mv name_* somefolder # 移动 以name_ 开始 的文件, *号为匹配所有
原因
文件数量过多导致 参数命令列表太长,超过linux系统限制。类似cp或mv的非系统内置命令会调用系统 exec() 命令,参数数量受限于 ARG_MAX 值(并非完全一致,依赖系统)。而类似 echo 和 find的内置命令则无这种限制。
查询1: get_conf ARG_MAX
$get_conf ARG_MAX # 在我的系统上,输出为2097152, 超过该限制则会报错。
查询2:xargs --show-limits
$xargs --show-limits
Your environment variables take up 3666 bytes
POSIX upper limit on argument length (this system): 2091438
POSIX smallest allowable upper limit on argument length (all systems): 4096
Maximum length of command we could actually use: 2087772
Size of command buffer we are actually using: 131072
Maximum parallelism (--max-procs must be no greater): 2147483647
Execution of xargs will continue now, and it will try to read its input and run commands; if this is not what you wanted to happen, please type the end-of-file keystroke.
Warning: echo will be run at least once. If you do not want that to happen, then press the interrupt keystroke.
参考链接
https://www.in-ulm.de/~mascheck/various/argmax/
精彩链接
发表评论