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.
This is what happen when I try to download using wget
.
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.
Now let’s try the --content-disposition
parameter to download NERD tree.
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!