Linux Curl Command with Examples

A curl is a command-line tool that helps transfer the data from the server to the client and vice-versa. It uses the following protocols, i.e., HTTP, SMTP, FTP, and POP3. etc. Curl is known to transfer multiple files at a time.

Syntax

Curl [options…] [URL..]

Install Curl

Sometimes curl is already installed in Linux by upgrading the packages. By default, it is working, but if it is not installed, there is a simple installation method. If curl is not already installed in your Linux system, it can be easily configured using a couple of commands.

Update the packages

The first step in the installation is to update already existing packages; this will enable the repositories to install the curl in Ubuntu.

Curl installation

After enabling the repositories, we are now able to install curl. This may take some time to refresh all the repositories. Install curl now.

This process is quite easy. As you only have to provide a user password that is essential for user authentication.

After effective installation, you will be able to use the curl command on Linux operating system.

Check version

To see the installed curl version, you need to use the following statement in the Linux terminal as it also verifies the installation because the version is only shown if curl is installed.

The output shows that the version of the installed curl is 7.68.

Example1.

The simple example we are going to introduce. By using this command, you will be able to get the content of the URL of the website on the Linux terminal.

$ curl URL

$ curl https://ubuntu.com/download/desktop

In the output, you can see the HTML code of that specific website. You can use the curl command for more than one website. More than one URL can be written within the command.

curl http://site.{ 1st , 2nd , 3rd }.com

Example 2.

If you want to save the content of any website in a file, it can be done by using “-o” in the curl command. We specify the name of the file in the command. It is then automatically created and saved.

curl –o [URL]

$ curl  -o outputtxt.html https://ubuntu.com/download/desktop.html

When you type the above command in the terminal, you will see the columns and their values. This is a progress meter. It shows the amount of data that is transmitted. The speed of transferring and the time also. It contains all the package information. You can see our downloaded file in the document folder.

This file is present in the folder that shows the creation and updating of a file through the command.

If you want to save the file with the name of the default website, then there is no need to mention the name. You only have to use capital o “-O” instead of the small one. Then it will automatically save the content in the file having the name of the respective website.

Curl –O [url….]

$ curl –O https://ftp.us.debian.org/debian/pool/main/n/nano/nano_2.7.4-1_amd64.deb

You can check the file created by using a grep command on the terminal.

Hence the file is created.

Example 3

If the download process of any file in your system has been stopped, it can be resumed by using the curl command. This helps in the situation when you are trying to download large files, but it is interrupted because of any reason. This can be accomplished by using –C in the curl command.

Curl –C – [URL….]

$ Curl –C – – O ftp://spedtesttele2.net/1MB.zip

Example 4

Curl command does not follow the HTTP location header by default. These are also known as redirects. When a website request is sent to another place, then it’s original, then the HTTP location header is sent as a response. For instance, if we want to open a Google website and write google.com in the browser, it will be redirected to another page with specific text like “the document has moved”.

The request is moved to the document “https://www.google.co.in/-. This can be altered by using the -L option in the curl command. We then insist that curl use the redirection with -L. to download the source code in HTML of www.google.com.

Example 5

Headers of URL contain the key values that are detached with a colon. These key values contain information like encoding, decoding, user information, type content, user agent information, etc. Headers are transferred between the server and the client. This is done when the client requests the server, and the server is permissible to send the response. To fetch the headers of the URL, we use –I in the curl command

$ Curl –I – -http2 https://linuxhint.com

This is the header information of a respective source that we have provided in the command. This information has a content security policy, cache status, date, content type, etc. you can see in the output image.

Example 6

–libcurl is an option that allows the user to output the source code in C language that uses libcurl for the respective option. If this option is used in the cURL command, it helps developers add this in the command.

curl [URL..] –libcurl [file…]

$ curl https://www.nts.org.pk/new/ > log.html – – libcurl code.c

Example 7

The DICT is a protocol that is used in this curl command. It is defined by the libcurl. It works in the curl implementation. This protocol can be easily used to define or explain the meaning of the word in the respective dictionary of the URL. For example, we want to get the meaning of the word memory; then we will use it in the command in such a way that first the protocol is defined, that is, DICT, and then the dictionary path and then the word.

Curl [protocol: [URL]:[word]

$ Curl dict://dict.org/d:memory

The output will contain a detailed explanation of that word having meaning, usage, etc. we have shown a glimpse of a small part of that.

Example 8

–limit-rate is the option that allows you to restrict the rate of transferring the data. It limits the upper bound of the rate. It prevents the curl command from hosing the bandwidth. So it helps in allowing limitations in downloading rate. It will prevent earlier consumption. The value can be written in bytes or kilobytes as well. In this command, we have limited the speed up to 1 megabyte.

$ curl – -limit-rate 1m –O https://download-installer.cdn.mozilla.net……….tar

Example 9

We can also download the URL from the file. Consider a file named address.txt having URLs in it. We can display all the URLs by using a Cat command.

If we combine xargs with the curl command, it will download a file from the list of URLs.

$ xargs –n 1 curl –O address.txt

Conclusion

In this article, we have explained the installation of curl with its working independently and with other options, including almost 9 examples. I hope now you can easily implement your desired examples with the curl command.