Wget File Download with Correct Name

How to tell wget to get correct file name when downloading from a redirected URL

I’m using wget a lot when I need to download a file from the internet to my headless server. But sometimes, the URL I got isn’t ended with a clean file name. Sometimes the URL redirect to another one, sometimes it has parameters. As you know, wget doesn’t handle the file name correctly when you have a non-clean URL.

For example, I want to download a vim script called The NERD tree from its official website. From the web page, we can see that the latest version by the time this article written is v5.0.0. As you can see, the file name is NERD_tree.zip with following URL.

https://www.vim.org/scripts/download_script.php?src_id=23731

This is what happen when I try to download using wget.

$ wget "https://www.vim.org/scripts/download_script.php?src_id=23731"
--2020-03-28 09:50:12--  https://www.vim.org/scripts/download_script.php?src_id=23731
Resolving www.vim.org (www.vim.org)... 202.221.179.29
Connecting to www.vim.org (www.vim.org)|202.221.179.29|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: unspecified [application/octetstream]
Saving to: ‘download_script.php?src_id=23731’

2020-03-28 09:50:13 (341 KB/s) - ‘download_script.php?src_id=23731’ saved [66208]

As you can see, what I get is a file with download_script.php?src_id=23731 as the name. What I want is NERD_tree.zip file name.

This file name is written in Content-Disposition header when doing HTTP request. Luckily, wget can read the content. The following syntax is how to do it.

wget --content-disposition $URL

Now let’s try the --content-disposition parameter to download NERD tree.

$ wget --content-disposition "https://www.vim.org/scripts/download_script.php?src_id=23731"
--2020-03-28 09:52:22--  https://www.vim.org/scripts/download_script.php?src_id=23731
Resolving www.vim.org (www.vim.org)... 202.221.179.29
Connecting to www.vim.org (www.vim.org)|202.221.179.29|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: unspecified [application/octetstream]
Saving to: ‘NERD_tree.zip’

2020-03-28 09:52:23 (317 KB/s) - ‘NERD_tree.zip’ saved [66208]

Now you can see for yourself that the file name is saved as NERD_tree.zip. The file name is correct!

That’s all i can write for today. Hopefully this mini tutorial is useful. Thank you for reading!

References