How to Use cURL to Download Files: A Step-by-Step Guide
Tutorials

Justas Vitaitis
Key Takeaways
-
cURL is a powerful command line tool that supports downloading over many protocols, making it ideal for scripting and automation.
-
You can use cURL to download multiple files by combining it with shell loops, PowerShell, or by using a configuration file with the --config option.
-
Customizing request headers, setting a user agent, or using authentication helps you handle protected or complex downloading tasks with ease.
cURL is a command line tool that helps developers interact with servers. It lets you send or get data using different internet protocols, and people often use it to download software, scripts, config files, images, and more from a remote server.
We’ll break down how to download files with cURL and show you how flexible it can be in downloading both single and multiple files.
What Is cURL and Why Use It to Download Files?
cURL stands for “Client URL”. It’s a free, open-source command line tool used for sending and receiving data using URL syntax. It supports protocols like HTTP, HTTPS, FTP, and many more. On most Unix-like operating systems like macOS and Linux, it comes pre-installed.
You’ll find cURL useful for:
- Sending GET or POST requests.
- Automating downloads.
- Testing APIs.
- Uploading or downloading content from servers.
Because of its broad support for protocols and simple usage, many developers prefer cURL to download files instead of using a browser or another app.
Basic Syntax to Download a File Using cURL
Here’s the most basic way to use cURL to download a file:
curl -O https://iproyal.com/robots\.txt
It saves the remote file with the same name locally. Let’s break down the common flags:
- -O (uppercase o). Saves the file with its original name.
- -o filename. Let you choose a custom filename.
- -L. Follows redirects if the URL changes.
Redirects happen more often than you would think. Some URLs don’t point straight to the file but instead pass you along. Learn how to follow redirects using cURL to avoid errors during your file downloads.
Downloading Files With cURL - Common Examples
There are several methods you can use to download files with cURL, and here’s a quick overview of the most common ones.
1. Basic File Download
curl -O https://cms.iproyal.com/uploads/IPR_Web_Preview_2845a84108.jpg
Running the command above will download one of IPRoyal’s brand images. Use the command when you want to save a remote file exactly as it is. It’s great for images, patches, or configuration files.
2. Download With Custom Filename
curl -o custom.jpg https://cms.iproyal.com/uploads/IPR_Web_Preview_2845a84108.jpg
With this line, you can name the file whatever you want. It’s useful when you want consistent naming for scripts or batch jobs.
The remainder of cURL commands are situational and are trickier to show perfect examples of, so we’ll be using placeholder URLs.
3. Resume an Interrupted Download
curl -C - -O https://example.com/largefile.zip
It will resume the download where it left off. If your downloading session breaks mid-way, you don’t need to start all over again.
4. Download Behind Redirects
curl -L -O https://short.url/file
Many links, especially from services like Dropbox or Google Drive, use redirects. Without -L, cURL won’t follow them, so the download may fail.
5. Use With Proxy or Authentication
curl -u user:password -O https://example.com/protected.zip
Or if you want to set a user agent for more control:
curl -A "MyAgent/1.0" -O https://example.com/data.json
For more secure areas or APIs, using proper cURL authentication can help you download files more safely.
Downloading Multiple Files With cURL
You can also download multiple files at once using a few different methods.
1. Multiple -O Commands
curl -O https://example.com/file1.txt -O https://example.com/file2.txt
Quick and easy when you know the exact URLs. Just list them one after the other.
2. Loop Through a .txt List
First, create a text file with URLs, one per line:
https://example.com/file1.txt
https://example.com/file2.txt
Then, on Unix-like systems (Linux, macOS, WSL), run:
while read url; do curl -O "$url"; done < urls.txt
It tells the shell to read each line from urls.txt and pass it to cURL. However, cURL itself cannot directly parse a .txt list of URLs. The loop works in bash or similar shells, but not in Windows CMD.
On Windows, you have two options: using PowerShell or writing a simple batch script with each URL in its own line. If you’re using PowerShell:
Get-Content urls.txt | ForEach-Object { curl.exe -O $_ }
3. Batch Script for Windows
Create a download.bat:
curl -O https://example.com/file1.zip
curl -O https://example.com/file2.zip
Run the batch script to download files one by one. Bat files are a simple automation tool that helps keep things tidy when working with bulk files.
Handling Output and File Saving
Sometimes you may not want to save the downloaded file with its original name. Or you don’t want it to overwrite existing files.
Use -o for custom names, like in our previous example, but if you want to send output to stdout instead of saving, there’s also the option of skipping the -o or -O:
curl https://example.com/data.json
To avoid overwriting, you can add a check in your script or use different names. Some like to include timestamps in the filename, for example.
You can also use built-in flags like -i (include headers) or -D - (print headers). For more details, check out our detailed guide on showing response headers in cURL .
Conclusion
Using cURL to download files is fast, flexible, and powerful. You can fetch a single file, download multiple files with a script, or resume broken downloads.
If you’d like to compare tools, check out our breakdown of cURL vs Wget to help you decide which one fits your needs best: a general-purpose transfer tool (cURL) or a file-centric downloader (Wget).