Mastering cURL: Essential Commands for Downloading, Uploading, and Automation
This comprehensive guide walks you through the core features of the cURL command-line tool, covering file downloads, redirects, resumable transfers, timeouts, authentication, proxy usage, chunked downloads, silent mode, header manipulation, and both uploading files and sending or reading emails.
cURL is a versatile command-line utility for transferring data using over 20 supported protocols, making it ideal for interacting with websites, APIs, and servers. It can download or upload files, emails, or web pages, and is often used within larger scripts for further processing.
Basic File Download
To retrieve a web page or file, use 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 transfer with Ctrl+C and resume it using the -C - option:
$ curl -C - example.com/some-file.zip --output MyFile.zipSpecifying Timeouts
Set a maximum execution time with -m (seconds): $ curl -m 60 example.com Set a connection timeout with --connect-timeout:
$ curl --connect-timeout 60 example.comAuthentication with Username and Password
Use -u for basic authentication, e.g., FTP:
$ curl -u username:password ftp://example.comUsing a Proxy
Define an HTTP proxy with -x:
$ curl -x 192.168.1.1:8080 http://example.comChunked Download of Large Files
Download specific byte ranges with --range and later combine parts using cat:
$ curl --range 0-99999999 http://releases.ubuntu.com/18.04/ubuntu-18.04.3-desktop-amd64.iso ubuntu-part1
$ curl --range 100000000-199999999 http://releases.ubuntu.com/18.04/ubuntu-18.04.3-desktop-amd64.iso ubuntu-part2
$ cat ubuntu-part? > ubuntu-18.04.3-desktop-amd64.isoClient Certificate Authentication
Specify a certificate file with --cert:
$ curl --cert path/to/cert.crt:password ftp://example.comSilent Mode
Suppress progress and error messages with -s. Combine with -O or --output to save files silently:
$ curl -s -O http://example.com
$ curl -s http://example.com --output index.htmlFetching Headers and Titles
Retrieve only the HTTP headers using -I: $ curl -I example.com Follow redirects while fetching headers with -I -L: $ curl -I -L example.com Send custom headers with multiple -H options:
$ curl -H 'Connection: keep-alive' -H 'Accept-Charset: utf-8' http://example.comUploading Data and Files
POST form data using -d:
$ curl -d 'name=geek&location=usa' http://example.comUpload a file with -d @filename or use -T for FTP uploads:
$ curl -d @filename http://example.com
$ curl -T myfile.txt ftp://example.com/some/directory/Sending Emails via SMTP
Send an email by specifying the SMTP server, sender, recipient, and email file:
$ curl smtp://mail.example.com --mail-from [email protected] --mail-rcpt [email protected] --upload-file email.txtReading Emails via IMAP/POP3
List mailboxes with IMAP authentication:
$ curl -u username:password imap://mail.example.comFetch a specific message using -X 'UID FETCH 1234':
$ curl -u username:password imap://mail.example.com -X 'UID FETCH 1234'cURL vs. wget
While both tools retrieve data, wget excels at recursive site downloads and directory traversal, whereas cURL offers broader protocol support and fine-grained control for uploads and custom requests.
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.
ITPUB
Official ITPUB account sharing technical insights, community news, and exciting events.
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.
