Curl Command in Linux with Examples

Introduction

curl is a command-line tool for transmitting data from or to a server that doesn't require any user input. You can download or upload data with curl using one of the supported protocols, which include HTTP, HTTPS, SCP, SFTP, and FTP. Curl has a lot of features, including the ability to resume transfers, limit bandwidth, support for proxy servers, user authentication, and more.

In this tutorial, we'll show you how to use the curl tool with practical examples and extensive explanations of the most common curl settings. We will also address a few FAQs on curl command in Linux.

Installing Curl

Most Linux distributions now come with the curl package pre-installed.

Open your terminal, type curl, and press enter to see if the curl package is installed on your machine. If you have curl installed, the system will print curl: try 'curl --help' or 'curl --manual' for more information. If you don't, you'll get a message like curl command not found.

Curl can be simply installed using your distribution's package management if it isn't already installed.

Install Curl on Ubuntu and Debian

sudo apt update
sudo apt install curl

Install Curl on CentOS and Fedora

sudo yum install curl

How to Use Curl

The curl command has the following syntax:

curl [options] [URL...]

curl, in its most basic version, prints the supplied resource to standard output when run without any options.

For instance, to get the homepage of example.com, type:

curl example.com

In your terminal window, the command will print the source code for the example.com webpage.

curl will try to guess the protocol you wish to use if no protocol is supplied, and it will default to HTTP.

Save the Output to a File

Use the -o or -O options to save the outcome of the curl command.

Lowercase -o saves the file with a predetermined name, such as vue-v2.6.10.js in the example below:

curl -o vue-v2.6.10.js https://cdn.jsdelivr.net/npm/vue/dist/vue.js

The command uppercase -O saves the file with the original name:

Download Multiple Files

Use multiple -O options followed by the URL to the file you wish to download many files at once.

We'll download the Arch Linux and Debian iso files in the following example:

curl -O http://mirrors.edge.kernel.org/archlinux/iso/2018.06.01/archlinux-2018.06.01-x86_64.iso  \
     -O https://cdimage.debian.org/debian-cd/current/amd64/iso-cd/debian-9.4.0-amd64-netinst.iso

Resume a Download

Using the -C - option, you can resume a download. This is beneficial if your connection drops during the download of a huge file, and you don't want to restart the download from the beginning.

For example, if you use the following command to download the Ubuntu 18.04 iso file:

curl -O http://releases.ubuntu.com/18.04/ubuntu-18.04-live-server-amd64.iso

If your connection drops unexpectedly, you can resume the download with:

curl -C - -O http://releases.ubuntu.com/18.04/ubuntu-18.04-live-server-amd64.iso

Get the HTTP Headers of a URL

HTTP headers are key-value pairs separated by colons that carry information such as the user agent, content type, and encoding. With the request or response, headers are exchanged between the client and the server.

To get only the HTTP headers of the specified resource, use the -I option:

curl -I --http2 https://www.ubuntu.com/

Test if a Website Supports HTTP/2

To see if a URL supports the new HTTP/2 protocol, use the -I option with the --http2 option to get the HTTP Headers:

curl -I --http2 -s https://vegastack.com/ | grep HTTP

curl will execute silently (quietly) and hide the progress meter and error notifications if you use the -s option.

curl prints HTTP/2.0 200 if the remote server supports it.

HTTP/2 200

Otherwise, you'll get HTTP/1.1 200:

Output

HTTP/1.1 200 OK

You don't need to use the --http2 option if you're using curl version 7.47.0 or newer because HTTP/2 is enabled by default for all HTTPS connections.

Follow Redirects

curl does not respect the HTTP Location headers by default.

If you try to access the non-www version of google.com, you'll find that you're diverted to the www version instead of getting the source of the page:

curl google.com

curl is told to follow any redirects until it reaches the final destination with the -L option:

curl -L google.com

Change the User-Agent

The remote server may be set to block the Curl User-Agent or return different data depending on the visitor's device and browser when downloading a file.

