Get file creation date via HTTP

Given the file on the web server (for example, http://foo.com/bar.zip โ†’ is only available via HTTP), is there a way to get date attributes (for example, date [created, changed]) without downloading the entire archive in the first place ?

Now I download the archive and read the attributes programmatically. The problem is that there are dozens of MiBs in the archive, so it seems like itโ€™s a waste of resources to download the whole thing, and as a result reads only a couple of bytes of information.

I understand that bandwidth is almost free, but I don't like squandering anyway.

+6
language-agnostic attributes download
source share
3 answers

Try reading Last-Modified from the header.

+7
source

Be sure to use the HTTP HEAD request instead of the HTTP GET request to read only HTTP headers. If you are doing an HTTP GET, you will still download the entire file, even if you decide to check the HTTP headers.

+6
source

Just for simplicity, here is a compilation of existing (ideal) answers from @ihorko and @ JanThomรค, which uses curl. Of course, another option is available, but here is a fully functional answer.

Use curl with the -I option:

-I, --head
(HTTP / FTP / FILE) Retrieve only the HTTP header! HTTP servers have a HEAD command, which is used to get nothing but the title of a document. When used in an FTP or FILE file, the curl displays only the file size and the time of the last modification.

Also, the -s is nice here:

-s, --silent
Quiet or calm mode. Do not show progress indicators or error messages. Makes twisting dumb. It will still output the data you request, potentially even to the terminal / stdout, unless you redirect it.

Therefore, something like this will do the trick:

 curl -sI http://foo.com/bar.zip | grep 'Last-Modified' | cut -d' ' -f 2- 
+1
source

All Articles