Master cURL on Linux: Essential Commands for Downloading, Uploading, and More
This guide walks you through practical cURL commands on Linux, covering file downloads, redirects, resume support, timeout settings, form submissions, email handling, and key differences from wget, all illustrated with clear examples and screenshots.
Introduction
cURL (client URL) is a versatile transfer tool on Linux that can upload and download files, often used to verify URL accessibility. Compared with wget, cURL offers more customizable request parameters, making it superior for simulating web requests, while wget excels at FTP and recursive downloads.
What is cURL
cURL interacts with websites or APIs, sending requests and receiving responses or saving data to files. It can be part of scripts to fetch data for further processing. Although cURL is smooth for downloading, tools like wget may be preferable for certain tasks.
Downloading Files
The most basic cURL command downloads a web page or file using HTTP by default. curl https://www.21cto.com Adding the --output option saves the content to a file: curl www.21cto.com --output 21cto.html For binary files such as Word documents, cURL streams the data in binary form and saves it to the current working directory.
Redirects
When a URL redirects, cURL does not follow by default. Use the -L option to follow redirects:
curl -L www.21cto.comResuming Downloads
If a download is interrupted, cURL can resume with the -C - option:
curl -C - 21cto.com/some-file.zip --output myfile.zipSetting Timeouts
Use -m to set a maximum execution time (in seconds): curl -m 60 21cto.com Or use --connect-timeout to limit connection establishment time:
curl --connect-timeout 60 21cto.comForm Submission
POST data with -d:
curl -d 'name=user&gender=male' https://sso.21cto.comUpload a file as POST data: curl -d @file-name https://sso.21cto.com Upload a file to an FTP server:
curl -T myfile.txt ftp://ftp.21cto.com/uploads/misc/Sending Emails
cURL can send emails via SMTP:
curl smtp://mail.example.com --mail-from [email protected] --mail-rcpt [email protected]The email body must be provided in a file (e.g., email.txt) with standard headers and content.
Reading Emails
cURL supports IMAP and POP3 to read mail. Example using IMAP:
curl -u username:password imap://mail.imapmail.comTo fetch a specific message, use the -X option with UID:
curl -u username:password imap://mail.imapmail.com -X 'UID FETCH 1234'Differences Between cURL and wget
Both can download content, but wget excels at recursive site downloads and FTP, while cURL is better for uploading files and fine‑grained request customization.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
21CTO
21CTO (21CTO.com) offers developers community, training, and services, making it your go‑to learning and service platform.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
