32

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.

Alex Poca
  • 501

1 Answers1

62

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.

Nick ODell
  • 2,608
  • 3
    You'll want to use --trust-server-names as well if you expect a redirect – forresthopkinsa Apr 11 '21 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) – MUY Belgium Jan 05 '22 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 02 '22 at 20:44