Curl Command in Linux – Usage + Examples

Last updated: March 15, 2022

curl is a command-line tool that is used for data transfer to and from a remote host. This could be from an HTTP server or FTP server without user interaction or intervention. Curl supports various network protocols such as HTTP, HTTPS, SCP, FTP, and STFP. The name CURL stands for "client URL".

The curl command is a versatile command-line tool that provides multiple options. It provides user authentication when downloading from an FTP server, proxy support, it can download multiple files in any file format from multiple URLs, initiate file transfer resumption in case of an interruption, and many more.

In this tutorial, we will learn how to use the Linux curl command and its most commonly used options.

How to install Curl

By default, the curl command-line tool comes preinstalled in a majority of modern Linux distributions. However, if for whatever reason curl is missing from your Linux flavor, here is how you can install it on major Linux distributions.

Install curl on Debian / Ubuntu

On Debian / Ubuntu distributions such as Mint, MX Linux, Zorin, and Elementary OS, run the command.

sudo apt install curl

Install curl on  Fedora / RHEL / CentOS / Rocky Linux

For Fedora and Red Hat distributions such as CentOS, Rocky Linux, and AlmaLinux, run the command:

sudo dnf  install curl

Install curl on  ArchLinux

For Arch Linux and Arch derivates such as Manjaro and Endeavour OS, run the command:

sudo pacman -Sy curl

Install curl on  OpenSUSE

On SUSE Linux including Leap and TumbleWeed, execute the following command:

sudo zypper install -Sy curl

With the installation of curl already out of the way, let us now shift gears and learn how to use the curl command.

How to use Curl Command

The curl command takes the following basic syntax.

curl [options] [URL...]

The URL can either be a fully qualified domain name or an IP address.

Without command line arguments, the curl command-line tool displays the source code of the resource. In the command below, Curl prints out the HTML code of the entire site on the terminal.

curl http://example.com
curl command a website

Where no protocol is specified, as shown above, the curl command defaults to the basic HTTP protocol.

Curl Command Options

Here are some of the commonly used curl command options.

OptionsDescription
-O Downloads and saves a file using its own file name.
-o Allows a user to download and save a file using a different file name.
-CAllows the resumption of a file download that was previously interrupted.
-IRetrieves HTTP headers only from a URL
--limit-rateSets the bandwidth limit during file download
-ASets the User-Agent when accessing the file
-bAllows you to send cookies.
--cert <cert>Specifies client SSL certificate file
-k, --insecureBypasses certificate checking. Mostly used with self-signed SSL certificates
-uUsed where a username and password  authentication is required by an FTP server
-TUploads a file to an FTP server
-X POSTPerforms a POST request on an HTTP server
-x, --proxyAllows connection to a server via a proxy server

Curl Command Examples

Let us now explore some of the most widely used curl commands. This includes curl options and will explain with its examples.

Check the version of Curl

First off, to verify the version of the curl utility installed, invoke the command:

curl --version
check curl version

From the output, you can see that we are using curl version 7.68.0

Download a single file

To download a file from a remote host, you can use either the -o or -O options. The uppercase -O option downloads the remote file using its own name.  In the example below, we are downloading the WordPress zip file and saving it locally without altering its name.

curl -O  https://wordpress.org/latest.zip

You can confirm the local file name using the ls command as follows:

ls -l | grep *.zip
download a single file

To download and save the file using a different filename, use the lowercase -O option. In the example shown, we are downloading and saving the latest.zip file as wordpress.zip.

curl -o wordpress.zip  https://wordpress.org/latest.zip

Download multiple files

To download multiple files in a single command, provide multiple URLs each preceded by the -O option on the command line as follows. The -O option is used several times to go along with the specified URLs.

curl -O url_1  -O url_2 ...

This way, you can download multiple binary files. In the example below, We are downloading the WordPress zip file and the latest Linux kernel tarball file.

curl -O https://wordpress.org/latest.zip -O https://cdn.kernel.org/pub/linux/kernel/v5.x/linux-5.16.tar.xz

When you download multiple files on the command line, the files will download one after the other in a queue.

Resume file download

Suppose you are downloading a file from a remote host and the data transfer is interrupted by loss of internet connectivity or server errors. Instead of starting the download all over again, you can resume the download using the -C option. You only have to provide the target file again.

In this example, we have simulated an interruption of the download of the Alpine ISO image by pressing Ctrl + C on the terminal.

To resume the previous file transfer or data transfer, we executed the following command.

curl -C  - -O https://dl-cdn.alpinelinux.org/alpine/v3.15/releases/x86_64/alpine-standard-3.15.0-x86_64.iso

As we have seen, the -C option resumes a previous file transfer at a specific offset in the event of interruptions or in case an http server fails. The given offset denotes the exact number of bytes that will be skipped, counted from the beginning of the source file before it is transferred to the destination. When used with uploads, the ftp server command SIZE will be ignored by curl.

Retrieve HTTP headers

HTTP headers are colon-separated, case-sensitive,  key-value pairs that are typically displayed in request and response messages of HTTP message headers. They provide a wide array of information such as encoding, content type, and user agent.

To get the HTTP headers only, use the -I flag followed by the URL of the resource. In the example below, we are fetching the HTTP headers for the URL https://www.centos.org:

curl -I https://www.centos.org
curl retrieve HTTP headers

Check if a website supports HTTP/2

