Linux Commands

Linux Curl Command

The Linux curl command is a utility that transfers data to and from a server over the internet. With the curl command, you can download files on the internet over a wide array of supported protocols such as SCP, FTP, HTTP, and HTTPS just to mention a few. Among the options provided by the Linux curl command, this includes proxy support, bandwidth limiting, and the ability to resume file downloads in case of downtime. In this guide, we look at the curl command and provide some of the example usages.

Basic Syntax

Curl command takes the syntax shown below:

$ curl [options] URL

Installing Curl

Surprisingly, the Linux curl command does not come pre-installed in all Linux distributions. Just to verify if curl is installed, you can run the following command to check the curl version

$ curl -V

From the output, I’m using curl version 7.68.0

If curl is not installed, you may encounter the error curl command not found. To address this, you need to install the curl command-line utility.

So, here’s how you can go about the installation.

Install Curl on Ubuntu / Debian Distributions

If your Debian or Ubuntu distro doesn’t come with curl, install it as follows

$ sudo apt install curl (For newer versions of Ubuntu)

$ sudo apt-get install curl (For older versions of Ubuntu)

Install Curl on RHEL/CentOS Distributions

If you are running RHEL or CentOS, install curl as follows:

$ sudo yum install curl

Install Curl on Fedora

For Fedora, install curl as follows:

$ sudo dnf install curl

Download a File Using the Curl Command

In its basic form, the curl command is used to download files over the internet with the -O option.

$ curl -O url

The -O option displays the progress meter of a file download. Also, it saves the file in its original name.

For example, to download the latest WordPress compressed file, run the command:

$ curl -O https://wordpress.org/latest.tar.gz

Download Multiple Files Using the Curl Command

The curl command also gives you the option of downloading multiple files in one command using the syntax shown below.

$ curl -O url_1  O url_2

For instance, to download the compressed WordPress file and the Linux kernel tarball in one command, run the command:

$ curl -O https://wordpress.org/latest.tar.gz -O https://cdn.kernel.org/pub/linux/kernel/v5.x/linux-5.12.2.tar.xz

Resume an Interrupted File Download

If you are downloading a file and your connection suddenly drops, don’t fret. You can pass the -C option to resume the file download.

If, for instance, you were downloading a file over the internet as follows,

$ curl -O https://wordpress.org/latest.tar.gz

In the event that the connection drops, you can easily resume the download with the -C option.

$ curl -C - -O https://wordpress.org/latest.tar.gz

Limit the Download Rate

Curl command can consume significant amounts of your bandwidth. The speed is usually in bytes/seconds without any arguments. However, you can cap the data transfer rate by using the –limit-rate option followed by the value as follows:

Kilobytes    – k or K

Megabytes – m or M

Gigabytes – g or G

$ curl --limit-rate [value] [URL]

For example, the command below limits the download rate of the Go binary file to 500 kilobytes/sec.

$ curl --limit-rate 500k -O https://golang.org/dl/go1.16.4.linux-amd64.tar.gz

Grab HTTP Headers of a URL

HTTP headers form the core part of HTTP requests and contains information about the requested webpage such as the type of the web server, the version of HTTP being used, content type, content length and so much more.

The -I option lets you print out the HTTP headers using the syntax as follows:

$ curl -I URL

For example:

$ curl -I https://linuxways.net

Transfer Files Using FTP Protocol

Lastly, the curl command provides you with the ability to download a file from an FTP server with authentication using the -u option.

$ curl -u username:password [FTP_URL]

To upload a file to the FTP server, use the -T option as shown:

$ curl -u {username}:{password} -T file FTP_URL

Conclusion

As you have observed, the curl command is quite a versatile command-line utility that facilitates file transfer over various protocols. These are just a few examples. For more options, head over to the documentation page.

About the author

Karim Buzdar

Karim Buzdar holds a degree in telecommunication engineering and holds several sysadmin certifications. As an IT engineer and technical author, he writes for various web sites. He blogs at LinuxWays.