The answer to the Stackoverflow question sounds like this:
Do two requests: a HEAD to get the file name from response header, then a GET:
url="http://www.vim.org/scripts/download_script.php?src_id=10872"
filename="$(curl -sI "$url" | grep -o -E 'filename=.*$' | sed -e 's/filename=//')"
curl -o "$filename" -L "$url"
It assumes a pretty simple format for the Content-Disposition header, which is most of the time not true. Meanwhile curl has a new option called
--remote-header-name
(or just
-J
), which does exactly what we intend ... ie. takes the filename for the
--remote-name
option from the Content-Disposition header. Actually even this option of curl was flawed at some point. So to be sure you might take the matter into your own hands and use a proper sed command to fetch the filename from the Content-Disposition line:
url="http://www.vim.org/scripts/download_script.php?src_id=10872"
filename="$(curl -sIL "$url" | sed -r -e 's/^ *Content-Disposition[ \t]*:[ \t]*[^ \t;]+;[ \t]*filename[ \t]*=[ \t]*("(([^"]|\")*)".*|([^; \t\r"]+)(([^;\r]*[^; \t\r]+)*)[ \t]*(;.*|[\r]?)$)/\2\4\5/' -e 't' -e 'd')"
[ -n "$filename" ] && curl -o "$filename" -L "$url" || curl -OL "$url"
You might have noticed that my regular expression allows for some irregularities in the Content-Disposition header (eg. HTTP headers should not start with a whitespace ... unless it's a folded header which started in a previous line). That's totally intentional.
Recent comments
2 weeks 3 hours ago
3 weeks 12 hours ago
9 weeks 4 days ago
9 weeks 4 days ago
9 weeks 6 days ago
10 weeks 15 hours ago
10 weeks 1 day ago
10 weeks 2 days ago
10 weeks 3 days ago
10 weeks 4 days ago