Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Download a .zip file from the command line

Tags:

curl

download

zip

I've been somewhat blindly trying lots of variations of curl and wget to try downloading some .zip files. The first thing I tried was this:

curl --klo ".\bhcdata#1.zip" "https://www.chicagofed.org/applications/bhc_data/bhcdata_create_output.cfm?DYR=2011&DQTR=[1-4]"

The command above is in fact provided in the documentation by the Chicago Fed, and yet it throws error messages on my Ubuntu machine: curl: option --klo: is unknown.

Is there a straightforward way to do this?

To clarify exactly what I'm trying to do: The Chicago Fed website lets you enter a year and quarter, and you click "Download data file" and it gives you a .zip file of the corresponding data. I want to do this for all quarters, so I need a way to write the command for each quarter so that I can loop over them. The [1-4] in the example command above would grab all four quarters in one year, but I'd be fine with just getting one quarter at a time, and I've tried replacing 1 as well. I've tried with and without various combinations of options, but nothing has worked yet.

like image 523
zkurtz Avatar asked Dec 12 '22 07:12

zkurtz


1 Answers

On my machine, the following command worked just fine:

curl -o ./bhcdata1.zip "https://www.chicagofed.org/applications/bhc_data/bhcdata_create_output.cfm?DYR=2011&DQTR=1"

Note:

  1. single dash instead of double dash
  2. I only specified the output file - with no quotes and with forward slash
  3. I simplified the URL to a specific quarter only

I found a file named bhcdata1.zip in my directory when I was done:

-rw-r--r--   1 floris  floris    1545868 Feb 13 21:05 bhcdata1.zip

You might find you need the -k flag as well… although I didn't need to on my machine. Then it would be

curl -ko ./bhcdata1.zip "https://www.chicagofed.org/applications/bhc_data/bhcdata_create_output.cfm?DYR=2011&DQTR=1"

worked equally well...

BONUS to get all four quarters, you can simply put the following lines in a script. Save it as downloadAll. Change the file to "executable" with chmod 755 downloadAll , then type ./downloadAll. Enjoy…

#!/bin/bash
for i in {1..4}
do
  curl -ko ./bhcdata$i.zip "https://www.chicagofed.org/applications/bhc_data/bhcdata_create_output.cfm?DYR=2011&DQTR=$i"
done

Result:

-rw-r--r--   1 floris  floris    1545868 Feb 13 21:11 bhcdata1.zip
-rw-r--r--   1 floris  floris    2413876 Feb 13 21:11 bhcdata2.zip
-rw-r--r--   1 floris  floris    1573810 Feb 13 21:11 bhcdata3.zip
-rw-r--r--   1 floris  floris    2500525 Feb 13 21:12 bhcdata4.zip

and if you wanted multiple years, do (some variant of)

#!/bin/bash
for yr in {2010..2012}
do
  for qtr in {1..4}
  do
    curl -ko ./bhcdata$yr_$qtr.zip "https://www.chicagofed.org/applications/bhc_data/bhcdata_create_output.cfm?DYR=$yr&DQTR=$qtr"
  done
done
like image 134
Floris Avatar answered Feb 01 '23 08:02

Floris