40

A little IoT server returns a file, whose name is given by current date and time, to make it unique. The format is 2018.07.04.18.22.45.dat.

Asking for address XX.XX.XX.XX:5001/read in a browser (with browser cache disabled) the file is returned with its original name.

Using wget, alas, I cannot preserve the file name: wget XX.XX.XX.XX:5001/read returns the proper content but with name read, read.1, read.2, etc.

Is there the possibility to collect it keeping the name, using wget or other commands?

ADDENDUM: using curl XX.XX.XX.XX:5001/read I obtain the raw content instead of the file.

ADDENDUM: as a imperfect workaround, I can generate a filename based on timestamp with wget -o $(date "+%Y.%m.%d-%H.%M.%S.%N.dat") XX.XX.XX.XX:5001/read. Of course it doesn't match the original filename.

7
  • 2
    Have you tried the --trust-server-names option? Commented Jul 4, 2018 at 17:42
  • Yes, @ivanivan, I tried it but got read.X anyway... Commented Jul 4, 2018 at 17:47
  • 6
    Have you tried the --content-disposition option? Commented Jul 4, 2018 at 17:59
  • @Nick ODell, thank you! It works. Please move your comment to an answer so I can upvote it. Commented Jul 4, 2018 at 18:02
  • 4
    Does this answer your question? Download file with actual name by wget Commented Jul 19, 2020 at 16:49

1 Answer 1

74

Use wget --content-disposition <url>

Explanation: The Content-Disposition header can be used by a server to suggest a filename for a downloaded file. By default, wget uses the last part of the URL as the filename, but you can override this with --content-disposition, which uses the server's suggested name.

More information can be found in the manual.

3
  • 5
    You'll want to use --trust-server-names as well if you expect a redirect Commented Apr 11, 2021 at 18:34
  • 1
    curl -JLO ... seems to handle more efficently the name from the server. It avoids the double repeated name problem (XYZ.shXYZ.sh) Commented Jan 5, 2022 at 11:57
  • Big J Lo fan, eh? BTW curl -JLO and wget --content-disposition both do not work perfectly. cURL will use the non-utf-8 file name, and wget will concat the non-utf-8 filename with the utf-8 one, and it won't decode the utf-8 one, either. Commented Sep 2, 2022 at 20:44

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.