使用 curl 下载多个 URL 而无需重复参数
Download multiple URLs with curl without repeating the arguments
我正在尝试使用 curls 下载多个 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。
So is there a way to have the arguments apply to all the URLs passed to curl without having to repeat it for each URL?
如果你有curl v7.19.0或更高版本,有--remote-name-all
,避免重复-O
。 -L
和-J
只需要给一次。
如果没有,你可以使用make-url-list | sed 's/^/-O /' | xargs curl -JL
如果你有wget
,它有与-J
和-O
类似的选项,在--content-disposition
和--trust-server-names
中,适用于所有给定的网址。
我正在尝试使用 curls 下载多个 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。
So is there a way to have the arguments apply to all the URLs passed to curl without having to repeat it for each URL?
如果你有curl v7.19.0或更高版本,有
--remote-name-all
,避免重复-O
。-L
和-J
只需要给一次。如果没有,你可以使用
make-url-list | sed 's/^/-O /' | xargs curl -JL
如果你有
wget
,它有与-J
和-O
类似的选项,在--content-disposition
和--trust-server-names
中,适用于所有给定的网址。