Curl Command in Linux with Examples
6 min read
In this tutorial, we will show you how to use the curl tool through practical examples and detailed explanations of the most common curl options.
What is Curl?
Curl is command line utility for transferring data from or to a server designed to work without user interaction. With curl, you can download or upload data using one of the supported protocols including HTTP, HTTPS, SCP, SFTP, and FTP. Curl provides a number of options allowing you to resume transfers, limit the bandwidth, proxy support, user authentication and much more.
The curl package is pre-installed on most Linux distributions today.
To check whether the curl package is installed on your system, open up your console, type
curl, and press enter. If you have curl installed the system will print
curl: try 'curl --help' or 'curl --manual' for more information, otherwise you will see something like
curl command not found.
If curl is not installed you can easily install it using the package manager of your distro.
Install Curl on Ubuntu and Debian
sudo apt install curl
Install Curl on CentOS and Fedora
sudo yum install curl
How to Use Curl
The syntax for the `
curl command is as follows:
curl [options] [URL...]
options- The Curl options starting with one or two dashes.
URL- URL of the remote server.
In its simplest form when used without any option, curl will display the resource specified in the [url] to the standard output.
In the following example we are retrieving the
The command above will print the source code of the
example.com homepage in your terminal window.
If you don’t specify the protocol curl will try to guess the protocol you want to use and it will default to
How to Save the Curl Output to a File
To save the result of the curl command you can use either the
-o saves the file with a predefined filename, which in the example below is
curl -o vue-v2.5.16.js https://cdn.jsdelivr.net/npm/vue/dist/vue.js
-O will save the file with its original filename:
curl -O https://cdn.jsdelivr.net/npm/vue/dist/vue.js
How to Download Multiple files with Curl
To download multiple files at once use multiple
-O flags followed by the URL to the file you want to download. In the following example we are downloading the Arch Linux and Debian iso files:
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
How to resume a download with Curl
You can resume a download by using the
-C - option. This is useful if your connection drops during download of a large file and instead of starting the download from scratch you can continue the previous one.
For example, if you are downloading the Ubuntu 18.04 iso file using the following command:
curl -O http://releases.ubuntu.com/18.04/ubuntu-18.04-live-server-amd64.iso
and suddenly your connection drops 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 with Curl
HTTP headers are colon-separated key-value pairs containing information such as user agent, content type an encoding. Headers are passed between the client and the server with the request or the response.
-I option allows you to fetch only the HTTP headers of the specified resource:
curl -I --http2 https://www.ubuntu.com/
How to Test if a Website Supports HTTP/2 with Curl
To check if a particular URL supports the new HTTP/2 protocol, fetch the HTTP Headers with
-I along with the
curl -I --http2 -s https://linuxize.com/ | grep HTTP
-s option, tells curl to run in a silent (quiet) and hide the progress meter and error messages.
If the remote server supports HTTP/2, curl will print
Otherwise the response will be
HTTP/1.1 200 OK
If you have curl version
7.47.0 or newer you do not need to use the
--http2 option because HTTP/2 is enabled by default for all HTTPS connections.
How to Follow Redirects with cURL
If you try to retrieve the google.com homepage without
www you will notice the following:
As you can see from the output above
google.com redirects to the www version and because by default curl doesn’t follow the HTTP Location headers you are not getting the source of the Google homepage.
-L option which instructs curl to follow any redirect until it reaches the final destination:
curl -L google.com
How to Change the Curl User-Agent
Sometimes when downloading a file, the remote server may be set to block the Curl User-Agent or maybe the page serves completely different content depending on the visitor device and browser.
In situations like this you can emulate a different browser using the
-A option as shown below:
curl -A "Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Firefox/60.0" https://getfedora.org/
The command above will emulate Firefox 60 requesting the page from
How to Specify a Maximum Transfer Rate
--limit-rate option allows you to limit the data transfer rate. The value can be expressed in bytes, kilobytes with the
k suffix, megabytes with the
m suffix and gigabytes with the
In the following example curl will download the Go binary and limit the download speed to 1mb:
curl --limit-rate 1m -O https://dl.google.com/go/go1.10.3.linux-amd64.tar.gz
This option is useful when you don’t want curl to consume all the available bandwidth.
How to Transfer Files via FTP with Curl
To access a protected FTP server with the
curl command use the
-u option and specify the username and password as shown below:
curl -u FTP_USERNAME:FTP_PASSWORD ftp://ftp.example.com/
The command will list all files and directories in the user’s home directory.
You can download a file from the FTP server with:
curl -u FTP_USERNAME:FTP_PASSWORD ftp://ftp.example.com/file.tar.gz
To upload a file to the FTP server use the
-T followed by the name of the file you want to upload:
curl -T newfile.tar.gz -u FTP_USERNAME:FTP_PASSWORD ftp://ftp.example.com/
How to Send Cookies with Curl
When making requests using curl, no cookies are sent or stored by default. Sometimes you may need to make an HTTP request with specific cookies to access a remote resource or to debug an issue.
To send cookies to a server, use the
-b switch followed by a filename containing the cookies or a string.
For example, to download the Oracle Java JDK rpm file
jdk-10.0.2_linux-x64_bin.rpm you’ll need to pass a cookie named
oraclelicense with value
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
Curl supports different types of proxies including HTTP, HTTPS and SOCKS. To transfer a data through a proxy server, use the
--proxy) option followed by the proxy URL.
The following command will download the specified web page using a proxy on
curl -x 192.168.44.1:8888 http://linux.com/
If the proxy server requires authentication, use the
--proxy-user) option followed by the user name and password separated by a colon (
curl -U mark:Passvv0rd -x 192.168.44.1:8888 http://linux.com/
The examples shown in this tutorial are simple, but demonstrate the most used curl options and are meant to help you understand how the
curl command work.
To learn more about curl visit the Curl Documentation page.