How to Download Files from the Web Using cURL

download-files-using-curl

If you’re looking for a reliable way to download files—whether it’s an image, a PDF, a video, or even a software installer—using the command line, cURL is one of the most powerful tools you can have in your toolbox.

Unlike your browser, which hides much of what happens behind the scenes, cURL gives you full control over the HTTP request. It’s scriptable, flexible, and works across macOS, Linux, and Windows. In this guide, we’ll explore how to use cURL to download files of all types, handle redirects, resume interrupted downloads, and even authenticate when files are protected.

🧰 What is cURL?

cURL (short for Client URL) is a command-line tool that allows you to transfer data to or from a server using a variety of protocols, including HTTP, HTTPS, FTP, and more. It’s pre-installed on most UNIX systems and available on Windows via WSL or Git Bash.

🚀 1. Basic File Download

Let’s start with a simple use case—downloading a file with its original filename.

curl -O https://www.labnol.org/files/sample.jpg

Here’s what’s happening:

  • -O: This tells cURL to use the same filename as the one in the URL path (sample.jpg).

This is perfect for one-off downloads, but it becomes even more useful when you’re scripting.

📝 2. Rename the File While Downloading

To download a file but save it under a custom name, use the lowercase -o:

curl -o myphoto.jpg https://www.labnol.org/files/sample.jpg

This is useful when you’re naming files dynamically in scripts or organizing them by context.

📄 3. Download PDFs, Docs, or Ebooks

You can use the same syntax to download PDF files or any document:

curl -O https://www.example.com/files/brochure.pdf

Or rename it while downloading:

curl -o summer-catalogue.pdf https://www.example.com/files/brochure.pdf

✅ Pro tip: Always wrap URLs in quotes (") if they contain special characters or ampersands (&).

🎞️ 4. Download Large Video Files

For video or audio files that are hosted directly (not embedded or streamed):

curl -L -O https://cdn.example.com/videos/tutorial.mp4
  • -L: Follows redirects automatically. This is often necessary for CDN-hosted files.

You can also throttle your download to avoid network congestion:

curl --limit-rate 1M -O https://cdn.example.com/videos/tutorial.mp4

🔁 5. Resume an Interrupted Download

If your download gets interrupted, don’t start over. Just resume it:

curl -C - -O https://cdn.example.com/videos/tutorial.mp4
  • -C -: Resume download from the last byte.

This is a lifesaver when dealing with unstable networks or large files.

🔐 6. Download from a Password-Protected URL (HTTP Basic Auth)

If a website requires login credentials, cURL can pass those securely:

curl -u yourusername:yourpassword -O https://www.example.com/secure/file.pdf

This uses HTTP Basic Authentication. If you’re unsure how this works, I’ve written a separate guide:

👉 How to send Basic Auth credentials using cURL

⚠️ Note: Always use HTTPS for password-protected requests to prevent leaking credentials in plaintext.

📦 7. Download Multiple Files from a List

You can save multiple URLs in a text file (files.txt) and download them all in one go:

files.txt

https://example.com/file1.jpg
https://example.com/file2.pdf
https://example.com/file3.mp4

Now run:

xargs -n 1 curl -O < files.txt

Each line will be processed as an individual download.

🌍 8. Download Files Behind Redirects (e.g., Google Drive, Dropbox)

Some links perform HTTP redirects or use shortened URLs. Add the -L flag:

curl -L -O https://bit.ly/yourfile

Without -L, cURL will stop at the redirection and not fetch the actual file.

🛠 9. Useful cURL Flags for Downloading

OptionDescription
-OSave file using the name in URL
-o filenameSave file with custom name
-LFollow redirects
-C -Resume download
--limit-rateThrottle speed (e.g. --limit-rate 500k)
-u user:passBasic authentication

💡 Bonus Use Case: Download a File and Pipe It Somewhere

Want to download a CSV file and preview the first few lines?

curl -s https://example.com/data.csv | head -n 5

Or unzip and extract a downloaded archive on the fly:

curl -L https://example.com/data.zip -o data.zip && unzip data.zip

📌 Final Thoughts

Using cURL to download files offers automation, speed, and control. Whether you’re scripting backups, fetching media assets, or testing file delivery from servers, it beats opening a browser and clicking around.

To go further, consider integrating cURL with cron, bash scripts, or serverless functions to build fully automated download workflows.

Posted by Devender Gupta