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 URL ranges like [01-05] with -o #1 fetch a numbered series

Curl
~/tips/tip-curl-glob-output-numbers

https://example.com/file[1-100].txt

Expands ranges and lists inside a URL so curl downloads a whole numbered series in one command.

curl can expand ranges in a URL by itself. For a numbered series of files, that replaces a shell loop.

curl has URL globbing. Square brackets give a range, curly braces a list. The manual shows these forms:

https://example.com/file[1-100].txt
https://example.com/file[001-100].txt
https://fun.example/{one,two,three}.jpg
https://example.com/file[1-100:10].txt

Leading zeros work if you write them in the range, letters work through the alphabet, and the :10 at the end is a step, so you get every tenth file. Several ranges next to each other are allowed, nesting is not.

To save every match under its own name, use #1 in the -o option. It is replaced with the current glob text, and #2 is the second glob and so on:

curl "https://example.com/photos/img[01-05].jpg" -o "holiday_#1.jpg"

That downloads five files and saves them as holiday_01.jpg up to holiday_05.jpg. With a list and a range together you can use -o "#1_#2".

The URL has to be in quotes. The manual says so explicitly: the shell would otherwise try to interpret the brackets, and also characters such as &, ? and *.

The caveat is the opposite case. A real URL that contains [, ], { or }, for example an API query with brackets, will be read as a glob and break. Switch it off with -g (--globoff). Numerical IPv6 addresses are detected and excluded automatically, so those still work. Combine the range with --parallel if you want the series to download at the same time, and with --fail if missing numbers should be reported as errors instead of saved as HTML error pages.

Further reading: curl man page: URL globbing, –output and –globoff

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