Why does wget'ing an image give me a file, not an image?

Why does wget choose this name?

Unfortunately, wget will make no assumptions about what you want to download unless you tell it to. It doesn't care if your file is an image, a document, a zipped file, et cetera.

The file is saved as whatever the URL ended with – so anything from the last slash to the end of the URL. In your case, that's .gif?id=2. The part after the actual file extension (.gif) is contained in the URL, but it's a HTTP query parameter. For wget, however, it will determine the output filename.

How can I set a different filename?

If you want to specify an output filename for wget, add the -O (uppercase O) option:

wget example.com/something.gif?id=2 -O 2.gif

This will override the default behavior and set the filename to 2.gif. Note that the -o (downcase) option specifies the output filename for any log messages wget might otherwise print to the shell.

Finally, there's the --content-disposition option which might result in the proper filename being set. But this entirely depends on the server you're downloading from sending the correct header information:

This option is useful for some file-downloading CGI programs that use Content-Disposition headers to describe what the name of a downloaded file should be.

The option is currently still marked as experimental and thus not enabled by default.


I strongly encourage you to read the manpage of the tools you're using to understand their behavior. Just enter man wget and read through it, especially the options it provides.

Also, to address what @Indrek wrote in the comment on your question: I assume you have a typo there and don't mean to download a GIF file into a file called .png — just changing the extension will not automatically make it a PNG. GIF and PNG use different encodings and you will have to use any kind of image conversion tool to convert between these formats. This conversion, however, will be lossless, so there's no harm done in downloading the files in the "wrong" format and converting later.


There is the command line option --content-disposition which - if the website supplies a correct header - should cause your file to be saved with the right name.

My debian squeeze box it says the option is "experimental" though...


This is simply because look at the path, it ends in ?id=2, therefore it will save as this, you can move the file or use the -O parameter to define a file name.

Tags:

Linux

Bash

Wget