×

Search anything:

curl command in Linux with Examples

Binary Tree book by OpenGenus

Open-Source Internship opportunity by OpenGenus for programmers. Apply now.

In this article, we will Explore about Curl Command and How to use it in Linux

Table of Content

1.Introduction to "Curl Command"
2.Installation of the curl Command
3.curl Syntax and its use
4.Curl Commands and Options
5.curl Protocols

Introduction to Curl Command?

2b10-article-210721-curl-body-text_copy-1

curl is a command line tool that enables data transfer over various network protocols for example using any of the supported protocols (HTTP, FTP, IMAP, POP3, SCP, SFTP, SMTP, TFTP, TELNET, LDAP or FILE). It communicates with a web or application server by specifying a relevant URL and the data that need to be sent or received.

curl is powered by libcurl.

The most common use cases for curl are:

  • Downloading files from the internet
  • Endpoint testing
  • Debugging
  • Error logging

Installation of the curl Command

The curl command comes with most of the Linux distributions. But, if the system does not carry the curl by default. You need to install it manually. To install the curl, execute the following commands:

Update the system by executing the following commands:

sudo apt update
sudo apt upgrade

Now, install the curl utility by executing the below command:

sudo apt install curl

Verify the installation by executing the below command:

curl -version

The above command will display the installed version of the curl command.

curl Syntax

syntex:

curl [options] [URL...]

for Example
curl https://iq.opengenus.org

This should display the content of the URL on the terminal. The URL syntax is protocol dependent and multiple URLs can be written as sets like

curl http://site.{one, two, three}.com

Some Useful Curl Commands and Options

  1. View curl Version

The -V or --version options will not only return the version, but also the supported protocols and features in your current version.

** Output**

ankit@ankit-HP-Notebook:~$ curl -V https://iq.opengenus.org
curl 7.68.0 (x86_64-pc-linux-gnu) libcurl/7.68.0 OpenSSL/1.1.1f zlib/1.2.11 brotli/1.0.7 libidn2/2.2.0 libpsl/0.21.0 (+libidn2/2.2.0) libssh/0.9.3/openssl/zlib nghttp2/1.40.0 librtmp/2.3
Release-Date: 2020-01-08
Protocols: dict file ftp ftps gopher http https imap imaps ldap ldaps pop3 pop3s rtmp rtsp scp sftp smb smbs smtp smtps telnet tftp 
Features: AsynchDNS brotli GSS-API HTTP2 HTTPS-proxy IDN IPv6 Kerberos Largefile libz NTLM NTLM_WB PSL SPNEGO SSL TLS-SRP UnixSockets

2. Download a File
One of the interesting and fascinating uses of curl is that we can download a file from the web. To download a file from the web, copy the download link and paste it with the curl command. Supported protocols

Syntax:

curl -o [file_name] [URL...]

-o : saves the downloaded file on the local machine with the name provided in the parameters.

Progress Meter: curl displays a progress meter during use to indicate the transfer rate, amount of data transferred, time left etc.

curl -# -o https://iq.opengenus.org/file.zip

If you like a progress bar instead of meter, you can use the -# option as in the example above, or –silent if you want to disable it completely.

output

ankit@ankit-HP-Notebook:~$ curl -# -o file.zip  https://iq.opengenus.org
####################################################################################################################################### 100.0%

-O : This option downloads the file and saves it with the same name as in the URL.
Syntax:

curl -O [URL...]

Example:

curl -O https://iq.opengenus.org/1MB.zip

output

ankit@ankit-HP-Notebook:~$ curl -O https://iq.opengenus.org/1MB.zip
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0       0      0 --:--:--  0:00:01 --:--:--     0

-C – : This option resumes download which has been stopped due to some reason. This is useful when downloading large files and was interrupted.
Syntax:

curl -C - [URL...]

Example:

curl -C - -O https://iq.opengenus.org//1MB.zip

ankit@ankit-HP-Notebook:~$ curl -O https://iq.opengenus.org/1MB.zip
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:--  0:00:01 --:--:--     0

