Alex Cory
Alex Cory

Reputation: 11845

How to download a file using curl

I'm on mac OS X and can't figure out how to download a file from a URL via the command line. It's from a static page so I thought copying the download link and then using curl would do the trick but it's not.

I referenced this StackOverflow question but that didn't work. I also referenced this article which also didn't work.

What I've tried:

curl -o https://github.com/jdfwarrior/Workflows.git
curl: no URL specified!
curl: try 'curl --help' or 'curl --manual' for more information

.

wget -r -np -l 1 -A zip https://github.com/jdfwarrior/Workflows.git
zsh: command not found: wget

How can a file be downloaded through the command line?

Upvotes: 65

Views: 106496

Answers (4)

RubenLaguna
RubenLaguna

Reputation: 24666

There are several options to make curl output to a file:

# saves it to myfile.txt
curl http://www.example.com/data.txt -o myfile.txt -L

# The #1 will get substituted with the url, so the filename contains the url
curl http://www.example.com/data.txt -o "file_#1.txt" -L 

# saves to data.txt, the filename extracted from the URL
curl http://www.example.com/data.txt -O -L

# saves to filename determined by the Content-Disposition header sent by the server.
curl http://www.example.com/data.txt -O -J -L

# -O Write output to a local file named like the remote file we get
# -o <file> Write output to <file> instead of stdout (variable replacement performed on <file>)
# -J Use the Content-Disposition filename instead of extracting filename from URL
# -L Follow redirects

Upvotes: 7

jfly
jfly

Reputation: 7990

The -o --output option means curl writes output to the file you specify instead of stdout. Your mistake was putting the url after -o, and so curl thought the url was a file to write to rate and hence that no url was specified. You need a file name after the -o, then the url:

curl -o ./filename https://github.com/jdfwarrior/Workflows.git

And wget is not available by default on OS X.

Upvotes: 76

Buddhi
Buddhi

Reputation: 995

curl -OL https://github.com/jdfwarrior/Workflows.git

-O: This option used to write the output to a file which named like remote file we get. In this curl that file would be Workflows.git.

-L: This option used if the server reports that the requested page has moved to a different location (indicated with a Location: header and a 3XX response code), this option will make curl redo the request on the new place.

Ref: curl man page

Upvotes: 57

Agile Bean
Agile Bean

Reputation: 7141

The easiest solution for your question is to keep the original filename. In that case, you just need to use a capital o ("-O") as option (not a zero=0!). So it looks like:

curl -O https://github.com/jdfwarrior/Workflows.git

Upvotes: 8

Related Questions