0

A lot of website redirect URL to let you the last version of a binary.

For example:

wget https://download.mozilla.org/?product=firefox-aurora-latest-l10n&os=linux64&lang=fr

Will let you download the latest Firefox Developper Edition. Output file will be "firefox-50.0a2.fr.linux-x86_64.tar.bz2".

But

wget https://download.mozilla.org/?product=firefox-aurora-latest-l10n&os=linux64&lang=fr -P $HOME

will leads to an output filename like: "?product=firefox-aurora-latest-l10n&os=linux64&lang=fr".

So I will use:

wget https://download.mozilla.org/?product=firefox-aurora-latest-l10n&os=linux64&lang=fr -P $HOME --trust-server-names

to redirect to the good file name: "firefox-50.0a2.fr.linux-x86_64.tar.bz2".

But on the next update the filename will be different.

I'm currently writting a script so I need to download the file with the good filename.

My question is:

How can I get the downloaded filename in a $var in order to use it next for example to extract the archive?

Note : I can't use basename as the name is not in the URL.

Note 2 : I use --trust-server-names because --content-disposition is experimental and not reliable.

noraj
  • 290
  • 3
  • 17
  • 1) Get the solved url: `curl -L --head -w '%{url_effective}' http://repo1/xyz/LATEST 2>/dev/null | tail -n1` from [Resolve filename from a remote URL without downloading a file](http://unix.stackexchange.com/questions/126252/resolve-filename-from-a-remote-url-without-downloading-a-file). – noraj Aug 30 '16 at 18:04
  • 2) Extract the filename: `shopt -s extglob; url=http://www.foo.bar/file.ext; echo ${url##+(*/)}; shopt -u extglob` from [Extract the base file name from a URL using bash](http://unix.stackexchange.com/questions/64432/extract-the-base-file-name-from-a-url-using-bash). – noraj Aug 30 '16 at 18:05
  • @julie-pelletier, sam, mdpc, g-man, jeff-schaller : it's not really a duplicate as it partially anwser my question. – noraj Aug 30 '16 at 18:07

1 Answers1

0

The filename can be seen in the stderr output of the command in a line like

Saving to: 'firefox-50.0a2.fr.linux-x86_64.tar.bz2'

So you can capture the stderr to a file or pipe and extract the string

if wget ... 2>log
then filename=$(awk <log '/^Saving to:/{print substr($0,13,length($0)-14)}')
...

Or if you want you can get the filename from the redirect by doing

wget -S --max-redirect=0 ...

This will not retrieve the file but on stderr will show the new location for it:

Location: https://download-installer.cdn.mozilla.net/pub/firefox/nightly/latest-mozilla-aurora-l10n/firefox-50.0a2.fr.linux-x86_64.tar.bz2

andn you can extract the filename from that.

meuh
  • 49,672
  • 2
  • 52
  • 114