我正在尝试通过以下语法下载两个文件:
curl -O http://domain/path/to/{file1,file2}
Run Code Online (Sandbox Code Playgroud)
问题是实际上只有第一个文件保存在本地,而第二个文件只是简单地打印到标准输出。
我确实意识到,如果我添加一个-O
它就可以正常工作:
curl -OO http://domain/path/to/{file1,file2}
Run Code Online (Sandbox Code Playgroud)
但是,如果文件数量变得太大,这不是不切实际吗?例如,
curl -O http://domain/path/to/file[1,100]
Run Code Online (Sandbox Code Playgroud)
我的问题是,真的没有办法一次下载多个单独的文件curl
(不添加正确数量的-O
)?
Bes*_*rks 34
我意识到这个问题已经有一个公认的答案,但我觉得我应该指出实际上有一种方法可以做到这一点。
该--remote-name-all
选项告诉 curl 的行为就像您使用-O
或--remote-name
对于每个文件一样。
https://curl.haxx.se/docs/manpage.html#--remote-name-all
此选项从 7.19.0 版本开始可用
https://curl.haxx.se/changes.html#7_19_0
jno*_*orp 12
更新:这已在 curl 7.19.0 中实现。请参阅@Besworks 答案。
根据手册页,除了使用多个 O 之外,无法保留原始文件名。或者,您可以使用自己的文件名:
curl http://{one,two}.site.com -o "file_#1.txt"
Run Code Online (Sandbox Code Playgroud)
导致http://one.site.com
被保存到file_one.txt
并http://two.site.com
保存到file_two.txt
。
甚至多个变量,如
curl http://{site,host}.host[1-5].com -o "#1_#2"
Run Code Online (Sandbox Code Playgroud)
导致http://site.host1.com
被保存到site_1
,http://host.host1.com
被保存到host_1
等等。
小智 6
这里的问题是 shell(可能是 BASH)如何解释命令curl
。
基本上,它是在看
curl -O http://domain/path/to/{file1,file2}
并将其扩展为:
curl -O http://domain/path/to/file1 http://domain/path/to/file2
这是一个问题,因为标志-O
只应用于第一个实例,而不应用于其后的任何实例。
这可以通过双引号 url 来修复:
curl -O "http://domain/path/to/{file1,file2}"
curl 团队已经在GitHub上承认了这一点,并且已提交了手册页和官方curl手册的PR ,因此他们应该在将来的某些 shell 中反映这种潜在的通配行为。
如果您想要一个简单的测试用例来查看此故障以及如何修复它,请尝试从Project Gutenberg下载Moby Dick的两个 .txt 文件 zip 。
跑步:
curl -O https://www.gutenberg.org/files/2701/2701-{0,h}.zip
将提供以下响应:
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 500k 100 500k 0 0 380k 0 0:00:01 0:00:01 --:--:-- 380k
Warning: Binary output can mess up your terminal. Use "--output -" to tell
Warning: curl to output it to your terminal anyway, or consider "--output
Warning: <FILE>" to save to a file.
(如果您提取的文件是原始文本,那么它只会转储到标准输出,可能是您的屏幕,这对于Moby Dick来说意味着这样做,这就是为什么使用 .zip 文件。)
跑步:
curl -O "https://www.gutenberg.org/files/2701/2701-{0,h}.zip"
将给出类似这样的输出:
[1/2]: https://www.gutenberg.org/files/2701/2701-0.zip --> 2701-0.zip
--_curl_--https://www.gutenberg.org/files/2701/2701-0.zip
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 500k 100 500k 0 0 407k 0 0:00:01 0:00:01 --:--:-- 407k
[2/2]: https://www.gutenberg.org/files/2701/2701-h.zip --> 2701-h.zip
--_curl_--https://www.gutenberg.org/files/2701/2701-h.zip
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 522k 100 522k 0 0 1000k 0 --:--:-- --:--:-- --:--:-- 1000k
请注意,此问题仅在{ }
用于通配符时才会出现。如果仅[ ]
使用则不需要双引号。
顺便说一句,如果wget
安装了,它可能可以处理通配符而无需引号。
跑步:
wget http://domain/path/to/{file1,file2}
将拉下这两个文件(如果它们确实存在)。
跑步:
wget https://www.gutenberg.org/files/2701/2701-{0,h}.zip
将按照上面的双引号示例下载 Moby Dick zip 文件curl
。