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]
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.
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.
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.
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!
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With