What is Curl Command and How to Use it in Linux

Try this guide with our instant dedicated server for as low as 40 Euros

Linux curl Command

Did you know a typical Linux installation can have about 1000 commands and utilities?

However, sysadmins use several commands to manage network data transfers and similar operations. Out of these commands, curl is perhaps the most popular command for managing data transfer over multiple protocols.

In this comprehensive guide to curl command, we’ll go into the details of using the command, common usage scenarios, and the options and flags you can use to tap into the versatility of this utility.

Let’s start with the introductions.

Table Of Contents

  1. What is the curl Command in Linux?
  2. How to Use the curl Command in Linux?
  3. Use curl With Different Protocols
  4. How to Use Advanced curl Commands in Linux?
  5. Further Examples of Using the curl Command in Linux
  6. Additional Tips and Tricks For Using the curl Command in Linux
  7. curl Supported Protocols
  8. curl Command Options
  9. Conclusion
  10. FAQs

What is the curl Command in Linux?

The curl command is a powerful utility that allows you to transfer data to or from a server using various protocols, such as HTTP and FTP. This command-line tool is generally available in all mainstream distributions. It is an essential part of installing packages where the process uses it to fetch data from the official repository or website.

The curl command is powered by the libcurl library and offers a simple way to interact with URLs. In fact, curl stands for Client for URL, indicating that it provides a seamless way of interacting and managing data transfers for a wide range of protocols, such as HTTPS, email protocols, tunneling, cookies, and SSH/SFTP. curl comes with a wide range of options and parameters that facilitate its use in different use cases.

The Basic Syntax of the curl Command

The basic syntax of the curl command is:

curl [options] [URL]

Here, [options] refer to the various options and parameters you can use with curl, and [URL] represents the specific URL you want to interact with.

How to Use the curl Command in Linux?

Now that you have the background information let’s look into the technical aspects of using the command. We’ll start with the Prerequisites of using the command.

The Prerequisites

Before you can dive into using curl, it’s essential to set up the right environment. For this, you’ll need:

  1. A Linux Operating System: You can use curl on all popular OS. However, we’ll use Linux in this tutorial.
  2. Terminal Access: You need access to a command-line interface or terminal to execute the curl commands.
  3. curl installed and ready: Not every Linux distro comes preloaded with curl. If that’s the case with your distro, install curl via package managers like apt for Debian-based distros or yum for RedHat-based distros:

sudo apt install curl # For Debian-based systems

sudo yum install curl # For RedHat-based systems

Now that you have curl installed on your Linux system let’s explore some common use cases and examples of using the command. Let’s see three very common scenarios where curl benefits users.

Also Read: 6 Simple Examples of Using the Linux watch Command

Use curl to Transfer Files and Content

One of the most common use cases of the curl command is to transfer files and content from a server to your local machine. You can use the -o option followed by the file name to specify the output file. For example, to download a file from a server and save it on the local system with the name example.txt, use the following command:

# curl -o example.txt [URL of the file on the server]

You can use this command to download the contents from a web page or an API endpoint (or any resource available at a particular URL) and save it to the file named example.txt.

Let’s see this command in action:

Consider the scenario where we wish to fetch data from the official RedSwitched website. For this demonstration, we’ll fetch the contents of the Instant Dedicated Server page.

Use the following command to save the content of the page to a local file named example.txt:

# curl -o example.txt https://www.redswitches.com/instant-dedicated-server/

Use curl to Transfer Files and Content

Here’s what happens during the execution of this command:

  1. curl makes a request to https://www.redswitches.com/instant-dedicated-server/
  2. The content from the URL will be saved to a local file named example.txt. If you don’t specify a directory, the system will create it in your current directory.

If the operation is successful, you won’t see any success message or output in the terminal. Instead, you can check the content of example.txt to verify that the data has been saved correctly.

Let’s look at another example:

