Curl Download File Example

Curl prints what it downloads to the terminal unless told to save it. Two flags do the saving, and a handful more make the difference between a download command and one you can trust in a script.

Last updated . How this page is checked.

The command

curl -fLO https://example.com/files/report.pdf

What the flags do

FlagWhat it does
-OSave under the filename from the URL. Long form: --remote-name.
-o nameSave under a name you choose.
-LFollow redirects. Download links very often redirect.
-fFail on HTTP errors rather than saving the error page.
-C -Resume a partial download from where it stopped.
--limit-rate 2MCap the transfer speed.

Naming the file

-O uses the last segment of the URL as the filename; -o lets you set it, including a path. If the URL has no useful final segment, for instance a download endpoint with an id in the query string, combine -O with -J and curl takes the name from the server's Content-Disposition header. --create-dirs makes any missing directories in an -o path.

curl -fL -o tools/report-2026.pdf --create-dirs https://example.com/files/report.pdf
curl -fLOJ "https://example.com/download?id=8841"

# several files in one command, reusing the connection
curl -fL -O https://example.com/a.zip -O https://example.com/b.zip

Downloads you can rely on

Three flags belong on almost every scripted download. -L, because release assets and CDN links redirect and without it you save a few bytes of redirect body. -f, because otherwise a 404 page is written to disk under the name of the file you wanted and the script carries on. And -C - when the file is large: curl looks at the size of the partial file and asks the server for the remainder with a Range request.

Verify what you got. A download finishing without error says nothing about whether the content is what the publisher intended.

curl -fL -C - -O https://example.com/big.iso      # rerun to resume
sha256sum big.iso                                  # compare with the published checksum

Common mistakes

  • Resuming needs server support. If the server ignores Range, curl reports that it cannot resume, and you start again.
  • -O on a URL ending in a slash fails because there is no filename to use. Give one with -o.
  • Piping a download into a shell (curl ... | sh) runs whatever arrives, including a truncated script. Download, inspect, then run.

Related curl examples

More under Network behaviour

References

These links leave api-tester.com, which is independent of the projects they point to.

FAQ

How do I download a file with curl?

Use curl -O URL to save it under its remote filename, or curl -o name URL to choose the filename. Add -L to follow redirects.

How do I resume a download with curl?

Run the same command with -C - added. Curl checks the partial file size and requests only the remaining bytes.

What is the difference between curl -o and -O?

Lowercase -o takes a filename you specify. Uppercase -O takes no argument and uses the filename from the URL.