Tutorial

How to Download Files with cURL

Updated on May 27, 2025
authorauthor

By bhogan and Vinayak Baranwal

How to Download Files with cURL

Client URL, or cURL, is a library and command-line utility for transferring data between systems. It supports many protocols and tends to be installed by default on many Unix-like operating systems. Because of its general availability, it is a great choice for downloading a file to your local system, especially in a server environment.

In this tutorial, you’ll use the curl command to download a text file from a web server. You’ll view its contents, save it locally, and tell curl to follow redirects if files have moved. This knowledge is particularly useful when working with REST APIs or setting up Node.js applications.

Downloading files from the Internet can be dangerous, so be sure you are downloading from reputable sources. In this tutorial, you’ll download files from DigitalOcean, and you won’t be executing any files you download.

Step 1 — Fetching remote files

Out of the box, without any command-line arguments, the curl command will fetch a file and display its contents to the standard output.

Let’s give it a try by downloading the robots.txt file from Digitalocean.com:

curl https://www.digitalocean.com/robots.txt

You’ll see the file’s contents displayed on the screen:

Output
User-agent: * Disallow: sitemap: https://www.digitalocean.com/sitemap.xml sitemap: https://www.digitalocean.com/community/main_sitemap.xml.gz sitemap: https://www.digitalocean.com/community/questions_sitemap.xml.gz sitemap: https://www.digitalocean.com/community/users_sitemap.xml.gz

Give curl a URL and it will fetch the resource and display its contents.

Saving Remote Files

Fetching a file and displaying its contents is all well and good, but what if you want to actually save the file to your system?

To save the remote file to your local system, with the same filename as the server you’re downloading from, add the --remote-name argument, or use the -O option:

curl -O https://www.digitalocean.com/robots.txt

Your file will download:

Output
% Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 286 0 286 0 0 5296 0 --:--:-- --:--:-- --:--:-- 5296

Instead of displaying the contents of the file, curl displays a text-based progress meter and saves the file to the same name as the remote file’s name. You can check on things with the cat command:

cat robots.txt

The file contains the same contents you saw previously:

Output
User-agent: * Disallow: sitemap: https://www.digitalocean.com/sitemap.xml sitemap: https://www.digitalocean.com/community/main_sitemap.xml.gz sitemap: https://www.digitalocean.com/community/questions_sitemap.xml.gz sitemap: https://www.digitalocean.com/community/users_sitemap.xml.gz

Now let’s look at specifying a filename for the downloaded file.

Step 2 — Saving Remote Files with a Specific File Name

You may already have a local file with the same name as the file on the remote server.

To avoid overwriting your local file of the same name, use the -o or --output argument, followed by the name of the local file you’d like to save the contents to.

Execute the following command to download the remote robots.txt file to the locally named do-bots.txt file:

curl -o do-bots.txt  https://www.digitalocean.com/robots.txt

Once again, you’ll see the progress bar:

Output
% Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 286 0 286 0 0 6975 0 --:--:-- --:--:-- --:--:-- 7150

Now use the cat command to display the contents of do-bots.txt to verify it’s the file you downloaded:

cat do-bots.txt

The contents are the same:

Output
User-agent: * Disallow: sitemap: https://www.digitalocean.com/sitemap.xml sitemap: https://www.digitalocean.com/community/main_sitemap.xml.gz sitemap: https://www.digitalocean.com/community/questions_sitemap.xml.gz sitemap: https://www.digitalocean.com/community/users_sitemap.xml.gz

By default, curl doesn’t follow redirects, so when files move, you might not get what you expect. Let’s look at how to fix that.

Step 3 — Following Redirects

Thus far, all of the examples have included fully qualified URLs that include the https:// protocol. If you happened to try to fetch the robots.txt file and only specified www.digitalocean.com, you would not see any output, because DigitalOcean redirects requests from http:// to https://:

You can verify this by using the -I flag, which displays the request headers rather than the contents of the file:

curl -I www.digitalocean.com/robots.txt

The output shows that the URL was redirected. The first line of the output tells you that it was moved, and the Location line tells you where:

Output
HTTP/1.1 301 Moved Permanently Cache-Control: max-age=3600 Cf-Ray: 65dd51678fd93ff7-YYZ Cf-Request-Id: 0a9e3134b500003ff72b9d0000000001 Connection: keep-alive Date: Fri, 11 Jun 2021 19:41:37 GMT Expires: Fri, 11 Jun 2021 20:41:37 GMT Location: https://www.digitalocean.com/robots.txt Server: cloudflare . . .

