What is curl command in linux

Linux curl Command Explained with Examples

Transferring data to and from a server requires tools that support the necessary network protocols. Linux has multiple tools created for this purpose, the most popular being curl and wget.

This tutorial will show you how to use the curl command and provide you with an exhaustive list of the available options.

Linux curl Command Explained with Examples

Note: If you do not have curl installed, install it by typing the following in the terminal:

What Is the curl Command?

curl (short for «Client URL») is a command line tool that enables data transfer over various network protocols. It communicates with a web or application server by specifying a relevant URL and the data that need to be sent or received.

curl is powered by libcurl, a portable client-side URL transfer library. You can use it directly on the command line or include it in a script. The most common use cases for curl are:

  • Downloading files from the internet
  • Endpoint testing
  • Debugging
  • Error logging

curl Syntax

The basic curl syntax is as follows:

curl https://www.gnu.org/gnu/gnu.html

The system outputs the HTML contents found on the URL provided after the curl command.

HTML contents found on the URL provided after the curl command

If you specify a URL that leads to a file, you can use curl to download the file to your local system:

The progress bar shows how much of the file has been downloaded so far.

The progress bar shows how much of the file has been downloaded

The syntax of URLs that are part of the command depends on the protocol. Multiple URLs that differ in one part are written together using braces:

Alphanumeric series are written with brackets:

While nested sequences are not supported, multiple sequences are allowed:

http://url.com/archive2014/vol3/part.html

curl Protocols

curl supports numerous protocols for data transfer. Find the complete list below.

Protocol Description
DICT A dictionary network protocol for querying dictionary servers about the meanings of words.
FILE A URL scheme for obtaining a file from the local file system using curl.
FTP, FTPS File Transfer Protocol, used for file transfers between clients and servers. FTPS is the version of the same protocol with an added SSL/TLS security layer.
GOPHER, GOPHERS An old protocol for searching, retrieving, and distributing internet documents, a precursor of HTTP. GOPHERS is the version of the same protocol with an added SSL/TLS security layer.
HTTP, HTTPS Hypertext Transfer Protocol, used for web and internet data transfer. HTTPS is the version of the same protocol with an added SSL/TLS security layer.
IMAP, IMAPS Internet Message Access Protocol, used for email access and management. IMAPS is the version of the same protocol with an added SSL/TLS security layer.
LDAP, LDAPS Lightweight Directory Access Protocol, used for distributed directory information access and management. LDAPS is the version of the same protocol with an added SSL/TLS security layer.
MQTT Message Queuing Telemetry Transport — a protocol for data exchange between small devices, usually IoT systems.
POP3, POP3S Post Office Protocol version 3 — a protocol for email retrieval from a server. POP3S is the version of the same protocol with an added SSL/TLS security layer.
RTMP Real-Time Messaging Protocol — a streaming protocol for audio, video, and other data.
RTSP Real Time Streaming Protocol, used for streaming media servers management.
SCP Secure Copy — a protocol for copying files to and from an SSH server.
SFTP SSH File Transfer Protocol — a version of the File Transfer Protocol using the SSH connection.
SMB, SMBS Server Message Block — a protocol for managing shared access to files and computer peripherals. SMBS is the version of the same protocol with an added SSL/TLS security layer.
SMTP, SMPTS Simple Mail Transfer Protocol — an email protocol for easy transmission of email. SMTPS is the version of the same protocol with an added SSL/TLS security layer.
TELNET An application layer protocol for bidirectional interactive text-oriented communication.
TFTP Trivial File Transfer Protocol, used for uploading or downloading files to or from a remote host.
Читайте также:  Узнать файловую систему флешки linux

curl Command Options

curl accepts a wide array of options, which makes it an extremely versatile command. Options start with one or two dashes. If they do not require additional values, the single-dash options can be written together. For example, the command that utilizes the -O , -L , and -v options can be written as:

The list of all the available options is given below.

Источник

What Is Curl Command in Linux and How to Use It