HTTP/2 protocol was introduced as a major upgrade or revision to the HTTP protocol which is the defacto protocol for the world wide web ( www ). It provides a wide array of benefits such as header compression, reduced latency through response and request multiplexing and so much more.

Most notable and respectable organizations have their webservers running HTTP/2. To verify if a site supports HTTP/2 protocol, use the -I option along with the --http2 flag as follows.

curl -I --http2 -s https://rockylinux.org | grep HTTP

The -s option suppresses the progress meter and instructs curl to run in silent mode. For a site that supports HTTP/2 protocol, you should get the output:

HTTP/2 200
curl check HTTP/2 support

Set maximum data transfer rate

The curl command typically consumes all the available bandwidth. If this is not what you want and you would prefer, instead, to put a cap on the data transfer rate, use the --limit-rate option. The option puts a limit on the data transfer rate of the specified file. The limit can be expressed in bytes, kilobytes with the suffix k, Megabytes with the suffix m and Gigabytes with g.

In the example below, we have limited the transfer rate of the TeamViewer binary file to 512 Kilobytes.

curl --limit-rate 512k -O https://download.teamviewer.com/download/linux/teamviewer_amd64.deb

Change the user agent

 Every time to launch your browser and connect to a site, the user agent ( UA ) which is included in the HTTP header relays to the website details about your browser, its version, and OS among many other details. Different websites respond differently to specific user agents and sometimes, you might find yourself having to change the user agent.

To change the user agent using curl to a different browser, use the -A option in the command line syntax shown.

curl -A "user-agent-name" [URL]

In this example, we are telling curl to use or emulating Firefox 96.0 from the Ubuntu Linux machine.

curl -A "Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:96.0) Gecko/20100101 Firefox/96.0"  https://example.com/

Transfer files over FTP

 FTP, short for File Transfer Protocol, is a protocol that allows users to transfer data to and from an FTP server. The curl command Linux is widely used for file upload and downloads on both secured and non-secured FTP servers.

To connect to a secured FTP server using the username and password authentication method use the -u option in the command line syntax shown.

curl -u FTP_USERNAME:FTP_PASSWORD ftp://ftp.example.com/

Upon logging in to the FTP server, curl displays all the files and directories in the remote host's directory. You can then proceed to download a file:

curl -u FTP_USERNAME:FTP_PASSWORD ftp://ftp.example.com/samplefile.tar.gz

In addition, you can upload a local file to the FTP server using the -T option:

curl -T localfile.tar.gz -u FTP_USERNAME:FTP_PASSWORD ftp://ftp.example.com/

To create a new directory in the FTP server, use the --ftp-create-dirs option. The option allows you to automatically create remote directories on the remote host during a file upload if it does not exist.

curl -T local_file.txt ftp://example.com/my_new_folder/ --ftp-create-dirs -u username:password

Send a POST request to a server

The curl command-line tool can also be used to make a post request to an HTTP server in such a way that it emulates a user filling an HTML form and clicking the 'Submit' button. 

The basic syntax for a POST request:

curl -X POST [options] [URL]

The -x option is used to specify the HTTP request method used to communicate with the remote server.  When sending a POST request. data is usually sent to the HTML form. The -F option is used followed shortly by the field=value pair.

In this example, we have demonstrated how you can send a POST request to a basic HTML form with username and  email fields

curl -X POST -F 'name=linuxsysops' -F '[email protected]' https://example.com/contact.php

In addition, you can make a POST request using the -d option.  When this happens, curl sends data using the application/x-www-form-urlencoded Content-Type. Unlike the -F option, the -d option is used several times, and one will be used for each specified field.

curl -X POST -d 'name=linuxsysops' -d '[email protected]' https://example.com/contact.php

HTTP basic authentication with curl

Curl also provides http basic authentication  using the -U option using the syntax shown

curl -u "login:password" [URL]

For security reasons, the basic authentication method should only be used over HTTPS or SSL connections. This is because the username and login credentials can easily be intercepted and decrypted by hackers.

Curl with SSL connections

SSL certificates provide high-level security to protect websites by ensuring that information exchanged between users' web browsers and the webserver is secure. However, certificate files can come in the way of you connecting to the remote several. To make curl ignore or bypass SSL validation, use the specified  -k or --insecure option. This option tells curl to initiate insecure SSL connections or file transfers and overlook security warnings for the invalid SSL certificate file.

curl --insecure [options] [URL]

Using Proxies

The curl command supports various types of proxies including SOCKS, HTTP, HTTPS, and many more. To successfully transfer data through a proxy server, use the -x ( --proxy ) option followed thereafter by the URL of the proxy server.

For example, in the following command, we are downloading the resource using the proxy ip address 192.168.20.100 on port 8888:

curl -x 192.168.20.100:8888 http://samplesite.com

Sending Cookies

At times, you may be required to send an HTTP request with specific cookies to debug or access a remote server. By default, no cookies are sent when requesting a remote file or resource. However, should you decide to send cookies, use the -b switch followed by a filename containing the cookies to make curl send the request with cookies.

The syntax for sending a request with cookies:

curl -b /path/to/cookiefile  http://example.com/

Conclusion

In this tutorial, we learned how to use curl command in Linux and demonstrated some of the useful options with examples. For additional information,  visit the curl command Documentation page.

SHARE

Comments

Please add comments below to provide the author your ideas, appreciation and feedback.

Leave a Reply

Leave a Comment