You could use curl to make another request manually, or you can use the --location or -L argument which tells curl to redo the request to the new location whenever it encounters a redirect. Give it a try:

curl -L www.digitalocean.com/robots.txt

This time, you see the output as curl followed by the redirect:

Output
User-agent: * Disallow: sitemap: https://www.digitalocean.com/sitemap.xml sitemap: https://www.digitalocean.com/community/main_sitemap.xml.gz sitemap: https://www.digitalocean.com/community/questions_sitemap.xml.gz sitemap: https://www.digitalocean.com/community/users_sitemap.xml.gz

You can combine the -L argument with some of the aforementioned arguments to download the file to your local system:

curl -L -o do-bots.txt www.digitalocean.com/robots.txt

Warning: Many resources online will ask you to use curl to download scripts and execute them. Before you run any scripts you have downloaded, it’s good practice to check their contents before making them executable and running them. Use the less command to review the code to ensure it’s something you want to run.

Step 4 — Downloading Files with Authentication

Some web files are protected and require authentication. curl allows you to handle these cases easily. This is particularly useful when working with proxy servers or secure API endpoints.

Basic Authentication (Username & Password)

To access a file that requires login credentials, use the -u flag:

curl -u username:password -O https://example.com/securefile.zip

Token-Based Authentication

You can also use headers to pass API tokens:

curl -H "Authorization: Bearer YOUR_TOKEN" -O https://api.example.com/protected/data.json

For better security, avoid hardcoding sensitive data. Instead, use environment variables or configuration files.

Step 5 — Handling Timeouts, Retries, and Resuming Downloads

Robust scripts need to account for network interruptions and delays.

Resume Interrupted Downloads

Use the -C - option to resume:

curl -C - -O https://example.com/largefile.iso

Set Timeouts

Prevent curl from hanging indefinitely:

curl --max-time 30 -O https://example.com/file.txt

Retry Failed Downloads

Automatically retry failed downloads up to 3 times:

curl --retry 3 -O https://example.com/file.txt

Step 6 — Automating Downloads with Shell Scripts

Automating downloads can be useful in CI/CD pipelines or regular backups. This is especially relevant when working with Node.js applications or REST APIs that require regular data updates.

Sample Script

#!/bin/bash
URL="https://example.com/file.zip"
DEST="/home/user/downloads/file.zip"
curl -L -o "$DEST" "$URL"

Make the script executable with chmod +x script.sh, then schedule it with cron or use it in a deployment pipeline.

Step 7 — Troubleshooting Common Download Issues

Sometimes downloads might fail or behave unexpectedly. Here are some common issues and their solutions:

File Not Downloading

If curl isn’t downloading your file, try these troubleshooting steps:

curl -I https://example.com/file.zip
  • Verify if the server requires a specific user agent:
curl -A "Mozilla/5.0" -O https://example.com/file.zip
  • Check for SSL/TLS issues:
curl -v -O https://example.com/file.zip
  • Try with different protocols if available:
# Try HTTP if HTTPS fails
curl -O http://example.com/file.zip
  • Check if the file exists and you have permissions:
curl -u username:password -O https://example.com/file.zip

If you’re still having issues, the verbose output (-v) will help identify the problem:

curl -v -O https://example.com/file.zip

Step 8 — Using wget as an Alternative

While curl is powerful, sometimes wget might be a better choice for certain download scenarios. wget is specifically designed for downloading files and has some features that make it particularly useful:

Basic wget Usage

To download a file with wget:

wget https://example.com/file.zip

Key wget Features

  1. Automatic retry on failure:
wget -t 3 https://example.com/file.zip
  • Download in the background:
wget -b https://example.com/file.zip
  • Limit download speed:
wget --limit-rate=200k https://example.com/file.zip
  • Download entire websites:
wget --mirror --convert-links --adjust-extension --page-requisites --no-parent https://example.com

When to Choose wget over curl

  • When you need recursive downloading
  • For mirroring entire websites
  • When you want automatic retry functionality
  • For simpler download commands (wget has fewer options to remember)

When to Stick with curl

  • When you need to interact with APIs
  • For more complex HTTP requests
  • When you need to send data to servers
  • For better script integration

Frequently Asked Questions (FAQ)

1. What is the difference between -O and -o in cURL?

The -O (uppercase “o”) option in curl saves the downloaded file using the original filename as provided by the server in the URL or HTTP headers. This is particularly useful when you want to preserve the server’s naming convention, or when downloading multiple files in a batch without having to specify each filename manually.

