6

I do

wget http://services.runescape.com/m=itemdb_rs/3809_obj_sprite.gif?id=2

and it downloads the following file:

3809_obj_sprite.gif?id=2

How can I make it save as the following?

2.png
2
  • 16
    A quick note: wget does not do image conversion. Saving a GIF file with a .png extension does not turn it into a PNG file. You'll want to save the file with the proper extension (in this case, .gif), and use image manipulation software to convert it to a different format.
    – Indrek
    Aug 4, 2012 at 16:17
  • 12
    An image is a file. Aug 4, 2012 at 21:37

6 Answers 6

38

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.

2
  • 1
    I have no idea why this has so many up votes, because the URL you posted is for a .gif file, changing the file name to 2.png will not magically make that .gif a .png. Either way an image is a file, everything saved anywhere is a file even directories are actually stored on filesystems as files.
    – user22908
    Aug 4, 2012 at 22:30
  • 4
    Jarrod, I now updated my answer and explicitly addressed what probably was a misunderstanding by the OP. As for you asking why this answer had so many up votes – are you inferring it should have been downvoted because I gave the OP wrong information? I never claimed that an image was not a file and that a GIF would magically turn into a PNG when renamed. I assumed it was a typo since the focus of the question was the file name.
    – slhck
    Aug 4, 2012 at 23:02
6

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...

2
  • 1
    Nice find, I missed that before. It doesn't work in this case, but it could in others.
    – slhck
    Aug 4, 2012 at 22:12
  • @slhck Yes, noticed that too - after writing it. But since it might work for others I left it :)
    – dualed
    Aug 4, 2012 at 22:28
3

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.

1

If the file you direct wget to download for you is indeed an image, then what it downloads is an image. wget doesn't bother to guess the correct file name and extension, it uses what it finds in the URL e.g http://foobar.com/photo.php?n=cat.png you might get something like photo.php?n=cat.png but it is still an image. To fix your problem, simply rename that file to the correct name cat.png. Note that on most operating systems programs need the correct file extension to know that they can open a given file.

1

curl also has an option to guess the file name from Content-Disposition header:

curl -OLJ http://services.runescape.com/m=itemdb_rs/3809_obj_sprite.gif?id=2

Here

-O tells curl to save to a file rather than flushing to stdout
-L makes it follow the redirects
-J enables guessing by Content-Disposition instead of dissecting the URL

0

Also, with curl, say you don't know what the filename is going to be, due to redirects, or whathaveyou.. just follow the redirects via -L and pipe it to a file, with the correct extension..

curl -L http://links.mrgray.com/ocrtest > /files/file.png

wget may have similar functionality, dunno.

2
  • Why the downvote? It works, and solves the guys' problem, no?
    – mralexgray
    Aug 5, 2012 at 8:12
  • I didn't downvote but you clearly haven't read the question.
    – nikhil
    Aug 5, 2012 at 12:17

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .