Curl Download Multiple Files in One Command

Downloading multiple files individually can be tedious and time-consuming. Fortunately, the curl command-line tool provides several efficient ways to download multiple files simultaneously or sequentially using a single command. This article covers the most effective methods to achieve this, including specifying multiple URLs, using globbing patterns for sequential files, and enabling parallel downloads to speed up the process.

Method 1: Specifying Multiple URLs Separately

The simplest way to download multiple files is to list each URL in the command, preceded by the -O (uppercase ‘O’) option. The -O option tells curl to save the file using its original remote name.

curl -O https://example.com/file1.zip -O https://example.com/file2.zip -O https://example.com/file3.zip

If you want to save the downloaded files with custom names, use the lowercase -o option followed by your preferred filename:

curl -o local_name1.zip https://example.com/file1.zip -o local_name2.zip https://example.com/file2.zip

Method 2: Using Globbing for Sequential or Patterned Files

If the files you want to download follow a specific naming pattern, you can use curl’s built-in URL globbing. This allows you to download a range of files using brackets or braces.

Downloading a Range of Numbers or Letters

To download files numbered from 1 to 5, use square brackets []:

curl -O "https://example.com/document-[1-5].pdf"

You can also specify step counters. For example, to download every second file between 1 and 10:

curl -O "https://example.com/image-[1-10:2].jpg"

This also works with alphabetical ranges:

curl -O "https://example.com/report-[a-e].pdf"

Downloading Specific File Sets

To download specific files that do not follow a strict numerical sequence, use curly braces {} to list the variables:

curl -O "https://example.com/{file1.zip,photo.jpg,archive.tar.gz}"

Method 3: Downloading Files in Parallel

By default, curl downloads multiple files sequentially (one after the other). If you are using curl version 7.66.0 or later, you can use the -Z (or --parallel) option to download the files at the same time, which significantly reduces the total download time.

curl -Z -O https://example.com/file1.zip -O https://example.com/file2.zip -O https://example.com/file3.zip

You can combine the parallel flag with globbing patterns as well:

curl -Z -O "https://example.com/file[1-5].zip"