Master cURL: Essential Commands for Downloading, Uploading, and Automation
This tutorial provides a comprehensive guide to using the cURL command-line tool on Linux, covering basic file downloads, following redirects, resuming interrupted transfers, setting timeouts, authentication, proxy usage, range requests, silent mode, header manipulation, data uploading, email sending, and a comparison with wget, all illustrated with practical examples and code snippets.
What Is cURL?
cURL is a versatile command-line utility that transfers data to or from a server using over 20 supported protocols, such as HTTP, HTTPS, FTP, and SMTP. It is commonly used in scripts to fetch web pages, download files, or interact with APIs.
Downloading Files
The simplest download uses the default HTTP protocol: $ curl http://www.google.com To save the output to a file, add the --output option:
$ curl www.likegeeks.com --output likegeeks.htmlFollowing Redirects
cURL does not follow redirects by default. Use the -L switch to enable this behavior:
$ curl -L www.likegeeks.comResuming Interrupted Downloads
Interrupt a download with Ctrl+C. To resume, use the -C - option together with --output:
$ curl -C - example.com/some-file.zip --output MyFile.zipSpecifying Timeouts
Set a maximum execution time with -m (in seconds): $ curl -m 60 example.com Set a connection timeout with --connect-timeout:
$ curl --connect-timeout 60 example.comAuthentication with Username and Password
Use the -u switch for basic authentication, e.g., with FTP:
$ curl -u username:password ftp://example.comUsing a Proxy
Specify an HTTP proxy with the -x switch:
$ curl -x 192.168.1.1:8080 http://example.comDownloading in Ranges (Chunked Downloads)
Download specific byte ranges using --range. Example for a 100 MB chunk:
$ curl --range 0-99999999 http://releases.ubuntu.com/18.04/ubuntu-18.04.3-desktop-amd64.iso part1Combine parts with cat:
$ cat ubuntu-part? > ubuntu-18.04.3-desktop-amd64.isoClient Certificate Authentication
Use --cert to provide a client certificate and password:
$ curl --cert path/to/cert.crt:password ftp://example.comSilent Mode
Suppress progress and error messages with -s. Combine with -O to save the file silently:
$ curl -s -O http://example.comFetching Headers (Getting the Title)
Retrieve only the response headers with -I: $ curl -I example.com Combine with -L to follow redirects and show each step’s headers:
$ curl -I -L example.comSending Multiple Headers
Pass custom headers using repeated -H options:
$ curl -H 'Connection: keep-alive' -H 'Accept-Charset: utf-8' http://example.comUploading Data (POST) and Files
Send form data with -d:
$ curl -d 'name=geek&location=usa' http://example.comUpload a file as raw data: $ curl -d @filename http://example.com Upload a file to an FTP server with -T:
$ curl -T myfile.txt ftp://example.com/some/directory/Sending Email via SMTP
Use cURL to send an email through an SMTP server:
$ curl smtp://mail.example.com --mail-from [email protected] --mail-rcpt [email protected] --upload-file email.txtReading Email (IMAP/POP3)
List mailboxes with IMAP:
$ curl -u username:password imap://mail.example.comFetch a specific message using -X:
$ curl -u username:password imap://mail.example.com -X 'UID FETCH 1234'cURL vs. wget
Both tools retrieve data from servers, but wget excels at recursive site mirroring and directory traversal, while cURL offers finer-grained control over individual requests, supports a wider range of protocols, and is better suited for scripting and uploading data.
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.
Liangxu Linux
Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)
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.
