What Is Curl Command in Linux 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:

curl <options> <url>

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.

-#Shows a progress bar when downloading a file using curl
-oSaves the downloaded file with a different name specified on the client’s system
-OWhen used, it will save the downloaded file with the same name as the <url>
-TUsed to upload a file to an FTP server
-xAccesses the <url> via a proxy server
-wMake 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 <options>, 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 <options> -o <file_name_to_save> <url>

For example:

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:

curl <options> -I <url>

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:

curl -I https://test.rebex.net/
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 <url>:

curl <options> --cookie-jar <file_to_write_cookies> <url>

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:

curl -u <username>:<password> -O <url>

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:

curl -w "%{time_total}\n" -o /dev/null <url>

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 "%{time_total}\n" -o /dev/null test.rebex.net
checking the response time of a server
comment Comments 1
  • Adam N Outler says:

    Curl is part of the stock Windows 10 image now as well.

Leave a Reply