Suppose you want to download the latest version of the Linux kernel from kernel.org. The URL for the file is https://www.kernel.org/pub/linux/kernel/v5.x/linux-5.10.tar.xz

Use the following command to download this file using curl and save it as linux-kernel-5.10.tar.xz on your local machine:

# curl -o linux-kernel-5.10.tar.xz https://www.kernel.org/pub/linux/kernel/v5.x/linux-5.10.tar.xz

curl --o

Once the command finishes execution, you should have the file linux-kernel-5.10.tar.xz in your current directory.

Bonus Tip:

If you want to save the file with the same name as the remote file without specifying it manually, you can use the –O (capital O) option:

# curl -O https://www.kernel.org/pub/linux/kernel/v5.x/linux-5.10.tar.xz

In this case, curl will save the file as In this case, curl will save the file as linux-5.10.tar.xz.

Also Read: How to Set or Change User Agent with cURL: A Step-by-Step Guide

Use curl With Different Protocols

curl supports various protocols, including HTTP/HTTPS and FTP/SFTP.

Send FTP Requests With curl

Let’s run a scenario where you want to download a file from an FTP server.

The file (named datafile.txt) is located in the root directory of the FTP server (named ftp.redswitches.com). We will use the curl command to save this file to the local system as example.txt.

In this scenario, the curl command will be structured as follows:

# curl -o example.txt ftp://ftp.redswitches.com/datafile.txt

In this example command:

  • -o example.txt indicates that the original datafile.txt should be saved to a local file named example.txt.
  • ftp://ftp.redswitches.com/datafile.txt is the URL of the file you wish to download.

In most practical cases, you need to supply user authentication credentials in the form of a username and password. In this case, you need to modify the command to include the credentials:

# curl -o example.txt ftp://username:[email protected]/datafile.txt

Important: Adding credentials to the CLI commands can pose a serious security risk.. Fortunately, curl provides safer ways of adding authentication to the commands.

Send HTTP Requests With curl

You can use curl to send HTTP requests and retrieve the responses. We’ll now show you how to send the GET, PUT, POST, and DELETE requests and carry out the tasks you’d do using the HTTP requests.

How to Send a GET Request

Syntax: curl [URL]

Use the following command to fetch the homepage of example.com:

# curl https://www.example.com

How to Send a POST Request

Syntax: curl -X POST -d [data] [URL]

where -X POST specifies the HTTP method, and -d is used to send data.

Use the following command to send a POST request containing a username and password to example.com/login:

# curl -X POST -d "username=john&password=secret" https://www.example.com/login

How to Send a PUT Request

Syntax: curl -X PUT -d [data] [URL]

where -X PUT specifies the HTTP method, and -d is used to send data.

Use the following command to PUT the email address of the user 123 to example.com/api/users/123

# curl -X PUT -d "[email protected]" https://www.example.com/api/users/123

How to Send a DELETE Request

Syntax: curl -X DELETE [URL]

where -X DELETE specifies the HTTP method.

Use the following command to delete the user 123 on example.com/api/users:

# curl -X DELETE https://www.example.com/api/users/123

How to Send Data as JSON

Syntax: curl -X POST -H "Content-Type: application/json" -d '[JSON data]' [URL]

where -X POST specifies the HTTP method and -H allows you to set headers.

Use the following command to send a POST request with JSON data (user name and age) to example.com/api/users:

# curl -X POST -H "Content-Type: application/json" -d '{"name": "John", "age": 30}' https://www.example.com/api/users

How to Add Headers to an HTTP Request

Syntax: curl -H "[header-name]: [header-value]" [URL]

Where -H allows you to set headers.

Use the following command to send a GET request to example.com with a header containing a custom user-agent:

# curl -H "User-Agent: MyCustomAgent/1.0" https://www.example.com

We recommend you check out the official documentation with the man curl command to see the list of flags to fine-tune the use of the curl

