Mastering File Transfers Using cURL

in #curl12 days ago

Every second, millions of files zip across the internet, quietly landing on your devices. Behind many of these transfers is a simple yet powerful tool—cURL. Whether you’re grabbing a quick file or orchestrating complex API interactions, cURL has got your back.
cURL (Client URL) is your command-line Swiss Army knife. Available on Windows, macOS, and Linux, it lets you fetch files, test web resources, and automate downloads—all without leaving the terminal. You can use it for straightforward downloads or weave it into scripts to handle sophisticated tasks like data manipulation or authentication.
Let’s cut to the chase. This guide dives into exactly how to download files using cURL. You’ll learn to name files and folders precisely, handle redirects like a pro, throttle download speeds, and much more. Plus, we’ll share OS-specific tips so you can get the most out of cURL whether you’re on Windows, Linux, or Mac.

Nail Your File Naming Right from the Start

cURL doesn’t save files automatically—it streams data to your terminal by default. Without telling it where and what to save, your download is lost in the void.

Want to save a file with a custom name? Use -o or --output.

Example:

curl -o report.html https://example.com

The webpage at example.com is saved as report.html in your current directory.

Need to save to a specific folder? Just specify the path:

curl -o ~/Downloads/report.html https://example.com

Heads-up: if the file already exists, cURL overwrites it—no warnings. If you want to keep the original and download with the remote file’s name, switch to uppercase -O:

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

To avoid overwriting, use a quick shell check before downloading:

if [ -f file.txt ]; then
  echo "File already exists! Skipping download."
else
  curl -O https://example.com/file.txt
fi

This way, you keep your data safe.

Control Redirect

Many downloads aren’t straightforward. URLs often redirect you to another location before the actual file appears. By default, cURL ignores these redirects — but you shouldn’t.

Add the -L flag to make cURL follow redirects automatically:

curl -L -o setup.zip https://example.com/download

Why is this vital? Without following redirects, you might end up saving a small HTML page instead of the file you want. It also helps with shortened URLs, HTTPS forwarding, and bypassing intermediary pages.

Want to control how many redirects cURL will follow? Use:

curl -L --max-redirs 5 -o file.zip https://example.com/download

If it hits the limit, cURL stops and throws an error. Smart and safe.

Downloading Multiple Files Simultaneously

Download a batch of files with ease.

If the files are independent URLs:

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

Files numbered sequentially? Brace yourself:

curl -O https://example.com/file{1,2,3}.zip

On Windows with a list in urls.txt:

Get-Content urls.txt | ForEach-Object { curl -O $_ }

Or loop through numbered files:

For ($i=1; $i -le 3; $i++) { curl -O "https://example.com/file$i.zip" }

On Linux/macOS, download from a list:

xargs -n 1 curl -O < urls.txt

Or loop through numbers:

for i in {1..5}; do 
  curl -O https://example.com/file$i.zip
done

Control Your Download Speed with Rate Limiting

Running downloads over limited or shared bandwidth? Control is in your hands.

Use --limit-rate to cap speed. For example:

curl --limit-rate 500k -O https://example.com/largefile.zip

This caps your speed at 500 KB/s.

Why bother? Slower downloads reduce network disruption, help avoid server throttling or bans, and keep your other apps humming smoothly.

Batch downloads with rate limit? Here’s a quick script:

Windows PowerShell:

1..3 | ForEach-Object { curl --limit-rate 500k -O https://example.com/archive$_.zip }

Linux/macOS Bash:

for i in {1..3}; do 
  curl --limit-rate 500k -O https://example.com/archive$i.zip
done

Silent Mode for Automation

If you’re automating, progress bars clutter your logs. Enter silent mode:

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

Want error messages but no progress? Combine flags:

curl -s -S -O https://example.com/file.zip

Managing Authentication

Sometimes, files hide behind login walls. cURL handles them smoothly.

For basic username/password:

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

Passwords with special characters? Use quotes:

curl -u username:"p@ssw0rd#123" -O https://example.com/protected.zip

If you provide only the username, cURL prompts for the password securely.

Access tokens? Add them in headers:

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

Or (less secure) in the URL:

curl -O https://api.example.com/data.json?access_token=your_token_here

Using Proxies with cURL

Proxies can hide your IP or bypass geo-restrictions.

Specify a proxy with -x:

curl -x http://proxy.example.com:8080 -o file.zip https://example.com/file.zip

With authentication:

curl -x http://user:[email protected]:8080 -o file.zip https://example.com/file.zip

For SOCKS proxies:

curl --proxy socks5h://proxy.example.com:1080 -o file.zip https://example.com/file.zip

socks5h ensures DNS queries go through the proxy, offering better anonymity.

Track Your Downloads with Progress Bars and Logs

Want to watch the download without drowning in info?

Use a sleek progress bar:

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

For verbose details (speed, time, headers):

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

After download, print speed:

curl -o file.zip -w "Download speed: %{speed_download} bits per second\n" https://example.com/file.zip

Troubleshooting with Request/Response Info

Diagnose errors fast.

Add -v for detailed HTTP exchanges:

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

You’ll see server IPs, request headers, response codes like 301, 403, 404, and more. This is your first line of defense against mysterious download failures.

Final Thoughts

cURL is a powerhouse. It handles everything from simple downloads to complex authenticated API requests. Its flexibility with redirects, proxies, speed limits, and progress tracking makes it indispensable for developers and sysadmins alike.
Dive in, experiment with these commands, and make cURL work for you. You’ll wonder how you ever downloaded files without it.