Curl command in Linux with 15 practical examples

Linux Tutorials

July 21, 2020 by Harry

curl command

In the mid-1990s, a Swedish programmer named Daniel Stenberg started a project called Curl, which eventually became what we see today as Curl command.

Initially, he started with the goal of developing a bot that would periodically download exchange rates from a web page, offering the Swedish krona equivalent in US dollars to IRC users (a way to chat and hold web conferences).

In short, the project grew as several protocols and other features were added over time. Now let’s see how to use Curl to transfer data and so on in Linux.

In the list below, we have put 15 curl commands together for you, which are as follows.

curl commands examples

Note: Before you start to read this article, you can buy Linux VPS to test the below commands.

1 – Show curl version

The -V or –version option shows you not only the version but also the supported protocols and features of the current version.

$ curl --version

curl 7.47.0 (x86_64-pc-linux-gnu) libcurl / 7.47.0 GnuTLS / 3.4.10 zlib / 1.2.8 libidn / 1.32 librtmp / 2.3
Protocols: dict file ftp ftps gopher http https imap imaps ldap ldaps pop3 pop3s rtmp rtsp smb smbs smtp smtps telnet tftp
Features: AsynchDNS IDN IPv6 Largefile GSS-API Kerberos SPNEGO NTLM NTLM_WB SSL libz TLS-SRP UnixSockets

2 – HOW TO DOWNLOAD A FILE using a curl command

If you need to download a file, you can use curl with -O options. The first option will save the file in your current directory with the same name, and the second option will allow you to save the file with another name or specified directory that you want.

$ curl -O http://yourdomain.com/yourfile.tar.gz # Save as yourfile.tar.gz
$ curl -o newfile.tar.gz http://yourdomain.com/yourfile.tar.gz # Save as newfile.tar.gz

3 – Continue a discontinued download

If a download is interrupted for any reason (for example, you used ctrl + c), you can easily continue downloading it. Use “-C –” (ie dash and C then enter space and dash again) which tells curl to start the download from where it left off.

$ curl -C - -O http://yourdomain.com/yourfile.tar.gz

Click on image to enlarge Name: curl command-1.png Views: 0 Size: 47.2 KB ID: 26611

4 – Download multiple files from different URLs

You can download a list of URL files contained within a text file by combining curl with xargs.

$ xargs -n 1 curl -O <listurls.txt

Click on image to enlarge Name: curl command-2.png Views: 0 Size: 85.7 KB ID: 26612

6 – Use Proxy with authentication

If you are behind a proxy server listening on port 8080 at proxy.yourdomain.com, run the following command.

$ curl -x proxy.yourdomain.com:8080 -U user: password -O http://yourdomain.com/yourfile.tar.gz

If your proxy does not need authentication (user and password) you can ignore -U user: password.

7 – HTTP header query

HTTP headers allow remote web servers to send more information about themselves along with the actual request. This provides the client with a request for details.

Run the following command to request HTTP headers from a website.

$ curl -I host.linux-zone.org

Click on image to enlarge Name: curl command-3.png Views: 0 Size: 57.6 KB ID: 26613
This information is always available in your browser development tools.

8 – Create a POST request with parameters

The following command sends the firstName and lastName parameters along with their variables to https://yourdomain.com/info.php.

$ curl --data "firstName = John & lastName = Doe" https://yourdomain.com/info.php

You can use this method to simulate the behavior of an HTML form.

9 – Download the file from an FTP server with authentication

If you want to download the file yourfile.tar.gz from another server from a remote FTP server, just run the following command to download the file to your current directory.

$ curl -u username: password -O ftp: //yourftpserver/yourfile.tar.gz

If the FTP server allows anonymous users to access, you can override the authentication here -u username: password.

10 – Upload files to an FTP server with authentication

To upload a file called mylocalfile.tar.gz to ftp: // yourftpserver using curl, run the following command.

$ curl -u username: password -T mylocalfile.tar.gz ftp: // yourftpserver

curl command example

11 – Specify User-Agent

The user agent is part of the information that is sent with an HTTP request. This shows what browser the client is using to create a request. Let’s see if our current curl version is used by default, and let ‘s change it to “I am a new web browser” later.

$ curl -I http: // localhost --user-agent "I am a new web browser"

Click on image to enlarge Name: curl command-4.png Views: 0 Size: 68.3 KB ID: 26614

12 – Save cookies on a website

Want to see which cookies were downloaded to your computer when you visited https://www.cnn.com? Use the following command to save them to the cnncookies.txt file. Then you can use the cat command to view the file.

$ curl --cookie-jar cnncookies.txt https://www.cnn.com/index.html -O

Click on image to enlarge Name: curl command-5.png Views: 0 Size: 60.0 KB ID: 26615

13 – Send website cookies

You can use the cookies you obtained from the previous trick in subsequent requests to the same site.

$ curl --cookie cnncookies.txt https://www.cnn.com

14 – Rename resolution

If you are a web developer and want to test a version of yourdomain.com before launching it live, you can use curl to view http://www.yourdomain.com locally as below do.

$ curl --resolve www.yourdomain.com:80:localhost http://www.yourdomain.com/

Therefore, requesting http://www.yourdomain.com tells curl to use localhost instead of DNS or the / etc / hosts / file.

15 – Limit download speed

To prevent curl from using your bandwidth, you can limit the download speed to 100 KB / s, as shown below.

$ curl --limit-rate 100K http://yourdomain.com/yourfile.tar.gz -O

Summary

In this article, we have shared a brief history of Curl and explained how to use the Curl command with 15 practical examples.

Do you know any other curl commands that may not be mentioned in this article? Be comfortable with us and share it in the comments section. Also, if you have a question, ask it. we are waiting for you.

Helpful

About the Author Harry

I like to learn and to teach to make things easier

Leave a Reply

Your email address will not be published. Required fields are marked *