featured image for what is curl and how to use it

Client URL or cURL is a data transfer tool in Linux that can make different kinds of requests from the client side to any remote server. With the curl command, you can make simple and complex requests to the server to access the necessary information. In this article, we explain what is the curl command and its different use cases in Linux for you to harness its power.

What Is the curl Command in Linux

cURL stands for ‘Client URL’ and is used to transfer data to and from a remote server using different types of network request types. It is essentially made up of two components – the command line tool “curl” and the “libcurl” library.

Both cURL and curl are sometimes interchangeably used, but they have some differences — cURL is a complete data transfer application that can be used with different languages. On the other hand, curl is a command line tool that uses the libcurl library to send and receive data on your Linux distro.

curl Command: Syntax and Options

Now that you know what is curl, it’s time to understand the syntax of the curl command in Linux:

Читайте также:  Nginx astra linux orel

We have listed some of the common options you can use with the curl command here, but you can check out other cool Linux commands via the linked article.

Options Description
-# Shows a progress bar when downloading a file using curl
-o Saves the downloaded file with a different name specified on the client’s system
-O When used, it will save the downloaded file with the same name as the
-T Used to upload a file to an FTP server
-x Accesses the via a proxy server
-w Make curl display information on stdout after a completed transfer

How to Use curl Command in Linux (6 Examples)

Now that you have a basic understanding of the syntax and options to use with the curl command, let us see some practical examples of using the curl command.

1. Saving a File using the curl command

When you use the curl command without any , it simply prints the source code of the web page you’re sending the request to. To save this output to a file in your Linux file system, use the curl command with the -o flag:

curl -o index.html https://test.rebex.net/

save the webpage in a file with curl

2. Testing If a Server Is Available or Not

With the -I flag, you can use the curl command in Linux to check whether a server is available or not. The syntax is:

Here, check for the first line of the response. If you get “200 OK” in the response, it means that the server is working fine while any other response implies the server is not working properly. For example, to check if test.rebex.net is available or not, use the following syntax:

using the curl command in Linux to check if a server is available or not

3. Accessing Cookies with curl Command

Whenever you visit a URL, some information gets stored in your system, which is later used when you visit next time the same URL. So, just use the —cookie-jar flag with the curl command to access the cookies stored in your Linux file system for the given :

For example, here’s the command to store all the cookies of https://test.rebex.net/ in the cookies.txt file:

curl --cookie-jar cookies.txt https://test.rebex.net/

saving the cookies in a file with curl command in Linux

4. Download Files from FTP Server using curl

Normally, when accessing a secure FTP server, you need to enter the password every time you log in to the server. But with curl, you can directly specify the username and password with the following syntax:

For example, you can use the following command to download the readme.txt file from the test.rebex.net FTP server with “demo” as the username and “password” as the password:

curl -v -u demo:password -O ftp://test.rebex.net/readme.txt

downloading a file from a ftp server

5. Setting User Agent Value with curl

Whenever you make a request to a server from any browser, the request body contains a “user-agent” argument that specifies which browser version you are using to send the request.

With the curl command, you can spoof the device and browser version you are using with the –user-agent flag. For example, if you want to use Mozilla version 4.73 on an X11 Linux system with the kernel version 2.2.15 and i686 architecture to test.rebex.net, you should use the following command:

curl --user-agent "Mozilla/4.73 [en] (X11; U; Linux 2.2.15 i686)" test.rebex.net

changing the user agent to Mozillia in Linux 2.2.15 with curl command

6. Check Server Response Time

With some clever use of the options -w, -s, and -o, you can use the curl command in Linux to check the response time of the server. The syntax is:

Here, the -w is used to write out the value of time_total variable to output screen, -o to save the output to /dev/null file. For example, to check the response time of the site “test.rebex.net” using this command. The highlighted section shows the response time of the server in seconds.

curl -w "%\n" -o /dev/null test.rebex.net

Источник

Оцените статью
Adblock
detector