Ask for a byte range with -r
Curl sends a Range header when you give it the -r option, and the numbers are byte offsets that start at zero. Asking for 0-99 returns the first hundred bytes, because both ends of the range are included. Leaving the end off, as in 900-, asks for everything from byte 900 to the end of the file. A negative-looking form such as -100 asks for the final hundred bytes, which is handy for reading the tail of a log or the footer of an archive.
curl -r 0-99 -o first-100-bytes.bin https://example.com/archive.bin curl -r 900- -o from-900.bin https://example.com/archive.bin curl -r -100 -o last-100-bytes.bin https://example.com/archive.bin
A protected file needs its credential sent in a header on every one of these requests. With a capability-style service the header looks like -H "Authorization: Capability <placeholder>", and a read-only credential is enough because a range request only reads. Keep the value out of shell history and shared logs, following the habit you would use for any bearer secret.
The sizes in the figure below come from an imaginary file of 1,000 bytes, chosen so the arithmetic is easy to check by hand. They illustrate how the offsets work and are not measurements from any server.
| Curl option | Bytes returned | Part of the file |
|---|---|---|
| Whole file | 1,000 | Bytes 0 to 999 |
-r 0-99 | 100 | Start of the file |
-r 500-599 | 100 | Middle of the file |
-r 900- | 100 | End of the file |
-r -100 | 100 | End of the file |
Confirm the server honoured the range
A server is free to ignore a Range header, and the HTTP specification says it may answer with the full file instead. In that case curl saves a complete file under the name you chose for a fragment, which can go unnoticed until something downstream fails. Look at the response status before you rely on the bytes. Adding -i prints the headers, and -w "%{http_code}\n" prints only the status code after the transfer.
curl -i -r 0-99 -o /dev/null https://example.com/archive.bin
A status of 206 means the server sent a partial body, and its Content-Range header states which bytes arrived and the total size, in the form bytes 0-99/1000. Receiving 200 means you were sent the whole file. The 416 status means the range lies outside the file, which usually happens when you ask for an offset beyond the end, and the reply then reports the real size so you can correct the request.
The total size in Content-Range is also useful on its own. It lets you plan a series of ranges, or learn how big a file is by requesting a single byte, without downloading anything substantial. A HEAD request with curl -I gives the same size through the Content-Length header, along with an Accept-Ranges header that says whether the server offers ranges at all.
Resume a stopped download with -C -
When a large download stops part way, curl can pick up where it left off. The option -C - tells curl to inspect the output file already on disk, take its length as the starting offset and request everything after it. You must name the output file with -o or -O so curl knows which partial file to inspect, and you must reuse the same name on the retry.
curl -C - -o archive.bin https://example.com/archive.bin
Curl appends the new bytes to the existing file, so the result is only as good as the match between the partial file and the remote one. If the server answers with 200 instead of 206, curl stops with an error saying the server does not seem to support byte ranges, which protects you from appending a full copy to a fragment. Treat that error as a signal to delete the partial file and start again.
The continue option belongs to downloads. An upload that fails part way follows whatever recovery procedure the receiving service documents, and the same flag does not rescue it. Our note on resuming an interrupted upload covers that direction.
Check that the file did not change between attempts
Resuming joins two separate responses, and it assumes the remote file stayed the same between them. If a build job replaced the file while your connection was down, the first part of the old version and the rest of the new one produce a corrupt archive that still has the right length. Neither curl nor the server can tell you this happened unless you ask a question that exposes it.
Servers that provide an ETag let you tie a resume to a specific version. Fetching the headers before the first attempt records the validator, and the If-Range header sends it back with the range request so the server returns the partial body only when the file is unchanged, and the whole new file otherwise. Support for If-Range varies, so confirm it against your server before depending on it. Where the service lets you name a pinned version in the request, using that name for every retry is a stronger guarantee than trusting whatever is current.
Compression adds one more trap to a resumed transfer. Ranges count the bytes as the server sends them, so combining --compressed with a partial request can give offsets that refer to the encoded stream rather than the file you can see. Leave compression off when you are slicing or resuming binary files.
Verify the finished file
A matching length is weak evidence that a resumed file is right. Compare a content digest of the finished file with a value you obtained from a trusted source before the transfer, such as the output of sha256sum on the sender's side. Keep that check in the script so a corrupt join fails loudly instead of being passed along to the next stage.
Services that store arbitrary bytes under a name can serve byte ranges of a stored object too. svc.nz does this for its stored objects, though hosted access is currently an invite-only private trial, so the same techniques apply to any server that answers range requests correctly. Start by running the HEAD request and the one-byte range request from this page against your own server, then read the status codes before you build a resume loop around it.
FAQ
Why did curl give me the whole file when I asked for a range?
The server may ignore range requests, in which case it answers with a 200 status and the full body. Check the status line and the Content-Range header before trusting the output. A server that supports ranges replies with 206 and names the bytes it sent.
Can I request several ranges in one curl command?
Curl accepts a comma-separated list of ranges, but a server may answer with a multipart body or with the whole file. Many simple servers honour only one range per request. Send separate requests when you need a predictable result.
Does the -C - option work for uploads?
The continue option is meant for downloads. Resuming an upload depends on a procedure the receiving service documents, so the same flag cannot be relied on to recover a stopped upload.
Sources
HTTP Semantics: range requests; curl documentation: the range option; curl documentation: continuing transfers.