brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: curl -Z runs several downloads in parallel

Curl
~/tips/tip-curl-parallel-z-downloads

curl --parallel --remote-name-all \

Starts every download on the command line at once instead of waiting for each one to finish.

A script that fetches a handful of files one after the other always makes me wait for no good reason. curl can start all the transfers at once with a single flag.

-Z (long form --parallel) tells curl to run every transfer on the command line at the same time instead of one by one. Each URL still needs somewhere to go, so I pair it with --remote-name-all, which saves each URL under the file name it has on the server:

curl --parallel --remote-name-all \
  https://example.com/a.iso \
  https://example.com/b.iso \
  https://example.com/c.iso

The number of simultaneous transfers is set with --parallel-max. The manual gives 50 as the default, so if you pass more URLs than that, curl starts a new one whenever an earlier one finishes. For a polite download from one server I would lower it:

curl -Z --parallel-max 4 --remote-name-all -- https://example.com/file1 https://example.com/file2

There is also --parallel-max-host, which caps the connections to one protocol, host and port. The manual says it was added in curl 8.16.0, so an older curl will not know it. The progress meter changes too: instead of one bar per file you get a single line that covers all transfers.

The caveat is the exit code. With serial transfers the exit code comes from the last transfer. With --parallel, curl keeps going when one transfer fails and exits with the error code of the first one that failed. If a script should stop at the first problem, add --fail-early. And without --fail or --fail-with-body, an HTTP 404 does not count as a failure at all, so a missing file will not trip your error handling by itself.

Further reading: curl man page: –parallel and –parallel-max

More Curl tips curl --trace-ascii – https://example.com/ Retry-After: 120 curl -fsS --fail-early \ url = "https://example.com/data/latest.json" curl -4 -s -o /dev/null -w '%{remote_ip} %{http_code}\n' https://example.com/ All Curl tips (71) All topics

Link