15 Practical examples of curl command

Curl is the most essential command-line tool in Linux / UNIX for transfer data to or from a server using various protocols.

Most common supported protocols to curl utility are HTTP, FTP, IMAP, POP3, SCP, SFTP, SMTP, TFTP, Telnet, LDAP, etc.

Curl has capabilities to transfer multiple files at once.

We can do many more thing using curl utility using various options.

Suggested Reading: Linux curl command help and examples

Examples of curl command:

Example 1: curl use to download a file

You can use to download files from internet protocol http, https, FTP, etc.

To download a file from the internet, you can use curl with an option “-o,” and it will download and save the file into your current working directory with the same name an in the remote server.

# curl -O https://linuxconcept.com/pdf_doc.tar.gz

Example 2: curl use to download multiple file

You can also download multiple files using CURL using the same option “-o” with all files, as shown below:

# curl -O https://linuxconcept.com/pageinfo.html -O https://linuxconcept.com/about_us.html 

Example 3: curl use to download multiple files from list

Sometimes you have a list of files to download from internet or remote servers; In this case, you can make a text file with the list of downloaded files and use the “xargs” with curl to download all files with a single command, as shown below:

# xargs -n 1 curl -O < listfiles.txt

Example 4: Resume download uncompleted file

The CURL support resume to download interrupted files. To use these functionalities, use the “-c” option with curl, as shown below:

# curl -C - -O https://linuxconcept.com/pdf_doc.tar.gz

Example 5: CURL use proxy

Sometimes you use the proxy to surf the internet, especially in office in that case also you can use the curl command to download the file.

You can use CURL like below example to use the proxy network.

# curl -x proxy.linuxconcept.com:8080 -U user:proxyweb -O https://linuxconcept.com/pdf_doc.tar.gz

Example 6: HTTP Headers query

Use HTTP header query to get the additional information from the remote server along with the actual request.

To request HTTP headers using curl command, follow below command:

# curl -I www.linuxconcept.com

Example 7: CURL use to POST request with parameters

You can use the curl command to post request with URL and parameters.

For example, in below command, we pass firstname and lastname parameters with their values, to https://linuxconcept.com/info.php

# curl --data "firstName=John&lastName=Shown" https://linuxconcept.com/info.php

Example 8: CURL use to download files from FTP server

You can also use the curl command to download a file from FTP server.

Here we download a file “curltips.tar.gz” from ftp://linuxconcept.com using curl command as shown below:

# curl -u username:password -O ftp://linuxconcept.com/curltips.tar.gz 

You can remove “-u username:password” from above command if your FTP server allows anonymous login, if not you should replace username and password with your credentials.

Example 9: CURL use to upload files to an FTP server

The curl command also use to upload a file on FTP server.

Here we use curl command to upload a file “myform.tar.gz” to ftp://linuxconcept.com, as shown below:

# curl -u username:password -T myform.tar.gz ftp://linuxconcept.com

Example 10: CURL can specify User agent

The user agent is sent information along with the HTTP request. It is use to identify the user’s platform and client application which is used to request any server. Generally, this information represents a web browser as the web browser generates maximum http requests.

By using curl command, you can specify user agent who will send through https request, as shown below:

# curl -I https://linuxconcept.com --user-agent "The new web browser"

Example 11: CURL use to store website cookies

The curl command also helps you to view and store website cookies into a file.

For example, if you want to see the cookies of https://www.cnn.com which is get downloaded into your browser when you browse the site. You can use below command to store cookies information into file “cnncookies.txt” which you can view using the cat command.

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

Example 12: CURL use to send website cookies

You can use the store cookies retrieved in the last example in a subsequent request to the same site.

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

Example 13: Modify name resolution

Curl command helps us to test development or staging sites using name resolution before moving it on a production server.

For example, the local version of “linuxconcept.com” before publishing it live, you can make curl resolve https://linuxconcept.com to your localhost system, as shown below:

# curl --resolve www.linuxconcept.com:80:localhost https://linuxconcept.com/

The above query to https://linuxconcept.com will tell curl to request the site from localhost instead of using DNS or /etc/hosts file.

Example 14: Limit download rate in CURL command

To use your network bandwidth with optimal utilization, you can limit the download rate while using curl command.

You can limit the download bandwidth with curl command, as shown below:

# curl --limit-rate 100K https://linuxconcept.com/curltips.tar.gz -O

Example 15: Check the version of CURL

To check the version of curl utility in Linux use it with the option “-v” or “–version,” as shown below:

# curl --version

0 Comments

Submit a Comment

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

Related Articles