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 --max-filesize refuses a download that is bigger than you expect

Curl
~/tips/tip-curl-max-filesize

curl --max-filesize 50M -O https://example.com/blocklist.txt

Stops curl from downloading a file that is larger than the size limit you set.

When a script fetches a small config or a feed, I do not want it to quietly pull a 4 GB file because a URL changed. --max-filesize sets a ceiling.

The option takes a size in bytes, with an optional unit suffix. k, M, G, T and P are accepted and, according to the manual, count in units of 1024:

curl --max-filesize 50M -O https://example.com/blocklist.txt
echo $?

If the server announces a file larger than the limit, the transfer does not start and curl returns exit code 63. A script can check for that value and handle it separately from other failures:

curl --max-filesize 50M -sS -O https://example.com/blocklist.txt
if [ $? -eq 63 ]; then echo "file too big, skipping"; fi

Setting the maximum to zero turns the limit off again.

The caveat is about files whose size is not known before the download starts. The manual says that before curl 8.4.0 the option had no effect for such files, even if the download ended up larger than the limit. Since 8.4.0, curl aborts the transfer when it reaches the threshold during the download. So on an older curl, --max-filesize is not a safe guard for dynamic URLs. Check curl --version if the script runs on a long-lived machine.

The manual adds that since curl 8.20.0 the option also stops transfers that would reach the threshold through automatic decompression with --compressed. I read that as a warning: on an older curl, a compressed download that unpacks to something big may not be caught by the limit.

The option is documented for FTP, HTTP and MQTT. For a plain slow connection, --limit-rate is the sibling that the manual points to.

Further reading: curl man page: –max-filesize

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