John
John

Reputation: 5834

How do I save a file using the response header filename with cURL?

This question relates to the command line version of cURL.

I'm trying to download a file from a CGI script.

http://example.com/perl/dl.pl?ID=2

Using a browser the filename comes up as remotefilename.gz. cURL wants to save the file as dl.pl?ID=2.

How do I get cURL to save to a file with the filename from the response header?

Upvotes: 114

Views: 58265

Answers (4)

Daniel Stenberg
Daniel Stenberg

Reputation: 58214

-J/--remote-header-name is the option you want.

You use -J in conjunction with -O, which makes curl use the file name part from the URL as its primary way to name the output file and then if there is a Content-disposition: header in the response, curl will use that name instead.

Upvotes: 179

jchook
jchook

Reputation: 7260

curl -OJ gave me segmentation fault: 11 error when attempting to download multiple URLs at once.

Instead, I used wget. The latest version supports HTTP Content-Disposition headers, which often contain filename information.

wget --content-disposition http://example.com/download/url

Upvotes: 25

egiray
egiray

Reputation: 1209

curl http://example.com/dl.php?file=3123123 -O -J

if server uses redirection use these:

--location-trusted
--max-redirs 10

Upvotes: 25

ghostdog74
ghostdog74

Reputation: 342967

you can try the -o or -O option.

Upvotes: -9

Related Questions