How to Use curl to Download Files (with Advanced Configuration for Speed) in Linux
How to Use curl
to Download Files (with Advanced Configuration for Speed) in Linux
Table of Contents
- Introduction
- Step 1: Basic Usage of
curl
π₯ - Step 2: Resume Interrupted Downloads π
- Step 3: Downloading Multiple Files in Bulk π
- Step 4: Speeding Up Downloads with Parallel Requests π
- Step 5: Set Up a Supercharged Alias (
surl
) βοΈ - Common
curl
Options (Cheat Sheet) π - Conclusion π―
1. Introduction
curl
is a versatile command-line tool for transferring data with URLs. It's not just for downloading files but can be used for various protocols. This tutorial will cover basic and advanced uses of curl
, focusing on optimizing download speed.
2. Step 1: Basic Usage of curl
π₯
To download a file using curl
, use the -O
option to save the file with its original name:
For example:
This command downloads file.zip
to your current directory.
3. Step 2: Resume Interrupted Downloads π
To resume an interrupted download, use the -C -
option:
This will continue downloading the file from where it left off.
4. Step 3: Downloading Multiple Files in Bulk π
You can download multiple files by using a loop with a list of URLs:
- Create a text file (e.g.,
urls.txt
) containing the URLs:
- Use a loop to download each file:
5. Step 4: Speeding Up Downloads with Parallel Requests π
curl
doesnβt natively support parallel downloads, but you can use tools like xargs
to achieve this:
Explanation:
-n 1
: Processes one URL at a time.-P 4
: Runs up to 4 parallel downloads.
6. Step 5: Set Up a Supercharged Alias (surl
) βοΈ
To create an alias called surl
with optimized options for faster and more resilient downloads:
- Open your
.bashrc
or.zshrc
file:
- Add the following alias:
Explanation of the options:
-O
: Save the file with its original name.-C -
: Resume downloads.--retry 5
: Retry up to 5 times on failure.--retry-delay 2
: Wait 2 seconds between retries.--max-time 120
: Set a maximum time of 120 seconds for the entire operation.--fail
: Fail silently on server errors.--silent
: Suppress progress output.-
--show-error
: Show error messages if they occur. -
Save and exit the editor (
Ctrl + O
,Enter
, thenCtrl + X
). -
Apply the changes:
Now you can use surl
for optimized curl
downloads:
7. Common curl
Options (Cheat Sheet) π
Hereβs a table of useful curl
options:
Option | Description |
---|---|
-O |
Save the file with its original name |
-C - |
Resume a partially downloaded file |
-L |
Follow redirects |
-s |
Silent mode (no progress or error messages) |
-S |
Show errors (use with -s ) |
--retry <num> |
Number of times to retry on failure |
--retry-delay <seconds> |
Delay between retries (in seconds) |
--max-time <seconds> |
Maximum time allowed for the operation |
--fail |
Fail silently on server errors (no output) |
-x <proxy> |
Use the specified proxy |
-u <user:password> |
Use the specified user and password for authentication |
--header <header> |
Add a custom header to the request |
8. Conclusion π―
curl
is a powerful tool for downloading files with various options to handle interruptions and optimize performance. With the surl
alias, you can streamline and enhance your downloading process for greater efficiency.