I need wget to save to a different folder, different filename, and same extension, but I do not have the filename before downloading.

For example, let's say I have:


and I need to download it to an id, "fj39ejd" in the folder downloads.

What is the best way to go about saving it to:

/path/to/downloads/fj39ejd.[extension it had before downloading]

Any advice on how I can achieve this?

  • Maybe I'm lost but, where are you expecting to get the file extension from if you don't have the filename available? – j883376 Jun 17 '13 at 21:10
  • 1
    @j883376: presumably, the Content-Disposition header. – rici Jun 17 '13 at 21:11
  • Have you tried putting together a shell script to download it to a temporary name and having some sort of case logic to rename it based off the output of the file command? – Bratchley Jun 17 '13 at 22:17

I've actually come up with two plausible ways of doing this:

a) You can save it to the folder "fj39ejd" which looks like:


or b) You can make a tmp folder, which would, in a while loop, check if it contains a file containing "fj39ejd" and then explode it, getting the extension. This one, however, is a bit expensive and the connection may time out.


If you want the local file to have the name chosen by the server, use wget --content-disposition --trust-server-names.

If you want the server-chosen extension with a locally-chosen name, you'll have to rename afterward. The easiest way is to create an empty directory, call wget in that directory, and rename whatever file was created to what you want.

dir="$(TMPDIR=$PWD mktemp -d)"
(cd dir && wget --content-disposition --trust-server-names 'http://ge.tt/api/1/files/someFile/0/blob?download')
set "$dir/"*
mv "$1" "../fj39ejd.${1#$dir/*.}"
rmdir "$dir"
cd ..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.