Fundamentals 13 min read

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.

Liangxu Linux
Liangxu Linux
Liangxu Linux
Master cURL: Essential Commands for Downloading, Uploading, and Automation

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.html

Following Redirects

cURL does not follow redirects by default. Use the -L switch to enable this behavior:

$ curl -L www.likegeeks.com

Resuming 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.zip

Specifying 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.com

Authentication with Username and Password

Use the -u switch for basic authentication, e.g., with FTP:

$ curl -u username:password ftp://example.com

Using a Proxy

Specify an HTTP proxy with the -x switch:

$ curl -x 192.168.1.1:8080 http://example.com

Downloading 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 part1

Combine parts with cat:

$ cat ubuntu-part? > ubuntu-18.04.3-desktop-amd64.iso

Client Certificate Authentication

Use --cert to provide a client certificate and password:

$ curl --cert path/to/cert.crt:password ftp://example.com

Silent Mode

Suppress progress and error messages with -s. Combine with -O to save the file silently:

$ curl -s -O http://example.com

Fetching 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.com

Sending Multiple Headers

Pass custom headers using repeated -H options:

$ curl -H 'Connection: keep-alive' -H 'Accept-Charset: utf-8' http://example.com

Uploading Data (POST) and Files

Send form data with -d:

$ curl -d 'name=geek&location=usa' http://example.com

Upload 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.txt

Reading Email (IMAP/POP3)

List mailboxes with IMAP:

$ curl -u username:password imap://mail.example.com

Fetch 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.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

HTTPNetworkingcURLfile transfercommand-lineFTP
Liangxu Linux
Written by

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.)

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.