Bash – cURL – download multiple files and specify output names

bashcurl

I have list of URLs with range in {}. I can download them with cURL in bash, but problem is output. I can use #1, but I need to have files named with different names (I have stored those names in array). How can change output names for multiple files ?

I have tried o download file by file, but that is about 40% slower, than multiple download.

Best Answer

You can iterate over two arrays, one with your URLs to download and another with your desired filenames, launching subprocesses that will run in parallel to download the files.

#!/bin/sh

# array of URLs
urls=( http://www.example.com/file1.zip http://www.example.com/file2.zip http://www.example.com/file3.zip )
# names of downloaded files
names=( myname1.zip myname2.zip myname3.zip )

for ((i = 0; i < ${#urls[@]}; ++i)); do
    printf "Launching subprocess to DL %s to %s\n" "${urls[i]}" "${names[i]}"
    curl -s -o "${names[i]}" "${urls[i]}" &
done

Or try something like this if you want it to be similar to using {} w/ curl:

#!/bin/sh

# desired filenames
names=( myname0.zip myname1.zip myname2.zip )

for (( i = 0; i < ${#names[@]}; ++i )); do
  printf "Launching subprocess to DL http://example.com/file%s.zip to %s\n" "$i" "${names[i]}"
  curl -s -o "${names[i]}" http://example.com/file"$i".zip &
done