How to use content-dispositon header to determine filename

Use Content-Disposition HTTP response header to determine filename and use it to store downloaded file.

Inspect HTTP headers for a file that will be used in the following examples.

$ curl --head https://exmaple.org/dokuwiki-stable.tgz
HTTP/1.1 200 OK
Server: nginx/1.10.3
Date: Fri, 23 Mar 2018 19:39:14 GMT
Content-Type: application/octet-stream
Content-Length: 3505643
Last-Modified: Fri, 23 Mar 2018 19:30:51 GMT
ETag: "5ab4d75b-357deb"
Content-Disposition: attachment; filename="dokuwiki-stable-2017-02-19e.tgz"
Accept-Ranges: bytes

curl

Write output to a local file using filename extracted from the URL.

$ curl --remote-name --write-out "Downloaded %{filename_effective} file" --silent https://exmaple.org/dokuwiki-stable.tgz
Downloaded dokuwiki-stable.tgz file

Write output to a local file using name specified in Content-Disposition HTTP response header.

$ curl --remote-name --remote-header-name --write-out "Downloaded %{filename_effective} file" --silent https://exmaple.org/dokuwiki-stable.tgz
Downloaded dokuwiki-stable-2017-02-19e.tgz file

wget

Write output to a local file using filename extracted from the URL.

$ wget --no-verbose https://wiki.fishsilentcruise.space/dokuwiki-stable.tgz
2018-03-23 19:54:29 URL:https://exmaple.org/dokuwiki-stable.tgz [3505643/3505643] -> "dokuwiki-stable.tgz" [1]

Write output to a local file using name specified in Content-Disposition HTTP response header.

$ wget --no-verbose --content-disposition https://wiki.fishsilentcruise.space/dokuwiki-stable.tgz
2018-03-23 19:54:53 URL:https://exmaple.org/dokuwiki-stable.tgz [3505643/3505643] -> "dokuwiki-stable-2017-02-19e.tgz" [1]

About Milosz Galazka

Milosz is a Linux Foundation Certified Engineer working for a successful Polish company as a system administrator and a long time supporter of Free Software Foundation and Debian operating system.