where -X DELETE specifies# curl -X POST -H “Content-Type: application/json” -d ‘{“name”: “John”, “age”: 30}’ https://www.example.com/api/users the HTTP method.

command in fetching files and sending HTTP commands to remote servers.

Users often see the HTTPS NOT SECURE warning in Chrome. This warning can deter users from visiting a website. If that’s the case with your website, check out our guide to fixing the HTTPS NOT SECURE warning in Chrome.

How to Use Advanced curl Commands in Linux?

Now that you are familiar with the basic usage of curl, let’s dive into some advanced features and options that demonstrate the versatility of curl commands.

Setting Headers in the curl Command

You can set custom headers in curl commands with the -H option. This is a useful functionality when you use the command to interact with APIs and services that require custom headers.

For instance, here is the command that uses a custom Authorization header when interacting with an API:

# curl -H "Authorization: Bearer TOKEN" [API URL]

Redirect Output From the curl Command

You can redirect the output of a curl command to a file or another command using the > operator. This is useful for saving the response or processing it further as input for another command. For instance, use the following command to save the response to a file named output.txt:

# curl [URL] > output.txt

The pipe command is another useful way to send the output of a command as the input for another command. Read our guide to using the pipe command to learn more about using this versatile utility in command-line and scripts.

Handling Authentication with the curl Command

Curl provides options to handle various types of authentication mechanisms, such as Basic Authentication and OAuth. You can use the –u option followed by the username and password to specify credentials. The syntax of the command is:

# curl -u username:password [URL]

Further Examples of Using the curl Command in Linux

Let’s explore some practical examples of how to use the curl command in Linux.

where -X DELETE specifies# curl -X POST -H “Content-Type: application/json” -d ‘{“name”: “John”, “age”: 30}’ https://www.example.com/api/users the HTTP method.

Download Multiple Files with curl

You can use curl to download multiple files at once by specifying multiple URLs separated by spaces. Here’s the sample syntax:

curl -o [output_file1] [URL1] -o [output_file2] [URL2]