**–limit-rate : **This option limits the upper bound of the rate of data transfer and keeps it around the given value in bytes.
Syntax:

curl --limit-rate [value] [URL]
Example:

curl --limit-rate 1000K -O https://iq.opengenus.org//1MB.zip

output

ankit@ankit-HP-Notebook:~$ curl --limit-rate 1000K -O https://iq.opengenus.org//1MB.zip
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:--  0:00:01 --:--:--     0

Sending mail : As curl can transfer data over different protocols, including SMTP, we can use curl to send mails.

Syntax:

curl –url [SMTP URL] –mail-from [sender_mail] –mail-rcpt [receiver_mail] -n –ssl-reqd -u {email}:{password} -T [Mail text file]

-T : This option helps to upload a file to the FTP server.
Syntax:

curl -u {username}:{password} -T {filename} {FTP_Location}

If you want to append a already existing FTP file you can use the -a or –append option.
output

ankit@ankit-HP-Notebook:~$ curl -u demo:password -O https://opengenus.org/readme.txt
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:--  0:01:21 --:--:--     0

Query HTTP Headers

The HTTP headers contain additional information; it allows the webserver to download this information. To query the HTTP headers from a website, execute the command with '-I' option as follows:

curl -I https://iq.opengenus.org/

output

ankit@ankit-HP-Notebook:~$ curl -I https://iq.opengenus.org/
HTTP/2 200 
server: nginx/1.14.0 (Ubuntu)
date: Thu, 20 Jan 2022 10:45:00 GMT
content-type: text/html; charset=utf-8
content-length: 38927
x-powered-by: Express
cache-control: public, max-age=0
etag: W/"980f-LiIgp8W3wKx3rlcNhUKPVsFcGm0"
vary: Accept-Encoding
strict-transport-security: max-age=63072000; includeSubDomains; preload
x-frame-options: SAMEORIGIN
x-content-type-options: nosniff

proxy

-x, –proxy: curl also lets us use a proxy to access the URL.

Syntax:

curl -x [proxy_name]:[port] [URL...]

If the proxy requires authentication, it can be used with the command:

curl -u [user]:[password] -x [proxy_name]:[port] [URL...]

Redirect

to follow redirect with Curl, use the -L or --location command-line option.

Syntax
The general form of the Curl Follow Redirect command is as follows:

curl -L [URL]

Example

curl -L --max-redirs 5 https://iq.opengenus.org/

DELETE request using Curl
To make a DELETE request using Curl, you need to use the -X DELETE command-line option followed by the target URL

Curl DELETE Request Syntax

curl -X DELETE [URL] [options]

Curl DELETE Example

curl -X DELETE https://iq.opengenus.org/
-H "Accept: application/json"

**Cookies with curl **
curl has a full cookie "engine" built in. If you just activate it, you can have curl receive and send cookies exactly as mandated in the specs.

These curl recipes show you how to add cookies to curl requests. By default, curl doesn't send any cookies but you can add your own cookies via the -b 'name=value' command line argument. To save cookies from the response to a file, use the -c file option. To load cookies from a file, use the -b file option.

Command line options:

-b, --cookie

Load Cookies from a File

curl -b cookies.txt https://www.google.com

Add a Cookie

curl -b 'session=abcdef' https://google.com

Add Two Cookies

curl -b 'session=abcdef' -b 'loggedin=true' https://google.com

curl has more than two hundred command-line options and the number of options keep increasing over time. Chances are the number of options will reach 250 within a few years.

To find out which options you need to perform as certain action, you can get curl to list them. First, curl --help or simply curl -h will get you a list of the most important and frequently used options.

Supported protocols

curl supports or can be made to support (if built so) the following transfer protocols.
DICT, FILE, FTP, FTPS, GOPHER, GOPHERS, HTTP, HTTPS, IMAP, IMAPS, LDAP, LDAPS, MQTT, POP3, POP3S, RTMP, RTMPS, RTSP, SCP, SFTP, SMB, SBMS, SMTP, SMTPS, TELNET and TFTP

curl command in Linux with Examples
Share this