This question already has an answer here:

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".


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.

marked as duplicate by Julie Pelletier, sam, mdpc, G-Man, Jeff Schaller Aug 30 '16 at 0:07

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


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.

  • 1st method doesn't work. – noraj Aug 30 '16 at 17:41
  • 2nd method is not generic, it will download the file if no redirection. – noraj Aug 30 '16 at 17:42

Not the answer you're looking for? Browse other questions tagged or ask your own question.