Serverspace Black Friday
JT
May 16 2024
Updated July 12 2024

How to use Curl in Linux?

Linux Maintenance

CURL or client URL is a multifunctional utility that allows you to communicate with servers using HTTP, HTTPS, FTP, FTPS, SCP, SSH and many other protocols. The main application users have found is to use the utility to make requests to web servers. However, the functionality of the utility, thanks to the libcurl library, leaves a large scope for possibilities.

How to install curl?

Installation is standard, as for any Linux package of a similar system. It is necessary to update the repository first, if you use apt-manager, then type the command:

apt update && apt upgrade \
apt install curl -y

For an RPM-like package manager, use the appropriate commands:

dnf update && dnf upgrade \
dnf install curl -y
Curl installation
Screenshot №1 — Curl installation

Having updated the repositories and installed the utility, let's turn to the tooltip and divide the help into categories or protocols of operation:

curl --help category
Help category
Screenshot №2 — Help category

This list provides a complete list of protocols that can be used to communicate with servers. If you replace the word category in the previous command with one of the given items, a detailed guide on how to use each of the options will open:

curl help http

The command above is as an example of searching for commands to work over the http protocol.

Basic examples of work

Let's look at the basic examples for working with the utility, but before that let's define the syntax used:

curl [options] <URL>

The main command for calling the curl utility, its main options, and the URL itself, which is accessed.

How do I download a file using Curl?

By default, when accessing the site curl forms a GET request, which asks the server for the headers and the file itself. To save the file, write the command:

curl -Lo /tmp/articles.pdf <URL>
Download file
Screenshot №3 — Download file

Note that to download from a site that may have a self-signed certificate, you must additionally use the -k option.

After downloading the file will be saved in the directory /tmp/ under the name articles.pdf, for which the -o option is responsible. The other -l option means that if the site responds with a 301, 302, etc. redirect code, curl will automatically go to the correct site and download the file. The file can also be left with the original name and downloaded to the current directory:

curl -Lo <URL>

However, if you want to output the file directly to the terminal, leave a dash instead of the file name:

curl -Lo - <URL> 

Such a solution will allow you to view the contents of a file faster with its headers and meta description, which may be necessary to check the contents of the file. For example, pdf for the presence of embedded scripts.

Please note that outputting the file to the terminal may disturb its correct work. That's why it is recommended to save to a file.

All steps in the tutorial can be performed on powerful cloud servers. Serverspace provide isolated VPS / VDS servers for common and virtualize usage.

Create Server
Screenshot №4 — Create Server

It will take some time to deploy server capacity. After that you can connect in any of the convenient ways. Let's return to explore utility.

How do I view headings using Curl?

Before downloading this or that file or caching, it is necessary to collect information about the page/file that will be accessed. For example, a pdf file is hosted on the site and it is necessary to find out its name, size and data about the last modification of the file. To do this, let's write the following command:

curl -lkI <URL>
Head of file
Screenshot №5 — Head of file

The MIME content-type field categorises the file as pdf. The next field, last-modified, contains the last modification date and the content-length field contains the size of the document body.

Continued interrupted download

Suppose we started downloading a file and then accidentally cancelled it. If the Accept-Ranges: bytes line is present in the server response headers, we can use the function to continue the cancelled download. To do this, the -c parameter must be added to the usual list of options when downloading -Lko, which will allow us to continue the download:

Continue download
Screenshot №6 — Continue download

The file will then be saved to the directory where the user is currently located.

How to change User-Agent or other header?

While working with a web server, you may need to implement custom headers. For this purpose, curl has the -H option, which will allow you to add a header with a value:

curl -LkvH "x-os-client: MacOS" <URL>
Custom header
Screenshot №7 — Custom header

There is a separate option -A for similar customisation of the User-Agent field. It must be specified with a new agent value, for example, the command may look like this:

curl -LkvA "MacOS" <URL>
User Agent
Screenshot №8 — User Agent

The use of CURL facilitates various network operations and gives the user flexibility and control when interacting with servers. Thanks to its capabilities, users can efficiently perform a variety of tasks related to data exchange.

Vote:
5 out of 5
Аverage rating : 5
Rated by: 1
33145 North Miami, FL 2520 Coral Way apt 2-135
+1 302 425-97-76
700 300
ITGLOBAL.COM CORP
700 300

You might also like...

We use cookies to make your experience on the Serverspace better. By continuing to browse our website, you agree to our
Use of Cookies and Privacy Policy.