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 -z downloads a file only if it is newer than the one you have

Curl
~/tips/tip-curl-time-cond-z

curl -R -z feed.xml -o feed.xml https://example.com/feed.xml

Downloads a file only when the server's copy is newer than your local file.

A cron job that downloads a list every night is wasteful when the list rarely changes. With -z I can tell curl to fetch the file only if the server has a newer one.

-z (--time-cond) takes a date, or a file name. If the argument does not look like a date, curl reads the modification time of that file. So you can point it at your local copy:

curl -R -z feed.xml -o feed.xml https://example.com/feed.xml

The manual describes the effect as requesting a file that has been modified later than the given time. The -R (--remote-time) part sets the downloaded file’s timestamp to the server’s one, which makes the next comparison line up. The manual lists --remote-time as the related option for this reason.

Starting the date with a dash flips the condition and asks for a document that is older than the date:

curl -z "-Wed 01 Sep 2021 12:18:00" https://example.com/file

The example dates are the ones from the manual. Date strings follow the curl_getdate rules, and a plain file name is the easiest way to avoid thinking about them.

Caveats. If you pass a file that does not exist, curl prints a warning and does the transfer without any time condition, so the first run downloads normally, which is what you want. The manual does not describe what happens to the file given with -o when nothing newer exists, so I would try the command on a copy before pointing it at data I care about. And a server that does not support the condition at all will just send the file again.

When the server sends ETags, --etag-compare is the other way to do the same thing, and the manual mentions it under related options.

Further reading: curl man page: –time-cond and –remote-time

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