How do I preserve the remote filename when Downloading a file using curl

ShellScriptingCurl

Shell Problem Overview


> Possible Duplicate:
> How do I save a file using the response header filename with cURL

I need to download many thousands of images in the format

http://oregondigital.org/cgi-bin/showfile.exe?CISOROOT=/baseball&CISOPTR=0

If you paste that link in a browser, it tries to download a file named 1.jp2

I want to use curl to do the same. However, when I run

curl -I 'http://oregondigital.org/cgi-bin/showfile.exe?CISOROOT=/baseball&CISOPTR=0';

the filename is reported as 404.txt which you can download and see that it is actually the file I want. I can't use the -O option because the name assigned to the file is no good, and I have technical reasons for needing the actual name used on the system.

How do I get curl to download the same file I have no trouble retrieving in my browser? Thanks.

Shell Solutions


Solution 1 - Shell

The solution is to use -O -J

> -O, --remote-name Write output to a file named as the remote file
> -J, --remote-header-name Use the header-provided filename

So...

curl  -O -J  'http://oregondigital.org/cgi-bin/showfile.exe?CISOROOT=/baseball&CISOPTR=0'

I had to upgrade my CURL. I had v 7.19 which doesn't support -J but 7.22 (which is the latest) does.

Solution 2 - Shell

You can use the -o option can you? eg

 curl  'http://oregondigital.org/cgi-bin/showfile.exe?CISOROOT=/baseball&CISOPTR=[0-9]' -o "#1.jpg"

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionKyle BanerjeeView Question on Stackoverflow
Solution 1 - ShellYevgeny SimkinView Answer on Stackoverflow
Solution 2 - Shellbash-o-logistView Answer on Stackoverflow