How To Use Curl Command.
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.
The curl package is pre-installed on most Linux distributions today.
If curl is not installed you can easily install it using the package manager of your distribution.
Install Curl on Ubuntu and Debian
Install Curl on CentOS and Fedora
How to Use Curl
The syntax for the curl command is as follows:
In its simplest form, when invoked without any option, curl displays the specified resource to the standard output.
For example, to retrieve the example.com homepage you would run:
The command will print the source code of the nexonhost.com homepage in your terminal window.
If no protocol is specified, curl tries to guess the protocol you want to use, and it will default to HTTP.
Save the Output to a File
To save the result of the curl command, use either the -o or -O option.
Lowercase -o saves the file with a predefined filename, which in the example below is vue-v2.6.10.js:
Uppercase -O saves the file with its original filename:
Download Multiple files
To download multiple files at once, use multiple -O options, 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:
Resume a Download
You can resume a download by using the -C – option. This is useful if your connection drops during the 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:
and suddenly your connection drops you can resume the download with:
Get the HTTP Headers of a URL
HTTP headers are colon-separated key-value pairs containing information such as user agent, content type, and encoding. Headers are passed between the client and the server with the request or the response.
Use the -I option to fetch only the HTTP headers of the specified resource:
Test if a Website Supports HTTP/2
To check whether a particular URL supports the new HTTP/2 protocol , fetch the HTTP Headers with -I along with the –http2 option:
The -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 prints HTTP/2.0 200:
Otherwise, the response is HTTP/1.1 200:
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.
By default, curl doesn’t follow the HTTP Location headers.
If you try to retrieve the non-www version of google.com, you will notice that instead of getting the source of the page you’ll be redirected to the www version:
The -L option instructs curl to follow any redirect until it reaches the final destination:
Change the User-Agent
Sometimes when downloading a file, the remote server may be set to block the Curl User-Agent or to return different contents depending on the visitor device and browser.
In situations like this to emulate a different browser, use the -A option.
For example to emulates Firefox 60 you would use:
Specify a Maximum Transfer Rate
The –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 g suffix.
In the following example curl will download the Go binary and limit the download speed to 1 mb:
This option is useful to prevent curl consuming all the available bandwidth.
Transfer Files via FTP
To access a protected FTP server with curl, use the -u option and specify the username and password as shown below:
Once logged in, the command lists all files and directories in the user’s home directory.
You can download a single file from the FTP server using the following syntax:
To upload a file to the FTP server, use the -T followed by the name of the file you want to upload:
Sometimes you may need to make an HTTP request with specific cookies to access a remote resource or to debug an issue.
By default, when requesting a resource with curl, no cookies are sent or stored.
To send cookies to the 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 a:
curl is a command-line tool that allows you to transfer data from or to a remote host. It is useful for troubleshooting issues, downloading files, and more.