
Workflow: Downloading Files with curl – Fast DevOps Task
Downloading files from the command line is a fundamental DevOps skill that every developer and system administrator needs to master. The curl
utility stands out as the most versatile and powerful tool for handling HTTP transfers, API interactions, and file downloads in automation scripts, CI/CD pipelines, and server management tasks. This comprehensive guide will walk you through everything from basic file downloads to advanced use cases, common troubleshooting scenarios, and performance optimization techniques that will make your workflow more efficient and reliable.
How curl Works for File Downloads
At its core, curl is a command-line tool that uses various protocols (HTTP, HTTPS, FTP, SFTP, and more) to transfer data. When downloading files, curl establishes a connection to the target server, sends appropriate headers, handles redirects, and streams the response data to your local filesystem.
The basic syntax for downloading files with curl follows this pattern:
curl [options] [URL] -o [output_file]
Under the hood, curl handles TCP connections, SSL/TLS handshakes, HTTP request formatting, and response parsing automatically. It supports HTTP/1.1, HTTP/2, and even HTTP/3 protocols, making it future-proof for modern web infrastructure.
Essential curl Download Commands
Here are the most commonly used curl commands for file downloads, progressing from basic to advanced scenarios:
Basic File Download
# Download and save with original filename
curl -O https://example.com/software.tar.gz
# Download and specify custom filename
curl -o myfile.zip https://example.com/download.zip
# Download multiple files
curl -O https://example.com/file1.txt -O https://example.com/file2.txt
Downloads with Progress and Resume
# Show progress bar instead of statistics
curl -# -O https://example.com/largefile.iso
# Resume interrupted download
curl -C - -O https://example.com/largefile.iso
# Download with retry on failure
curl --retry 3 --retry-delay 2 -O https://example.com/file.zip
Authentication and Headers
# Basic authentication
curl -u username:password -O https://secure.example.com/file.zip
# Bearer token authentication
curl -H "Authorization: Bearer YOUR_TOKEN" -O https://api.example.com/download
# Custom user agent
curl -A "MyApp/1.0" -O https://example.com/file.tar.gz
Real-World Use Cases and Examples
CI/CD Pipeline Downloads
In continuous integration environments, curl frequently downloads dependencies, artifacts, and deployment packages:
#!/bin/bash
# Download and verify deployment artifact
ARTIFACT_URL="https://releases.example.com/app-v1.2.3.tar.gz"
CHECKSUM_URL="https://releases.example.com/app-v1.2.3.tar.gz.sha256"
curl -fsSL "$ARTIFACT_URL" -o app.tar.gz
curl -fsSL "$CHECKSUM_URL" -o app.tar.gz.sha256
# Verify integrity
sha256sum -c app.tar.gz.sha256 || exit 1
API Data Export
# Download data from REST API with pagination
for page in {1..10}; do
curl -H "Accept: application/json" \
-H "Authorization: Bearer $API_TOKEN" \
"https://api.example.com/data?page=$page&limit=100" \
-o "data_page_$page.json"
sleep 0.5 # Rate limiting
done
Server Provisioning Scripts
# Download and install software packages
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
curl -fsSL https://get.docker.com -o get-docker.sh
sh get-docker.sh
Performance Optimization and Advanced Options
For high-performance downloads and production environments, these optimization techniques can significantly improve speed and reliability:
Parallel Downloads
# Download multiple files in parallel using xargs
echo -e "https://example.com/file1.zip\nhttps://example.com/file2.zip" | \
xargs -n 1 -P 4 curl -O
# Parallel download with GNU parallel
parallel -j 8 curl -O {} ::: \
https://example.com/file1.zip \
https://example.com/file2.zip \
https://example.com/file3.zip
Connection Optimization
# Use HTTP/2 and connection reuse
curl --http2 --keepalive-time 60 -O https://example.com/file.zip
# Limit bandwidth usage
curl --limit-rate 200K -O https://example.com/largefile.iso
# Set connection timeout
curl --connect-timeout 30 --max-time 300 -O https://example.com/file.zip
Comparison with Alternative Tools
Tool | Protocol Support | Resume Support | Parallel Downloads | Authentication | Best Use Case |
---|---|---|---|---|---|
curl | HTTP/HTTPS/FTP/SFTP/SCP | Yes (-C -) | Manual scripting | Multiple methods | Automation, APIs, versatility |
wget | HTTP/HTTPS/FTP | Yes (-c) | No | Basic/Digest | Recursive downloads, mirrors |
aria2 | HTTP/HTTPS/FTP/BitTorrent | Yes | Yes (built-in) | Multiple methods | Large files, torrents |
axel | HTTP/HTTPS/FTP | Yes | Yes (segmented) | Limited | Accelerated downloads |
Common Issues and Troubleshooting
SSL Certificate Problems
# Skip SSL verification (use cautiously)
curl -k -O https://selfsigned.example.com/file.zip
# Specify custom CA bundle
curl --cacert /path/to/ca-bundle.crt -O https://example.com/file.zip
# Use system CA store
curl --capath /etc/ssl/certs -O https://example.com/file.zip
Handling Redirects and Response Codes
# Follow redirects automatically
curl -L -O https://example.com/redirect-to-file.zip
# Fail silently on HTTP errors
curl -fsSL -O https://example.com/might-not-exist.zip
# Show only HTTP status code
curl -s -o /dev/null -w "%{http_code}" https://example.com/file.zip
Debugging Network Issues
# Verbose output for debugging
curl -v -O https://example.com/file.zip
# Show timing information
curl -w "@curl-timing.txt" -O https://example.com/file.zip
# Contents of curl-timing.txt:
# time_namelookup: %{time_namelookup}\n
# time_connect: %{time_connect}\n
# time_appconnect: %{time_appconnect}\n
# time_pretransfer: %{time_pretransfer}\n
# time_redirect: %{time_redirect}\n
# time_starttransfer: %{time_starttransfer}\n
# time_total: %{time_total}\n
Best Practices and Security Considerations
Security Guidelines
- Always verify checksums or signatures for downloaded files, especially in production environments
- Use HTTPS URLs whenever possible to prevent man-in-the-middle attacks
- Avoid using
-k
(insecure) flag in production scripts - Store authentication credentials in environment variables or secure vaults, not in scripts
- Implement proper error handling and logging for audit trails
Performance Best Practices
- Use connection reuse for multiple downloads from the same host
- Implement exponential backoff for retry logic
- Consider bandwidth limitations in shared environments
- Use appropriate timeout values based on file sizes and network conditions
- Monitor download progress for large files to detect stalled transfers
Production-Ready Download Script
#!/bin/bash
set -euo pipefail
download_file() {
local url="$1"
local output="$2"
local max_retries=3
local retry_delay=5
for attempt in $(seq 1 $max_retries); do
echo "Attempt $attempt: Downloading $url"
if curl -fsSL \
--connect-timeout 30 \
--max-time 300 \
--retry 2 \
--retry-delay 2 \
-o "$output" \
"$url"; then
echo "Download successful: $output"
return 0
else
echo "Download failed (attempt $attempt)"
if [ $attempt -lt $max_retries ]; then
sleep $retry_delay
retry_delay=$((retry_delay * 2))
fi
fi
done
echo "Download failed after $max_retries attempts"
return 1
}
# Usage
download_file "https://example.com/file.zip" "./downloads/file.zip"
Integration with DevOps Tools
Docker and Containerization
# Dockerfile example
FROM alpine:latest
RUN apk add --no-cache curl
RUN curl -fsSL https://releases.hashicorp.com/terraform/1.0.0/terraform_1.0.0_linux_amd64.zip \
-o terraform.zip && \
unzip terraform.zip && \
mv terraform /usr/local/bin/
Kubernetes Init Containers
apiVersion: v1
kind: Pod
spec:
initContainers:
- name: download-config
image: curlimages/curl:latest
command: ['sh', '-c']
args:
- |
curl -fsSL -H "Authorization: Bearer $CONFIG_TOKEN" \
https://config.example.com/app-config.yaml \
-o /shared/config.yaml
volumeMounts:
- name: config-volume
mountPath: /shared
For comprehensive curl documentation and advanced features, refer to the official curl documentation. The curl project also maintains an excellent free online book that covers everything from basic usage to advanced networking concepts.
Understanding curl’s file download capabilities transforms routine DevOps tasks from time-consuming manual processes into efficient, automated workflows. Whether you’re managing server deployments, building CI/CD pipelines, or handling API integrations, mastering these curl techniques will significantly improve your productivity and system reliability.

This article incorporates information and material from various online sources. We acknowledge and appreciate the work of all original authors, publishers, and websites. While every effort has been made to appropriately credit the source material, any unintentional oversight or omission does not constitute a copyright infringement. All trademarks, logos, and images mentioned are the property of their respective owners. If you believe that any content used in this article infringes upon your copyright, please contact us immediately for review and prompt action.
This article is intended for informational and educational purposes only and does not infringe on the rights of the copyright owners. If any copyrighted material has been used without proper credit or in violation of copyright laws, it is unintentional and we will rectify it promptly upon notification. Please note that the republishing, redistribution, or reproduction of part or all of the contents in any form is prohibited without express written permission from the author and website owner. For permissions or further inquiries, please contact us.