If a script downloads the same file every night, I would rather let the server say whether it changed. HTTP has an ETag for exactly that, and curl can remember it for you.
Two options work as a pair. --etag-save writes the ETag from the response to a file. --etag-compare reads an ETag from a file and sends it back as an If-None-Match header, so the server can answer that nothing changed. The manual suggests using both with the same file name in one command, which saves the new tag if the resource did change:
curl --etag-save etag.txt --etag-compare etag.txt \
-o data.json https://example.com/data.json
On the first run there is no etag.txt yet. The manual says a missing or empty file is treated as an empty ETag, so you simply get a normal download and the tag gets stored. On the next run curl asks the server whether the file still matches.
A few details from the manual that are worth knowing:
- Use both options with a single URL only.
- If the server sends no ETag,
--etag-savecreates an empty file, and then every run is a full download. Not every server sends one, so check withcurl -Ifirst. - The compare file should hold one line with one ETag.
- From curl 8.12.0,
--create-dirsalso creates missing folders for the--etag-savepath.
My caveat: the manual does not say what happens to the -o file when the server answers that nothing changed. Before I pointed this at a file I care about, I would try it on a copy and look at the file afterwards. For a date-based variant, --time-cond does the same job with a timestamp instead of a tag.
Further reading: curl man page: –etag-save and –etag-compare
Going deeper: Guide: your Unraid cache is full and Docker stopped, how to find out why and fix it