经常和图像打交道 经常需要用到删除命令 但是会报 参数列表太长 主要是目录下的图像文件太多了
四种解决”Arg list too long”参数列表过长的办法
一、使用find命令的-exec选项:
通过将需要处理的文件传递给 find
命令的 -exec
选项,可以避免参数列表过长的问题。例如:
find /path/to/files -name "*.txt" -exec some_command {} \;
二、使用xargs命令:xargs
命令可以将输入作为参数传递给其他命令,避免参数列表过长。例如:
find /home/ubuntu/data/upload/ -name "*.jpg" | xargs -i rm {}
三、将参数写入文件:
将需要传递的参数写入文件,然后使用 xargs
或循环读取文件中的参数。这适用于参数太多以至于无法一次传递的情况。例如:
find /path/to/files -name "*.txt" > file_list.txt
xargs some_command < file_list.txt
四、使用for循环:
使用 for
循环逐个处理文件。这种方法适用于需要处理的文件数较少的情况。例如:
for file in /path/to/files/*.txt; do
some_command "$file"
done
用的是第二个方案:
find /home/ubuntu/data/upload/ -name "*.jpg" | xargs -i rm {}