wget: downloaded file name

Crazy_Bash picture Crazy_Bash · Dec 20, 2011 · Viewed 25.9k times · Source

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 it's 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]

Answer

est picture est · Jan 14, 2013
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.