我有一个很深的目录结构,有大量的文件(大约 1M)。我想对每个执行命令。(在我的情况下,文件是我想针对它运行 optipng 的 png。)
我试过这个:
find . -name *.png | xargs sudo optipng -o2
Run Code Online (Sandbox Code Playgroud)
但我收到错误argument list too long。
我假设 xargs 应该能够处理这个问题,而且我的语法一定有问题。
直接使用 find 而不是通过 xargs 执行 exec。
sudo find . -name '*.png' -exec optipng -o {} +
Run Code Online (Sandbox Code Playgroud)
如您所见,使用 sudo 作为执行命令xargs不起作用。换个方式试试:
find . -name '*.png' | sudo xargs optipng -o2
Run Code Online (Sandbox Code Playgroud)
不同之处在于您的版本创建了一个类似的命令
sudo optipng -o2 file1 file2 file3 file4 ......
Run Code Online (Sandbox Code Playgroud)
而 sudo 无法处理这么多参数。
相反,执行 sudo 一次,然后启动,xargs然后生成如下命令
optipng -o2 file1 file2 file3 file4 ......
Run Code Online (Sandbox Code Playgroud)
如果optipng可以处理许多文件作为参数,它应该可以工作。
如果这仍然不起作用,您将不得不-exec像杰兰特的答案那样使用,但这应该是您的最后手段。
| 归档时间: |
|
| 查看次数: |
1537 次 |
| 最近记录: |