Let’s run a scenario where we’ll download the RedSwitches website’s homepage (https://www.redswitches.com) and a blog titled How to Quit Vim Editor and Save Your Work (https://www.redswitches.com/blog/how-to-vim-save-quit-exit/).
The contents of these pages will be saved in local files titled redswitches-homepage.txt ( for the homepage) and linux-blog.txt (for the blogpost).

Here’s the curl command to accomplish this:

# curl -o redswitches-homepage.txt https://www.redswitches.com -o linux-blog.txt https://www.redswitches.com/blog/operating-systems/linux/

curl -o hompage. txt

Important: Always make sure you have permission to download and distribute content from websites.

where –X DELETE specifies# curl -X POST -H “Content-Type: application/json” -d ‘{“name”: “John”, “age”: 30}’ https://www.example.com/api/users the HTTP method.

Check the curl Version in Linux

Checking the curl version on your Linux system is an essential task for using the command. You can use the –version option to check the version of curl installed on your Linux system. This will display the version information along with other details.

# curl --version

curl --version

Use curl to Transfer Data Securely With SSL

Curl supports secure data transfer using SSL/TLS. You can use the –ssl option, followed by the desired protocol, to transfer data securely.

A sample command will be:

# curl --ssl https://example.com

Additional Tips and Tricks For Using the curl Command in Linux

The curl command is incredibly robust and is often used to execute some amazing ideas. Let’s see some examples of this robustness:

where -X DELETE specifies# curl -X POST -H “Content-Type: application/json” -d ‘{“name”: “John”, “age”: 30}’ https://www.example.com/api/users the HTTP method.

Use the User Agent Option in curl

You can specify a custom user agent for your requests using the –A option. This can be useful when interacting with websites that require a specific user agent to access their content. Check out the following command:

# curl -A "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3" [URL]

Save the Output of a curl Command to a File

In addition to redirecting the output to a file, you can also use the -o option followed by a file name to save the output directly to a file.

# curl -o output.txt [URL]

Working with Proxies in curl Commands

You can use curl commands when you work with proxies for your requests. You can use the -x option followed by the proxy server URL to specify a proxy.

# curl -x http://proxy.example.com:8080 [URL]

curl Supported Protocols

One of the standout features of curl is its protocol versatility. It’s not just for HTTP; it speaks several other “languages” fluently:

      1. HTTP & HTTPS: Web browsing’s backbone.
      2. FTP & FTPS: Traditional methods for file transfers, with FTPS offering security enhancements.
      3. SCP & SFTP: Secure alternatives leveraging SSH for file transfers.
      4. LDAP & LDAPS: Access and management of directory services.
      5. IMAP, IMAPS, POP3, POP3S: Protocols for email management.
      6. SMTP & SMTPS: Simple protocols for sending emails.
      7. TELNET: A vintage protocol for remote terminal connections.
      8. DICT: A protocol used for dictionary queries, allowing clients to fetch definitions from a dictionary server.

curl Supported Protocols

curl Command Options

The diversity and flexibility of the curl command come from the long list of options and flags that allow users to fine-tune their requests and handle different tasks.

Here are some of the most commonly used curl command options:

General Options

-V, –version: Display the curl version information.

–help: Lists all the available curl options with brief descriptions.

curl Command Options

Connection Options

–connect-timeout <seconds>: Sets the maximum timeout in seconds for server connection.

-k, –insecure: Allows curl to proceed for insecure server connections (without certificates)

connection options

File & Data Transfer Options

-o, –output <filename>: Writes the output to a file instead of standard output.

-O, –remote-name: Downloads and saves the file with the same name as the remote file.

-I, –head: Fetches only the HTTP headers (useful to view meta-info about resources).

C, –continue-at <offset>: Continues a previous file transfer from a specified offset.

-T, –upload-file <file>: Uploads a file to the provided URL.

File & Data Transfer Options

Protocol-specific Options

-X, –request <command>: Specifies a custom request method when communicating with an HTTP server (e.g., GET, POST).

–data <data>: Sends specified data in a POST request to the server.

-form <name=content>: For form-based uploads (HTTP POST).

-u, –user <user:password>: Provides authentication credentials for the server connection.

protocal and specific options

Information & Debugging

-v, –verbose: Enables the verbose mode with more information about the connection and request.

-s, –silent: This silent mode doesn’t show progress status or error messages.

–trace <file>: Writes a debug trace to the specified file.

information and dubugging

SSL Options

-cert <cert[:passwd]>: Specifies the client certificate file for SSL.

-cacert <file>: Provides a file with one or multiple certificates for peer verification.

ssl opitons

HTTP Interaction

–cookie <name=data>: Sends the specified cookie to the server.

–cookie-jar <filename>: Saves HTTP cookies to a file after the request.

–referer <URL>: Sends the ‘Referer Page’ information to the server.

–user-agent <string>: Sends the ‘User-Agent’ string to the server. It can be used to mimic a browser.

http interction

FTP Options

–ftp-alternative-to-user <string>: Replaces the default USER [name] command with the one specified in the command.

–ftp-create-dirs: Creates the remote dirs if they do not exist.

–ftp-method [multicwd/singlecwd/nocwd]: Determines how to reach the requested file/directory on the FTP server.

–ftp-pasv: Requests the server to enter the passive mode. Useful for connections behind firewalls.

ftp optons

Authentication & Identities

–basic: Uses HTTP Basic Authentication.

–digest: Uses HTTP Digest Authentication.

–negotiate: Uses HTTP Negotiate (SPNEGO) Authentication.

–ntlm: Uses HTTP NTLM authentication.

-pubkey <key>: Public key file name (for SSH).

authentifications and identites

Time & Size Conditions

–time-cond <date expression>: Makes the request conditional based on the file’s timestamp: Makes the request conditional based on the file’s timestamp.

–max-filesize <bytes>: Doesn’t transfer data larger than the specified size in bytes.

time and size condtions

Proxy and Tunneling

–proxy <[protocol://][user:password@]proxyhost[:port]>: Uses the specified proxy and credentials.

–proxy-basic: Uses Basic Authentication on the proxy.

–proxy-digest: Uses Digest Authentication on the proxy.

–proxy-negotiate: Uses Negotiate Authentication on the proxy.

proxy and tunneling

Advanced Options

–limit-rate <rate>: Limits the transfer speed to the specified rate.

–local-port <num/range>: Forces curl to bind to a specific local port number or range.

–max-redirs <num>: Sets the maximum number of redirects to follow.

–dns-servers <addresses>: Uses the specified DNS servers to resolve host names.

advance opitons

Other Data Options

–data-raw <data>: Sends data in a POST request without URL-encoding it.

–data-urlencode <data>: URL-encodes the provided data and sends it in a POST request.

–post301: Makes curl continue the post action even after receiving a 301 redirection.

other date options

Miscellaneous Options

-L, –location: Follows HTTP 3xx redirects.

–compressed: Asks for the response to be compressed.

-H, –header <header>: Sends custom headers to the server.

miscellnous options

Also Read: A Guide on How to Install and Use Linux Screen Command in 2024

Conclusion

This short introduction to the curl command in Linux only scratches the surface. It’s a good practice to consult the official documentation or run man curl in the terminal for a comprehensive understanding and to explore further applications.

The power and flexibility of curl make it an unmatched tool in the Linux toolset. Whether it’s a simple GET request or a complex series of interactions over various protocols, curl is a powerful option that brings a lot of flexibility to the game.

Just as curl is foundational for network interactions, choosing the right hosting platform is crucial for the performance and reliability of your projects.

For those seeking top-tier hosting solutions, consider exploring RedSwitches Bare Metal Hosting Provider. These solutions are an excellent choice for businesses and professionals looking for uninterrupted server uptime and secure platforms for your projects.

So, If you’re looking for a robust server for your projects, we offer the best dedicated server pricing and deliver instant dedicated servers, usually on the same day the order gets approved. Whether you need a dedicated server, a traffic-friendly 10Gbps dedicated server, or a powerful bare metal server, we are your trusted hosting partner.

FAQs

Q. What is curl command in Linux?

A The curl command is a command line tool used for making HTTP requests in Linux. It is used to transfer data to or from a server via protocols like HTTP, HTTPS, FTP, etc.

Q. What are some common uses of the curl command?

Some common uses of the curl command include making HTTP requests, downloading files, testing APIs, and transferring data between servers.

Q. How can I make a basic HTTP request using curl?

You can make a basic HTTP GET request using curl with the following command:

# curl [URL]. For example, curl https://example.com

Q. How can I make a POST request using curl?

You can use the -X flag and specify the HTTP method as POST in a curl command. You can also pass data to the server using the -d flag.

# curl -X POST -d "name=John" [URL]

Q: How can I view the response headers using curl?

A: You can view the response headers using the -I flag with curl.

# curl -I [URL]

Q. How can I follow redirects using curl?

By default, curl does not follow redirects. To enable redirect following, you can use the -L flag:

# curl -L [URL]

Q. What is libcurl?

Libcurl is a library that brings the functionality of the curl command line tool to programming languages. It allows developers to make HTTP requests and handle data transfer in their applications.

Q. How do I use a basic curl command?

The basic curl command consists of the command followed by the URL of the server you want to send the request to.

# curl https://example.com

Q. How can I send data to a server using curl?

You can send data to a server using the -d or –data option followed by the data you want to send. Here’s a sample command:

# curl -d "username=johndoe&password=12345" https://example.com/login

Try this guide with our instant dedicated server for as low as 40 Euros