Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

wget: downloaded file name

Tags:

I'm writing a script for Bash and I need to get the name of the downloaded file using wget and put the name into $string.

For example, if I downloading this file below, I want to put its name, mxKL17DdgUhcr.jpg, to $string.

wget http://pics.sitename.com/images/191211/mxKL17DdgUhcr.jpg 45439 (44K) [image/jpeg] Saving to: «mxKL17DdgUhcr.jpg»  100%[===================================================================================================>] 45 439      --.-K/s   в 0s  2011-12-20 12:25:33 (388 MB/s) - «mxKL17DdgUhcr.jpg» saved [45439/45439] 
like image 365
Crazy_Bash Avatar asked Dec 20 '11 10:12

Crazy_Bash


People also ask

Where is the file downloaded from wget?

When downloading a file, Wget stores it in the current directory by default. You can change that by using the -P option to specify the name of the directory where you want to save the file. Download the jQuery file you downloaded previously, but this time save it in the Downloads subdirectory.

How do I download files using wget?

Let's start with something simple. Copy the URL for a file you'd like to download in your browser. Now head back to the Terminal and type wget followed by the pasted URL. The file will download, and you'll see progress in realtime as it does.

What is -- content disposition in wget?

Use wget --content-disposition <url> Explanation: The Content-Disposition header can be used by a server to suggest a filename for a downloaded file. By default, wget uses the last part of the URL as the filename, but you can override this with --content-disposition , which uses the server's suggested name.

How do I redirect wget output?

wget with –output-document The wget command outputs the document content in a separate file by default. However, we can use the –output-document (-O) option to redirect the content to a file of our choice. As a particular use case, if we use – as the file, it directs the content to stdout. Great!


1 Answers

wget --server-response -q -O - "https://very.long/url/here" 2>&1 |    grep "Content-Disposition:" | tail -1 |    awk 'match($0, /filename=(.+)/, f){ print f[1] }' ) 

This is the correct version as there are may be several 301/302 redirects and finally a Content-Disposition: header to set the file name

Guessing file name based on URL is not always correct.

like image 65
est Avatar answered Oct 24 '22 07:10

est