The Computer Oracle

How to wget a file with correct name when redirected?

--------------------------------------------------
Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------

Music by Eric Matyas
https://www.soundimage.org
Track title: Dreaming in Puzzles

--

Chapters
00:00 How To Wget A File With Correct Name When Redirected?
00:48 Accepted Answer Score 209
02:08 Answer 2 Score 96
02:20 Answer 3 Score 5
02:30 Answer 4 Score 0
02:46 Thank you

--

Full question
https://superuser.com/questions/301044/h...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#linux #commandline #wget #redirection #curl

#avk47



ACCEPTED ANSWER

Score 209


-O file
--output-document=file
   

The documents will not be written to the appropriate files, but all will be concatenated together and written to file. If - is used as file, documents will be printed to standard output, disabling link conversion. (Use ./- to print to a file literally named -.)

So,

wget -O somefile.extension 'http://www.vim.org/scripts/download_script.php?src_id=9750'

Or, you may be able to get wget to automatically use the filename proposed by the server using the --content-disposition option if supported by your version.

wget --content-disposition 'http://www.vim.org/scripts/download_script.php?src_id=9750'

Caveats as per the man page,

--content-disposition

If this is set to on, experimental (not fully-functional) support for "Content-Disposition" headers is enabled. This can currently result in extra round-trips to the server for a "HEAD" request, and is known to suffer from a few bugs, which is why it is not currently enabled by default.

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.

You can achieve the same automated behavior with curl, using,

curl -JLO 'http://www.vim.org/scripts/download_script.php?src_id=9750'

-O uses the remote name, and -J forces the -O to get that name from the content-disposition header rather than the URL, and -L follows redirects if needed.




ANSWER 2

Score 96


With wget you can do this:

wget --trust-server-names <url> 

to save the file using the last file name the server gives you.




ANSWER 3

Score 5


You could also use aria2c - it seems to work nicely with the Content-Disposition headers.




ANSWER 4

Score 0


Worked by following:

curl -o molokai.vim http://www.vim.org/scripts/download_script.php?src_id=9750

wget -O somefile.extension http://www.vim.org/scripts/download_script.php?src_id=9750

(changed case to smaller i.e. (the wget -O) to (wget -o)