如何有效地使用curl -Z(--parallel)?
我需要使用 curl 下载数千个文件。我知道如何使用 xargs -Pn
(或 gnu parallel
)进行并行化,但我刚刚发现curl本身可以使用参数 -Z|-- 并行化下载curl-7.66 中引入了并行
(请参阅curl-goez-parallel),这可能更清晰或更容易共享。 我需要使用 -o|--output
选项和 --create-dirs
。 URL 需要进行百分比编码,URL 路径成为文件夹路径,也需要转义,因为路径可以包含单引号、空格和常见的可疑内容(因此 -O 选项 不安全,并且
-OJ 选项
没有帮助)。 如果我理解得很好,curl 命令应该像这样构建:
curl -Z -o path/to/file1 http://site/path/to/file1 -o path/to/file2 http://site/path/to/file2 [-o path/to/file3 http://site/path/to/file3, etc.]
这确实有效,但是处理数千个 URL 的最佳方法是什么。与 -K config
一起使用的 config
文件有用吗?如果 -o path/to/file_x http://site/path/to/file_x 是另一个程序的输出怎么办?我还没有找到任何方法在文件中记录命令,例如每行一个命令。
I need to download thousands of files with curl. I know how to parallelize with xargs -Pn
(or gnu parallel
) but I've just discovered curl itself can parallelize downloads with the argument -Z|--parallel
introduced in curl-7.66 (see curl-goez-parallel) which might be cleaner or easier to share.
I need to use -o|--output
option and --create-dirs
. URLs need to be percent-encoded, the URL path becoming the folder path which also need to be escaped as path can contain single quotes, spaces, and usual suspects (hence -O option
is not safe and -OJ option
doesn't help).
If I understand well, curl command should be build like so:
curl -Z -o path/to/file1 http://site/path/to/file1 -o path/to/file2 http://site/path/to/file2 [-o path/to/file3 http://site/path/to/file3, etc.]
This works indeed, but what's the best way to deal with thousands URLS. Can a config
file used with -K config
be useful? what if the -o path/to/file_x http://site/path/to/file_x
is the output of another program? I haven't found any way to record commands in a file, one command per line, say.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我最终在curl 用户邮件列表上询问。
对我有用的解决方案是构建一个配置(纯文本)文件,以编程方式构建:
config.txt:
并使用此curl命令:
该命令比这更深入,并且需要最新的curl版本(7.81+)
用于选项像 -K, --config 参见 https://curl.se/docs/manpage.html
华泰
I ended up asking on curl user mailing list.
The solution that worked for me was building a config (plain text) file built like so programmatically:
config.txt:
and use this curl command:
The command is a bit reacher than this and requires a recent curl version (7.81+)
For options like -K, --config see https://curl.se/docs/manpage.html
HTH