使用curl下载多个URL,无需重复参数



我正在尝试使用卷曲下载多个URL:

user@PC:~$ curl -LOJ "https://example.com/foo.jpg" "https://example.com/bar.jpg"
% Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
Dload  Upload   Total   Spent    Left  Speed
100   445  100   445    0     0    517      0 --:--:-- --:--:-- --:--:--   518
<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>404 - Not Found</title>
</head>
<body>
<h1>404 - Not Found</h1>
<script type="text/javascript" src="//wpc.75674.betacdn.net/0075674/www/ec_tpm_bcon.js"></script>
</body>
</html>
user@PC:~$ ls foo.jpg
foo.jpg
user@PC:~$ ls bar.jpg
ls: cannot access 'bar.jpg': No such file or directory

但它只将参数(-LOJ(应用于第一个URL,因此只下载第一个文件。

如果我重复每个URL的参数,这个问题将不再发生,并且两个文件都会被下载:

user@PC:~$ curl -LOJ "https://example.com/foo.jpg" -LOJ "https://example.com/bar.jpg"
% Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
Dload  Upload   Total   Spent    Left  Speed
100   445  100   445    0     0    481      0 --:--:-- --:--:-- --:--:--   481
% Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
Dload  Upload   Total   Spent    Left  Speed
100   445  100   445    0     0   1534      0 --:--:-- --:--:-- --:--:--  1539
user@PC:~$ ls foo.jpg
foo.jpg
user@PC:~$ ls bar.jpg
bar.jpg

那么,有没有一种方法可以将参数应用于传递给curl的所有URL,而不必对每个URL重复它?

您需要使用-n1,如

{ echo "https://example.com/foo.jpg"; echo "https://example.com/bar.jpg"; } | xargs -n1 curl -LOJ

要告诉xargs为每个url启动一个curl,不要将两个url都作为参数运行单个curl。

那么,有没有一种方法可以将参数应用于传递给curl的所有URL,而不必对每个URL重复它?

  • 如果您有curl v7.19.0或更高版本,则存在--remote-name-all,以避免重复-O。CCD_ 5和CCD_。

  • 如果没有,您可以使用make-url-list | sed 's/^/-O /' | xargs curl -JL

  • 如果您有wget,它与--content-disposition--trust-server-names中的-J-O具有类似的选项,这些选项适用于所有给定的URL。

最新更新