Skip to content
Advertisement

wget: downloaded file name

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]

Advertisement

Answer

Use the basename command to extract the filename from the URL. For example:

url=http://pics.sitename.com/images/191211/mxKL17DdgUhcr.jpg
filename=$(basename "$url")
wget "$url"
Advertisement