Commandcurl

Download a file

Saves a URL's response body to a chosen local file and follows any redirects along the way, since curl doesn't follow them by default.

Last updated

Commandcurl
curl -L -o output.zip https://example.com/file.zip

How it works

-o output.zip writes the response body to that file instead of curl's default behavior of dumping the raw bytes straight to your terminal, which is unreadable and often disruptive for anything binary. -L makes curl follow HTTP redirects — a huge share of file-hosting and CDN links respond with a 301/302 pointing at the real object storage URL, and curl does NOT follow those automatically without this flag.

Without -L, a redirected URL downloads only the tiny redirect response itself (a few bytes of HTML or nothing at all), not the actual file — it's easy to think the download "worked" because curl exits cleanly, so always include -L unless you've confirmed the link is a direct, non-redirecting URL.

Watch out for

  • →-O (capital, no filename argument) is a shortcut that saves under the remote server's own filename instead of one you choose — convenient for quick downloads, but risky if the URL doesn't end in a clean, predictable filename.
  • →curl doesn't resume a partial or interrupted download by default; add -C - to resume from where a previous attempt left off, provided the server supports HTTP range requests.
  • →A failed download (404, expired link) still writes an output file by default, containing whatever error page the server returned — add -f (--fail) to make curl exit with an error and skip writing the file on an HTTP failure status instead.

Part by part

-L
Follow redirects. Download links very often redirect to a CDN, and without this you save the redirect page instead of the file.
-o output.zip
Write to this filename. Lowercase o takes a name; uppercase O reuses the name from the URL.

Common mistakes

  • curl https://example.com/file.zip

    With no output flag curl prints the file to the terminal, which for a binary means a screen of noise and often a confused terminal that needs reset. Nothing is saved.

    curl -L -o output.zip https://example.com/file.zip
  • curl -o output.zip https://example.com/file.zip

    Without -L a redirect is saved as the file, so you end up with a few hundred bytes of HTML named output.zip. The giveaway is an archive that will not open and is far too small.

  • curl -L -O https://example.com/download?id=123

    Uppercase O takes the filename from the URL, and a URL ending in a query string has no usable name — curl either errors or writes something unhelpful. Name it explicitly when the URL is not a plain path.

Questions

How do I resume an interrupted download?

Add -C - and curl continues from wherever the local file stopped. The server has to support range requests, which most file hosts do; if it does not, curl restarts from the beginning.

How do I check the file downloaded correctly?

Compare a checksum against the one the publisher provides. A matching hash proves the bytes are identical to what was published — it says nothing about whether the publisher is trustworthy.

Why is my download slower than in a browser?

Browsers open several connections at once. curl uses one, which is usually fine; for very large files a downloader that parallelises ranges will be faster if the server allows it.

What is the difference between curl and wget here?

wget follows redirects and saves to a sensible filename by default, so the equivalent command is shorter. curl is the more flexible tool for constructing requests, which is why it needs the flags spelled out.

Related in Commands