brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: curl --etag-save and --etag-compare skip a download that has not changed

Curl
~/tips/tip-curl-etag-save-compare

curl --etag-save etag.txt --etag-compare etag.txt \

Stores a download's ETag and sends it next time, so an unchanged file is not fetched again.

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-save creates an empty file, and then every run is a full download. Not every server sends one, so check with curl -I first.
  • The compare file should hold one line with one ETag.
  • From curl 8.12.0, --create-dirs also creates missing folders for the --etag-save path.

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

More Curl tips curl --trace-ascii – https://example.com/ Retry-After: 120 curl -fsS --fail-early \ url = "https://example.com/data/latest.json" curl -4 -s -o /dev/null -w '%{remote_ip} %{http_code}\n' https://example.com/ All Curl tips (71) All topics

Link