On the other hand, the -o (lowercase “o”) option allows you to specify a custom filename for the downloaded file. This is helpful for organizing your downloads, preventing filename collisions, or when you want to give the file a more meaningful name locally.

Example:

curl -O https://example.com/file.zip         # Saves as file.zip
curl -o custom-name.zip https://example.com/file.zip   # Saves as custom-name.zip

2. How do I resume an interrupted download with cURL?

If your download was interrupted due to a network error or system reboot, you can resume it using the -C - option. This tells curl to continue the download from where it left off, assuming the server supports HTTP range requests.

Example:

curl -C - -O https://example.com/largefile.iso

This is particularly useful for large ISO files or multi-gigabyte datasets, especially when working with unreliable internet connections or automating downloads in scripts.

3. Can I download files behind authentication with cURL?

Yes, curl supports both basic and token-based authentication methods.

For basic authentication using a username and password:

curl -u username:password -O https://secure.example.com/file.zip

For token-based authentication, use the Authorization header:

curl -H "Authorization: Bearer YOUR_TOKEN" -O https://api.example.com/file.zip

Always avoid hardcoding sensitive credentials. Use environment variables or configuration files when scripting to enhance security.

4. What should I do if the download URL redirects?

Some URLs redirect from http to https, or from an old endpoint to a new one. By default, curl does not follow these redirects.

To handle this, add the -L or --location option:

curl -L -O http://example.com/download

This is especially important when accessing public APIs, shortened URLs, or migrating download links.

5. Is cURL available on Windows?

Yes, cURL is included by default in Windows 10 and later. You can use it directly from Command Prompt or PowerShell.

For older versions, or for enhanced Unix-like behaviour, you can install it via:

Once installed, you can run cURL commands just like on Linux or macOS. This enables cross-platform scripting and consistent development workflows.

6. How can I download multiple files at once with cURL?

You can download multiple files in a single command using either a list of URLs or a pattern. Here are two common approaches:

Using multiple URLs:

curl -O https://example.com/file1.zip -O https://example.com/file2.zip

Using a pattern with brace expansion:

curl -O https://example.com/file{1..5}.zip

For more complex scenarios, you can also use a text file containing URLs and the -K option:

# urls.txt contains one URL per line
curl -K urls.txt

7. How do I handle SSL/TLS certificate issues with cURL?

Sometimes, you might encounter SSL certificate errors when downloading from servers with expired or self-signed certificates (SSL). While not recommended for production use, you can bypass certificate verification using the -k or --insecure option:

curl -k -O https://example.com/file.zip

For a more secure approach, you can specify a custom certificate:

curl --cacert /path/to/certificate.pem -O https://example.com/file.zip

Remember that bypassing certificate verification can expose you to security risks, so use these options cautiously.

8. How can I monitor download progress and speed with cURL?

By default, cURL shows a progress bar, but you can customise the output using various options:

For a simple progress bar:

curl -# -O https://example.com/file.zip

For detailed progress information:

curl -w "\nDownloaded: %{size_download} bytes\nSpeed: %{speed_download} bytes/sec\nTime: %{time_total} seconds\n" -O https://example.com/file.zip

You can also create a custom progress format using the -w option with various variables like %{speed_download}, %{time_total}, and %{size_download}.

Conclusion

curl lets you quickly download files from a remote system. It supports a wide range of protocols like HTTP, HTTPS, FTP, and more—making it a reliable and script-friendly choice for file transfers. But it doesn’t stop there.

From simple downloads to complex API interactions, curl can handle everything from setting custom headers and authentication to managing redirects and resumable downloads. It’s a staple tool for developers, sysadmins, and DevOps engineers who need precise control over network communication without relying on heavyweight tools.

Whether you’re automating tasks in a CI/CD pipeline, integrating data from external sources, or testing endpoints in REST APIs or Node.js applications, curl fits naturally into modern development workflows.

To dive deeper into all its capabilities, view the manual page by running:

man curl

Or explore online examples to sharpen your command-line skills even further.

Continue building with DigitalOcean Gen AI Platform.

About the author(s)

Category:
Tutorial

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
2 Comments
Leave a comment...

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Tested on Ubuntu 20.04 with Interactive terminal: Great! OK

How can I download .mkv or .mp4 files in ubuntu bash?

Join the Tech Talk
Success! Thank you! Please check your email for further details.

Please complete your information!

Become a contributor for community

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

DigitalOcean Documentation

Full documentation for every DigitalOcean product.

Resources for startups and SMBs

The Wave has everything you need to know about building a business, from raising funding to marketing your product.

Get our newsletter

Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

New accounts only. By submitting your email you agree to our Privacy Policy

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.