The Linux curl command

This article explains what is cURL command in Linux and How to use it with examples.

cURL command in Linux

cURL is a cross-platform utility that comes installed on most Linux distributions. Basically, it is used to transfer files from a server but without user intervention. This means that once it is executed and we dictate the parameters, nothing else has to be done.

It is also used in cars, television sets, routers, printers, audio equipment, mobile phones, tablets, settop boxes, media players and is the internet transfer backbone for thousands of software applications affecting billions of humans daily. From https://curl.haxx.se/

As you have noticed, cURL is used a lot in Linux scripts, this tells us about the robustness and usefulness of the tool.

The curl  command support below protocols,

  • HTTP and HTTPS.
  • FTP, SFTP, and FTPS.
  • IMAP, IMAPS, POP3, and POP3S.
  • SMB and SMBS.
  • SCP.
  • TELNET.
  • LDAP and LDAPS.
  • SMTP and SMTPS.

It also works with libcurl, which is a free URL transfer library on the client-side and also with a command interpreter which is curl.

This is open-source distributed under the MIT License and compatible with almost all existing Linux distributions.

cURL automates file transfers or unsupervised operation sequences.

Install curl on Linux

Curl should already be installed on your Linux distribution. If that is not the case, run these commands depending on the Linux distribution you use.

Note – curl is available from the official repositories of almost any current distribution.

Debian, Ubuntu, Linux Mint or one of the derivatives like ElementaryOS

:~$ sudo apt install curl

Fedora, Red Hat, CentOS, and derivatives

:~$ sudo dnf install curl

OpenSUSE

:~$ sudo zypper in curl

Anyway, in the download section of the curl website, there are the packages for each case.

How to use curl command

Curl is a program that is used through the command with the same name. This command has a basic syntax and is very easy to memorize.

:~$ curl [options...] <url>

To see the rest of the options, just run the curl command along with the option that displays the help.

:~$ curl – help

The Linux curl command

As you can see, there are many different options, however, you will learn the most useful and common situations of the command.

The most basic use of curl is to display the content of a website.

:~$ curl google.com

When the command is executed, you will see the source code of the website. Since no protocol is specified, curl will take HTTP as its default.

You can enhance the above command with the help of the less command to scroll through the terminal

:~$ curl google.com | less

And if you want to download that source code to your computer, you can add the -o option and give a name to the generated file.

:~$ curl google.com -o google.html

That is how easy it is to get the source code for a website.

Downloading a file with curl

One of the most common uses of curl is to download a file from a server. This is probably the most used option.

To do this, we have two options that will help us a lot. The first is -o will allow us to define the name of the downloaded file. The second is -O (capital) which will take the name of the file that the remote. That is, without any modification.

So to download files, just run the following command:

:~$ curl [options] [url]

For example:

:~$ curl -O http://example.com/file.tar.gz

This will download the file.tar.gz to the current working directory. If we want to define another name we use this one:

:~$ curl -o newfile.tar.gz http://example.com/file.tar.gz

If for some reason the download stops, you can use it again by using the -C option like this:

:~$ curl -C - -O http://example.com/file.tar.gz

You can also download several files in one command:

:~$ curl -O http://example.com/file.tar.gz -O http://example2.com/file2.tar.gz

Download a password-protected file

Some servers protect the download of certain files by requesting a username and password. These are usually provided by the server administrator.

With the -u option you can specify a username and password and download using curl.

:~$ curl -u user:password http://server/file -o filename

Remember to change the parameters to your own. Also in the server field can be an IP address or domain name. Finally, assign a name to the file with its extension.

The Linux curl command also works behind a proxy

In case you are behind a proxy, you can also tell curl this. This way the program will be executed correctly. For example,

:~$ curl -x proxyserver -U username:password -O http:// example.com/file.tar.gz

If the proxy does not require a username and password, you can omit the -U option. You can also specify a listening port on the proxy server.

Downloading a file from FTP with curl

In the previous section, you have learned to download a file with curl but in the HTTP protocol. However, it happens that some files are usually on an FTP server that requires a username and password. So curl can also work with FTP.

This way, you can download a file using FTP.

:~$ curl ftp://FTP-server/file -user user:password -o file_name

Replace the fields of the previous command with your own.

Ignore a website’s SSL certificate

It is not advisable but sometimes we can ignore a website’s SSL certificate. To do this, use the -k option as follows:

:~$ curl -k [url]

This will mean that the curl does not have to establish a secure connection with the server. This may not be recommended.

Limit download rate with the Linux curl command

For some good reason, it is advisable to limit the discharge rate by using curl. This is useful in shared networks or where we don’t want to affect the total bandwidth.

In this case, the option that allows this is –limit-rate and setting a limit in Mbs or Kbs

~$ curl – limit-rate 900k [url]

Of course, you can use other extra options like -O or -o

Conclusion

The curl command is one of the powerful commands in Linux and is so simple that it even goes unnoticed many times. Thanks to this tool, it is possible to download files and connect to servers with multiple targets.

This command is quite useful in performing scripts that do not require user intervention. On the other hand, curl is also used to perform tests on different servers.

Have you used to curl? do you like it? what do you use it for? let us know.

Curl tutorial

Share This: