Use Curl Command

Curl utility is used for transfer data from or to a server using one of the supported protocols including HTTP, HTTPS, SCP, SFTP, and FTP. It can also be used to download or upload files with supported options like proxy support, resume transfer and much more. The wget command is alternative way to transfer the files. In this tutorial, we have described how to use the curl command in linux with examples and detailed explanations of each common curl options.

Install Curl

Today, curl package comes pre-installed with most of Linux distributions. You can check whether it is installed or not, just open terminal and type curl and hit Enter key. If it’s installed then you should get output like this curl: try 'curl --help' or 'curl --manual' for more information , else you will see something like curl command not found.

Install Curl on Ubuntu and Debian

sudo apt install curl

Install Curl on CentOS and Fedora

sudo yum install curl

Syntax of Curl Command

Below is the basic system of the Curlcommand:

curl [options] [URL…]
  • options – Curl options starting with one or two dashes.
  • URL – Remote server URL.

Curl Command Examples

Common of the curl command

When the curl command is used without any options then it will return the source code of the given URL.

For examples, execute the below command to retrieve the source code of www.example.com

curl www.example.com

The above command will print the source code of the www.example.com home page in your terminal.

Download a file using the curl command

To download file using curl command you should pass -O (uppercase) or -o (lowercase) flag along with curl command. If you used -O (uppercase) then it will save file with original name while -o (lowercase) flag will save file with given name.

Run the below command using -O (uppercase) flag to save file with original file name:

curl -O http://www.example.com/test.zip

Now execute the following curl command along with -o (lowercase) flag to save it with another name newtest.zip

curl -o newtest.zip http://wwww.example.com/test.zip

Resume a download with Curl

This is useful when you are downloading a large size file and your connection drops in between. You can resume uncompleted downloads by using -C - option instead of starting from first.

For example, you are downloading Ubuntu ISO image file using below command:

curl -O http://ubuntu.biz.net.id/18.04.2/ubuntu-18.04.2-desktop-amd64.iso

and your internet connection drops then you can resume it from where it has stopped using below command:

curl -C - -O http://ubuntu.biz.net.id/18.04.2/ubuntu-18.04.2-desktop-amd64.iso

Send POST request with the Curl command

You can send POST request using curl command along with --data option. You can see in below example how to send the parameters as well:

curl --data "firstname=Abc&region=AR" https://www.example.com/insert

Get the HTTP Headers of a URL with Curl

Curl command can be used to get HTTP headers. Those are containing value like content-typ, user agent, encoding, etc. The -I option allows you to fetch only the HTTP headers of the specified resource:

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

How to Follow Redirects with cURL

Generally, the non-www versions of site are redirected to www versions of website. For example if you visit google.com, you will output as below:

In above output you can see that google is redirecting www version so you are not getting source code of Google home page.

At such time you should use the -L option along with curl command which instructs to follow any redirect until it reaches the final destination:

curl -L google.com

Limit maximum transfer rate

When you want to control your transfer rate for download then you can do it by using --limit-rate option. You can use command as given below:

curl --limit-rate 700K -O http://ubuntu.biz.net.id/18.04.2/ubuntu-18.04.2-desktop-amd64.iso

How to Change the Curl User-Agent

Due to server security of some website you face that you are not able to download file using curl. So when you try to downloading a file, the remote server block the Curl user agent and serves different page.

In such situations, you can try by changing a user agent using -A option:

curl -A "Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Firefox/60.0" https://getfedora.org/

Download Multiple files with Curl

To download multiple files at a time use -O option followed by the URL to the file you want to download.

For example, if you want to download Ubuntu server edition and desktop edition then you should pass command as below:

curl -O http://releases.ubuntu.com/18.04.2/ubuntu-18.04.2-desktop-amd64.iso -O http://releases.ubuntu.com/18.04.2/ubuntu-18.04.2-live-server-amd64.iso

Using Proxies

Using Curl command you can use proxies like HTTP, HTTPS, and SOCKS. You should use -x option along with command to transfer data through a proxy server.

In the below example we will use 192.168.43.125:8888 as a proxy to download backup.tar.gz file:

curl -x 192.168.43.125:8888 -O http://www.example.com/backup.tar.gz

If the proxy server needs to authenticate then you can use -U option followed by username and password separated by colon:

curl -U tecnstuff:PASSWORD -x 192.168.43.125:8888 -O http://www.example.com/backup.tar.gz

Thanks for reading and please provide your suggestions in below comment.

Leave a Reply