Use the -A option to imitate a different browser in instances like these.

To simulate Firefox 60, for example, you'd use:

curl -A "Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Firefox/60.0" https://getfedora.org/

Specify a Maximum Transfer Rate

You can limit the data transfer rate with the --limit-rate option. Bytes, kilobytes with the k suffix, megabytes with the m suffix, and gigabytes with the g suffix can all be used to indicate the value.

curl will download the Go binary and limit the download speed to 1 MB in the following example:

curl --limit-rate 1m -O https://dl.google.com/go/go1.10.3.linux-amd64.tar.gz

curl will not consume all of the available bandwidth if you choose this option.

Transfer Files via FTP

Use the -u option with curl and supply the username and password as indicated below to access a protected FTP server:

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

The command lists all files and directories in the user's home directory once logged in.

The following syntax can be used to download a single file from an FTP server:

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

Use the -T option followed by the name of the file you want to upload to the FTP server:

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

Send Cookies

To access a distant resource or troubleshoot an issue, you may need to make an HTTP request with appropriate cookies.

When using curl to request a resource, no cookies are sent or saved by default.

Use the -b switch followed by a filename containing the cookies or a text to transmit cookies to the server.

To download the Oracle Java JDK rpm file jdk-10.0.2 linux-x64 bin.rpm, for example. A cookie named oraclelicense with the value a must be passed:

curl -L -b "oraclelicense=a" -O http://download.oracle.com/otn-pub/java/jdk/10.0.2+13/19aef61b38124481863b1413dce1855f/jdk-10.0.2_linux-x64_bin.rpm

Using Proxies

HTTP, HTTPS, and SOCKS are among the proxy types supported by curl. Use the -x (--proxy) option, followed by the proxy URL, to send data through a proxy server.

The following command uses a proxy on 192.168.44.1 port 8888 to download the desired resource:

curl -x 192.168.44.1:8888 http://linux.com/

Use the -U (--proxy-user) option followed by the user name and password separated by a colon (user:password) if the proxy server requires authentication:

curl -U username:password -x 192.168.44.1:8888 http://linux.com/

FAQs on curl command in Linux

How do I use the curl command in Linux? 

To use curl, open a terminal and enter curl followed by the desired options and URL. For example, curl https://example.com will retrieve the content of the specified URL and display it on the terminal.

Can the curl command be used for file uploads or downloads? 

Yes, the curl command can be used for both file uploads and downloads. With appropriate options, you can upload files to a server or download files from a remote location using protocols such as FTP, SFTP, or HTTP.

What output formats are supported by the curl command? 

By default, the curl command displays the retrieved content directly on the terminal. However, you can use options like -o or --output followed by a filename to save the output to a specific file. Additionally, you can use options like -O or --remote-name to save the output with the original filename from the server.

Can curl follow redirects to retrieve content from redirected URLs?

Yes, by default, curl follows HTTP or HTTPS redirects. If a requested URL issues a redirect response, curl automatically follows the redirects until it reaches the final destination URL.

Can I specify custom headers or send data using the curl command? 

Yes, you can use the -H or --header option to add custom headers to your request. Additionally, curl supports sending data using various methods like GET, POST, PUT, DELETE, and more, using the -X or --request option.

Is it possible to limit or control the speed of data transfer with curl? 

Yes, curl provides options like --limit-rate to limit the download or upload speed of data. You can specify the desired speed in bytes per second.

Can I use curl to test APIs or perform HTTP request methods other than GET? 

Yes, curl is commonly used to test APIs or perform HTTP requests using different methods like POST, PUT, DELETE, etc. You can specify the method using the -X or --request option, followed by the desired method.

Conclusion

curl is a command-line tool for transferring data to or from a remote site. It can be used to diagnose problems, download files, and more.

The examples in this tutorial are straightforward, but they showcase the most common curl parameters and are intended to help you grasp how the curl command works.

Curl documentation can be found on the Curl Documentation page.

If you have any queries, please leave a comment below and we’ll be happy to respond to them.