cURL or Client URL is a powerful command-line tool designed for transferring data to and from servers using a wide variety of protocols, including HTTP, HTTPS, FTP, SFTP, SCP, and more.
It is widely used by developers, system administrators, and data analysts for tasks such as downloading files, testing APIs, web scraping, and automating data transfers.
In this tutorial, I will walk you through some of the core practical curl download use cases, from basic to advanced. However, before I move on to the use cases, let’s quickly look at the prerequisites.
The Prerequisites
Before you move on to the use cases, ensure you have the following:
- You have curl installed on your system (usually pre-installed on most Linux distros)
To verify curl installation and its version, run the following command:
# curl –version
This command displays the following information:
- curl version number
- Supported protocols (HTTP, HTTPS, FTP, SFTP, etc.)
- SSL/TLS library information
- Compilation features and capabilities
Now that you have verified that cURl is present and available on your system, let’s begin our discussion of the use cases.
Use Case #1: Basic File Download with curl
The most fundamental curl operation is to download a file from a URL. For this, you need to know the current directory.
Run the following pwd command to confirm your current directory:
# pwd
Once you know the current directory, execute the following command to download your file into the current working directory:
# curl -O https://example.com/file.zip
Here, the -O saves the file with the same name as in the URL.
Use Case #2: Save File with a Custom Name
Now, there are instances where you need to save a file under a different name. In such cases, run the following command:
# curl -o custom_name.zip https://example.com/file.zip
Here, the -o saves the file in the name you specify.
Use Case #3: Download to a Specific Directory
If you want to save files in a specific directory, specify the complete path with the curl command.
If the directory doesn’t exist, create it with the following command:
# mkdir -p /path/to/save/
Replace /path/to/save/ with the desired directory path.
Now download it to the directory with the curl command:
# curl -o /path/to/save/file.zip https://example.com/file.zip
Important: Ensure the target directory (here, /path/to/save/) has WRITE permissions.
Use Case #4: Download from Non-Default Ports
Many development and testing servers run on custom ports rather than the standard HTTP (80) or HTTPS (443). In such cases, execute the following command:
# curl http://example.com:8080/file.zip -O
Replace 8080 with your custom port.
Some of the port specification rules you need to consider include:
- Always include the protocol (http:// or https://)
- The port number follows the hostname with a colon
- This command syntax works with any protocol curl supports
Use Case #5: Resume Interrupted Downloads
Large file downloads can be interrupted by network issues, system shutdowns, or connection timeouts. In such cases, curl‘s resume feature saves time and bandwidth:
# curl -C – -O https://example.com/large_file.iso
Here, -C indicates curl to determine where to resume if the download was interrupted automatically.
Use Case #6: Download Multiple Files Sequentially
If you want to download multiple files sequentially with a single command, run the following command:
# curl -O https://example.com/file1.zip -O https://example.com/file2.zip
Files will be downloaded one by one.
Use Case #7: Download Multiple Files in Parallel
If you have sufficient bandwidth, you can download files simultaneously:
# curl -Z -O https://example.com/file1.zip -O https://example.com/file2.zip
Here, -Z enables multiple downloads simultaneously. You can also use –parallel instead of -Z.
Note that the curl version should be 7.66 or higher for this functionality.
Use Case #8: Limit Download Speed (Rate Limiting)
If you want to control bandwidth usage to avoid exhausting the connection, run the following command:
# curl –limit-rate 100K -O https://example.com/large_file.iso
Here, –limit-rate 100K limits the download to 100 KB/s.
Use Case #9: Abort Slow Downloads
If you want to automatically cancel downloads that are too slow, execute the -Y and -y in the curl command:
# curl -Y 100 -y 10 -O https://example.com/slow_file.iso
Here, the download aborts if the speed drops below 100 KB/s for 10 seconds.
Use Case #10: Show or Hide Download Progress
You can control how much information curl displays during downloads.
If you want to display the progress bar, run the following command:
# curl -# -O https://example.com/file.zip
Here, -# displays the progress bar.
Now, if you want to download files silently, execute the following command:
# curl -s -O https://example.com/file.zip
Here, -s suppresses all output except errors.
Use Case #11: Follow HTTP Redirects
At times, URLs automatically redirect, especially with shortened links or CDN-backed URLs. curl doesn’t follow redirects by default. In such cases, execute the following command to follow the HTTP redirect:
# curl -L -O https://example.com/redirected_file.zip
Here, -L redirects the curl. Without -L, curl stops at the redirect and downloads the redirect page (usually HTML) instead of the actual file.
Use Case #12: Basic Authentication for Protected Files
If you need to access a password-protected file using HTTP basic authentication, execute the following command:
# curl -u username:password -O https://example.com/protected/file.zip
Important: We always recommend using HTTPS to avoid credentials being sent in plaintext. Avoid hardcoding credentials; prefer environment variables or prompts for security.
Use Case #13: Ignore SSL Certificate Errors
SSL certificates are necessary to ensure the website is legitimate and the data is not tampered with.
However, at times you need to download from servers with self-signed or expired certificates; in such cases, run the following command:
# curl -k -O https://example.com/insecure_file.zip
Here, -k bypasses all SSL certificate verification. Never use in production scripts or with sensitive data.
Use Case #14: Download via FTP and SFTP
curl supports file transfer protocols beyond HTTP and HTTPS. FTP, one of the oldest protocols, and SFTP, a completely different protocol that provides secure file transfer capabilities, are also supported by the curl command.
FTP Download
To download files via FTP, run the following command:
# curl -u ftp_user:ftp_password -O ftp://example.com/file.zip
SFTP Download
Now, to download files via SFTP, execute the following command:
# curl -u sftp_user -O sftp://example.com/file.zip
SFTP typically requires SSH key authentication, and curl might prompt for a password, or it might need proper SSH keys configured.
Use Case #15: Download Only File Headers (No Body)
If you need file information without downloading the entire file, run the following command:
# curl -I https://example.com/file.zip
It is particularly useful to verify file size, last modified date, or server headers.
Use Case #16: Use a Custom User-Agent
Some servers block requests from curl‘s default user agent for various security and compliance reasons. In such cases, execute the following command:
# curl -A “MyDownloaderBot/1.0” -O https://example.com/file.zip
Use Case #17: Download Files with URL Patterns
URL patterns are a way to write multiple URLs quickly using placeholders and ranges, instead of typing each URL separately.
If you want to download multiple files using URL patterns, run the following command:
# curl “https://example.com/files_[1-5].zip” -O
Conclusion
We discussed how cURL can be used for a variety of download scenarios, ranging from basic file downloads to advanced use cases like resuming interrupted downloads, handling authentication, and downloading files via specific protocols like FTP, SFTP, and HTTP.
Some of the other useful flags include –retry (auto-retries failed downloads), –fail (fails silently on errors), and –compressed (requests compressed content for faster downloads). To understand more curl commands, run man curl or curl –help.
FAQs
How do I download files with curl and ensure they have a specific filename?
You can easily download files with curl and control the output file name using the -o (output) or -O (remote file name) flags. The -o flag allows you to specify a completely new name for the downloaded file. If you want curl to save the file with the same name it has on the remote server, use the uppercase -O flag.
What’s the main difference between curl and wget for downloading files, and when should I use one over the other?
Both curl and wget are powerful command-line tools for downloading files, but they serve slightly different primary purposes. curl is fundamentally a data transfer tool designed to work with a wide range of protocols, making it excellent for interacting with API endpoints, uploading data, and more complex HTTP operations. wget, on the other hand, is primarily designed for non-interactive downloading of files and supports recursive downloads, making it ideal for mirroring websites or retrieving multiple files from a server efficiently.
Can I use curl to download files from an API that requires authentication?
Absolutely! curl is exceptionally well-suited for interacting with API endpoints that require authentication. You can provide a username and password directly in your curl command using the -u flag, followed by username:password.
My curl download seems stuck, or it’s not showing progress. How can I troubleshoot this, and what common issues might prevent a file from downloading correctly?
If your curl download files with curl command appear to be stuck, there are several things to check. First, ensure you have an active internet connection. You can add the -v (verbose) flag to your curl command to see detailed information about the connection, headers, and transfer progress.
Common problems include incorrect URLs, network firewalls blocking the connection, server-side issues, or authentication failures (if the API requires a username and password).
How do I download multiple files or a directory using curl? Is there a built-in recursive option like in wget?
While curl is excellent for downloading individual files, it does not have a built-in recursive option like wget to download entire directories or multiple linked files automatically. If you need to download files with curl in a recursive manner or fetch a list of files, you typically need to combine curl with other command-line tools like xargs or write a simple script.
Latest AMD Server
Streaming Server