Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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:

http://ge.tt/api/1/files/someFile/0/blob?download

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?

share|improve this question
    
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
    
@j883376: presumably, the Content-Disposition header. –  rici Jun 17 '13 at 21:11
    
I've come up with two ways, and I can answer my question once the 8 hour timer allows me to, if wanted, since it will not fit in the comments. (that or I can when I get 10 rep.) –  rtainc Jun 17 '13 at 21:16
    
@rtainc: that's serious reptrolling but here's an upvote anyway :) –  rici Jun 17 '13 at 21:23
    
@rici I didn't mean it like that, but thanks ;) –  rtainc Jun 17 '13 at 21:32

2 Answers 2

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 ..
share|improve this answer

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

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

downloads/fj39ejd/theFileNameThatWasBeing.Downloaded

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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