Curl, short for Client URL, is a handy and flexible command-line tool used to transfer data between client systems. It supports a myriad of protocols such as FTP, FTPS, HTTP, HTTP, LDAP, POP3, and SCP just to mention a few.
[ You might also like: How to Download Files with Wget Command in Linux ]
Curl comes with options such as proxy support, bandwidth limiting, and the ability to fetch HTTP headers. In this guide, we take a deep dive into the curl command and illustrate its usage with command-line examples in Linux.
Installing Curl in Linux
Usually, curl comes preinstalled in most modern Linux distributions. However, there are a few exceptions and in such situations, you will be compelled to install curl.
To confirm if curl is installed, run the command:
$ curl -V
From the output, we can see that curl is already installed.
If curl is not already installed on your system, use any of the following commands to install it.
$ sudo apt install curl [On Debian, Ubuntu and Mint] $ sudo yum install curl [On RHEL/CentOS/Fedora and Rocky Linux/AlmaLinux] $ sudo emerge -a net-misc/curl [On Gentoo Linux] $ sudo pacman -S curl [On Arch Linux] $ sudo zypper install curl [On OpenSUSE]
The Curl Command Syntax
The basic syntax for the curl command is:
$ curl [options] URL
Without any arguments, curl displays the source code of the specified URL on your terminal.
Let’s look at some curl commands that are helpful when downloading files.
1. Download a File with Curl Command
You can download a file using curl and the
-O option followed by the URL of the resource to be downloaded
$ curl -O URL
-O option saves the file in its original name in the current working directory. It also displays the progress meter of the download.
For example, to download the Linux Kernel tarball run the command:
$ curl -O https://cdn.kernel.org/pub/linux/kernel/v5.x/linux-5.15.5.tar.xz
2. Download Multiple Files with Curl Command
You can download multiple files at one go using curl with the
-O option before every URL.
Here is the syntax:
$ curl -O URL1 -O URL2 -O URL3 ...
For example, to download the Git and WordPress tarball files in a single command, run:
$ curl -O https://mirrors.edge.kernel.org/pub/software/scm/git/git-2.35.0.tar.xz -O https://wordpress.org/latest.tar.gz
3. Limit File Download Speed with Curl Command
File downloads can considerably eat into your bandwidth and slow your browsing experience. Thankfully, you can set a transfer rate by using the
--limit-rate option followed by speed. The desired speed can be specified in kilobytes (k) or (K), megabytes (m) or (M), or gigabytes (g) or (G).
$ curl --limit-rate [value] [URL]
For example, the command below limits the download rate of the Node.JS binary file to 200 Kbps.
$ curl --limit-rate 200k -O https://nodejs.org/dist/v16.14.2/node-v16.14.2.tar.gz
4. Resume Broken File Download with Curl Command
Downloads usually get interrupted due to reasons such as network interruptions. You can use the
-C option with curl to resume a download from where it was interrupted.
For instance, you can resume the download of git in the event that the network is interrupted.
$ curl -C - -O http://yourdomain.com/yourfile.tar.gz
5. Fetch HTTP Headers of a URL with Curl Command
HTTP headers are an element of HTTP requests and they contain information such as the webserver type, user agent, HTTP version, content type, and more.
To request the HTTP header of a URL, use the
-I option as shown:
$ curl -I https://www.google.com
6. Transfer Files Using FTP and Curl Commands
You can upload and download a file from any FTP server using Curl and the
$ curl -u username:password FTP-server-URL
You can upload a file using the
-T flag as shown.
$ curl -u username:password -T file FTP-server-URL
The curl command comes with plenty of options that facilitate data transfer over the internet. For more information and options on curl, visit the curl documentation page.