The curl command in Linux is a great way to automate the process of uploading and downloading data from a server. Curl supports all significant protocols such as HTTP, HTTPS, SFTP, FTP, and more.

Features like pausing and resuming transfers, limiting bandwidth, proxy support, and user authentication make it a perfect command-line utility for data transfer.

Let's take a deep dive and explore what is curl and how to use it.

What Is the curl Command?

Curl was initially developed to automate downloading currency rates from a web page periodically. The real-time currency rates provided Swedish Kronor equivalents in US dollars to IRC users. The popularity of curl grew exponentially as people started using its widespread applications.

Curl is one of the oldest and most popular open-source projects to ever exist. The name stands for Client URL, and is used in countless systems around the world. Curl finds its use in a variety of applications like web development and bug testing.

Developers don't need to type the commands manually; they can bundle them into scripts and use them to automate complex operations. Here are some examples of how to use curl.

Installing curl on Linux

Although this popular package comes pre-installed on most Linux distros, you can easily download curl if it's not already installed on yours. Use the following commands to install curl on your machine.

On Ubuntu and Debian:

        sudo apt install curl
    

On RHEL-based distros like CentOS and Fedora:

        sudo yum install curl
    

To install curl on Arch Linux, type:

        sudo pacman -S curl
    

Related: How to Manage systemd Services Using the systemctl Command in Linux

How to Use the curl Command

Curl uses the following syntax for all its commands:

        curl options url
    

...where options and url change according to the task at hand. Curl downloads the source code of the URL, whenever you don’t mention the option/task. The latest curl version also guesses the protocol—if it’s not mentioned in the URL—and defaults it to HTTP.

Here are some helpful curl commands to perform various functional tasks:

Download a File Using curl

You can download resources through a specified URL using curl. This command has two flags that you can use; -o and -O.

  • The -O command saves the file in the current directory with the same name as in the remote server.
  • The -o command, on the other hand, lets you choose the file name and location.

Here is an example of both these commands:

        curl -O https://cdn.jsdelivr.net/npm/vue/dist/vue.js
    

The file gets downloaded in the current directory, with its original name as on the remote server.

        curl -o newfile.tar.gz http://yourdomain.com/yourfile.tar.gz
    

When you run the aforementioned command, curl will download and save the file as newfile.tar.gz. Note that you'll have to specify the file name and path to the directory as the input.

Resume an Interrupted Download Using curl

Downloads can get interrupted due to multiple reasons, like network interruptions or an expired link. The feature to resume an interrupted download helps in such situations by saving you from the hassle of downloading the entire file again.

Use the -C flag with either -o or -O to resume an interrupted download using curl.

        curl -C -O http://yourdomain.com/yourfile.tar.gz
    

Downloading Multiple Files Using curl

There is no dedicated command to download multiple files simultaneously through curl, but you can use the -o or -O flags multiple times in the same command to achieve similar results.

For example:

        curl -O http://website1.com/file1.iso -O https://website2.com/file2.iso
    

The aforementioned command will download the ISO files from both URLs simultaneously in the current directory.

Get the HTTP Headers of a Web Page

HTTP headers contain crucial information such as the user agent, content type, and encoding. You can request the HTTP header of a URL using the -I (capitalized i, not L) and --http2 flags:

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

Output:

Get the HTTP Headers of a URL

The -I command shows various information including the server software details, date, content type, X-cache status, and more.

Specify a Maximum Transfer Rate

File transfers usually acquire most of your bandwidth, restricting you from doing any other task.

To tackle this issue, you can set a maximum transfer rate for your downloads using curl. Use the --limit-rate method followed by a k, m, or g (denotes kilobytes, megabytes, or gigabytes respectively) modifier.

The following command restricts the transfer speed to 1MBps:

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

Output:

Specify a Maximum Transfer Rate

Transfer Files via FTP and curl

You can transfer files using the File Transfer Protocol by accessing any FTP server using curl. Curl gives you the functionality to download and upload files after connecting to the FTP server.

Use the following command to connect to an FTP server:

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

...where username and password are the credentials for logging in to the server.

You can download any file from the server by specifying the file URL in the following command:

        curl -u username:password -O ftp://ftp.example.com/file.tar.gz
    

You can also upload files to the FTP server using the -T flag:

        curl -T newfile.tar.gz -u username:password ftp://ftp.example.com
    

The -T command followed by the specified file uploads it to the FTP server.

Related: How to Securely Transfer Files on Linux With sftp

Using Proxies With curl

Curl supports a plethora of different proxies like HTTPS, HTTP, and SOCKS. You can use these proxies to transfer data securely using curl.

Use the following command to set up proxies in curl:

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

The -x option connects you to port 8888 on the server 192.168.44.1. Make sure that you replace the proxy details in the command with the proxy you want to connect to. The aforementioned proxy server doesn't require authentication, but you can access a protected proxy using the following syntax:

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

Specify the username and password separated by a Colon next to the -U flag.

View Version Details

This command comes in handy when you want to check the package's version information. Use the --version flag to view the curl version:

        curl --version
    

The output should mention the curl version, its protocols, and features.

Identifying the Benefits of curl Command in Linux

Curl comes equipped with plenty of options to automate data transfer on your machine. The feature to resume interrupted downloads and FTP support comes in handy in several applications.

The examples in this tutorial aim to show the most popular curl commands with their respective codes to help you understand the curl command better.