31

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.

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

1 Answer 1

61

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
  • 3
    You'll want to use --trust-server-names as well if you expect a redirect 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) 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.
    – Josh M.
    Sep 2, 2022 at 20:44

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .