cURL is a powerful command-line tool used for transferring data with URLs. It supports multiple protocols, including HTTP, HTTPS, FTP, and SFTP, making it an essential utility for developers, system administrators, and automation tasks. One of its most common uses is downloading files from the internet.
This guide will explain how to use cURL to download files, covering various use cases, options, and troubleshooting tips.
1. What Is cURL?
cURL (Client URL) is a free and open-source tool for sending and receiving data over a network. It is widely used for:
-
Downloading and uploading files.
-
Interacting with APIs.
-
Automating web requests.
-
Debugging network issues.
Most Linux distributions, macOS, and Windows (with recent updates) come with cURL pre-installed.
1.1 Checking if cURL Is Installed
To verify if cURL is installed, run:
curl --version
If not installed, you can install it using:
-
Ubuntu/Debian:
sudo apt install curl -y
-
CentOS/RHEL:
sudo dnf install curl -y
-
MacOS (Homebrew):
brew install curl
-
Windows: Download from the official cURL website or use WSL.
2. Basic cURL Command to Download a File
2.1 Simple File Download
The most basic way to download a file with cURL is:
curl -O https://example.com/file.zip
Here, -O
(uppercase "O") saves the file with its original name from the server.
2.2 Save with a Custom Name
If you want to save the file with a different name, use:
curl -o newname.zip https://example.com/file.zip
The -o
(lowercase "o") option specifies the output filename.
3. Downloading Multiple Files with cURL
To download multiple files at once, use:
curl -O https://example.com/file1.zip -O https://example.com/file2.zip
Alternatively, if the files follow a pattern:
curl -O https://example.com/file[1-3].zip
This command will download file1.zip
, file2.zip
, and file3.zip
.
4. Downloading Files from an FTP Server
If you need to download a file from an FTP server, use:
curl -u username:password -O ftp://ftp.example.com/file.zip
For anonymous FTP downloads:
curl -O ftp://ftp.example.com/publicfile.zip
5. Resuming an Interrupted Download
If a download was interrupted, you can resume it using the -C -
option:
curl -C - -O https://example.com/file.zip
This is useful for large files or unstable connections.
6. Downloading with Authentication
6.1 Basic Authentication
Some files require authentication. Use -u
to pass credentials:
curl -u user:password -O https://example.com/protectedfile.zip
6.2 Token-Based Authentication
For API-based authentication, use headers:
curl -H "Authorization: Bearer YOUR_ACCESS_TOKEN" -O https://example.com/file.zip
7. Handling Redirects
Some URLs redirect to another location. Use -L
to follow redirects:
curl -L -O https://example.com/download
8. Setting Download Speed Limits
To prevent overloading the network, set a speed limit using --limit-rate
:
curl --limit-rate 500k -O https://example.com/largefile.zip
This limits the speed to 500 KB/s.
9. Verifying File Integrity
After downloading, verify the integrity using a checksum:
sha256sum file.zip
Compare the output with the checksum provided by the file source.
10. Using cURL in Scripts
You can automate downloads with shell scripts. Example:
#!/bin/bashURL="https://example.com/file.zip"OUTPUT="file.zip"curl -o $OUTPUT $URLecho "Download complete: $OUTPUT"
Save this script as download.sh
and run it using:
chmod +x download.sh./download.sh
11. Common Errors and Fixes
Error | Solution |
---|---|
Could not resolve host |
Check your internet connection or correct the URL. |
403 Forbidden |
Ensure you have the right permissions or authentication. |
404 Not Found |
Verify the file URL is correct. |
Connection timed out |
Try increasing timeout: --connect-timeout 60 . |
SSL certificate error |
Use -k to ignore SSL verification (not recommended). |
cURL is a versatile tool for downloading files efficiently. Whether you’re downloading a single file, handling authentication, resuming interrupted downloads, or automating tasks, cURL provides powerful options.
By mastering these techniques, you can enhance productivity, streamline workflows, and manage downloads effortlessly from the command line.