The Wayback Machine - https://web.archive.org/web/20240812150423/https://www.geeksforgeeks.org/curl-command-in-linux-with-examples/
Open In App

curl Command in Linux with Examples

Last Updated : 11 Apr, 2024
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

In the realm of Linux, mastering the command line is essential for efficient and powerful usage of the operating system. Among the number of command line tools available, `curl` stands out as one of the most versatile and powerful utilities. Originally developed by Daniel Stenberg, `curl` is a command-line tool and library for transferring data with URLs. It supports a wide range of protocols, making it an invaluable tool for fetching, uploading, and managing data over the Internet. In this comprehensive guide, we delve into the intricacies of the `curl` command in Linux, exploring its features, options, and various use cases.

Understanding the Basics

At its core, `curl` is designed to transfer data using various protocols such as HTTP, HTTPS, FTP, SCP, SFTP, and more. Its syntax is straightforward:

curl [options] [URL]

Here,

[options] can be various command-line flags that modify the behavior of curl

[URL] specifies the location from which to fetch or send data.

Fetching Data Using curl Command

One of the most common use cases of `curl` is fetching data from a URL. This could be a simple HTML page, a file, or any resource accessible via a URL. To fetch a web page using `curl`, you simply provide the URL as an argument:

curl https://example.com

This command will retrieve the HTML content of the specified URL and display it in the terminal.  

curl https://www.geeksforgeeks.org

This should display the content of the URL on the terminal. The URL syntax is protocol dependent and multiple URLs can be written as sets like: 

curl http://site.{one, two, three}.com

URLs with numeric sequence series can be written as: 

curl ftp://ftp.example.com/file[1-20].jpeg

Progress Meter: curl displays a progress meter during use to indicate the transfer rate, amount of data transferred, time left, etc. 

curl -# -O ftp://ftp.example.com/file.zip
curl --silent ftp://ftp.example.com/file.zip

If you like a progress bar instead of a meter, you can use the -# option as in the example above, or –silent if you want to disable it completely. 

Example:

Handling HTTP Requests Using curl Command

The `curl` allows you to send custom HTTP requests with various methods such as GET, POST, PUT, DELETE, etc. For instance, to send a GET request:

curl -X GET https://api.example.com/resource

Similarly, to send a POST request with data:

curl -X POST -d "key1=value1&key2=value2" https://api.example.com/resource

In this example, the `-d` flag is used to specify data to be sent with the request.

Downloading Files Using curl Command

curl is also widely used for downloading files from the internet. To download a file, you simply provide the URL of the file as an argument:

-o: saves the downloaded file on the local machine with the name provided in the parameters. 
Syntax:

curl -o [file_name] [URL...]

Example:

curl -o hello.zip ftp://speedtest.tele2.net/1MB.zip

Output:

The above example downloads the file from the FTP server and saves it with the name hello.zip.

-O: This option downloads the file and saves it with the same name as in the URL. 
Syntax:

curl -O [URL...]

Example:

curl -O ftp://speedtest.tele2.net/1MB.zip

Output:

Uploading Files

In addition to downloading, `curl` can also upload files to a server using various protocols. For example, to upload a file via FTP:

curl -T uploadfile.txt ftp://example.com/upload/

In this example, `-T uploadfile.txt` specifies the file to be uploaded (`uploadfile.txt`). The `-T` flag is used to upload a file with FTP. `curl` then sends the specified file to the FTP server located at `ftp://example.com/upload/`. This is useful for automating file uploads or transferring files via FTP from the command line.

Handling Authentication

curl supports various authentication methods including Basic, Digest, and OAuth. You can specify authentication credentials using the `-u` flag:

curl -u username:password https://example.com/api

The `-u` flag is used to specify authentication credentials (`username:password`). In this example, `curl` will include these credentials in the request header when accessing `https://example.com/api`. This is commonly used when accessing protected resources or APIs that require authentication.

Examples of Curl Command

-C – Option:

This option resumes download which has been stopped due to some reason. This is useful when downloading large files and was interrupted. 
Syntax:

curl -C - [URL...]

Example:

curl -C - -O ftp://speedtest.tele2.net/1MB.zip

Output:

–limit-rate Option:

This option limits the upper bound of the rate of data transfer and keeps it around the given value in bytes. 
Syntax:

curl --limit-rate [value] [URL]

Example:

curl --limit-rate 1000K -O ftp://speedtest.tele2.net/1MB.zip

Output:

The command limits the download to 1000K bytes.

-u Option:

curl also provides options to download files from user authenticated FTP servers. 

Syntax:

curl -u {username}:{password} [FTP_URL]

Example:

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

Output: 

-T Option:

This option helps to upload a file to the FTP server. 

Syntax:

curl -u {username}:{password} -T {filename} {FTP_Location}

If you want to append an already existing FTP file you can use the -a or –append option.

–libcurl Option:

This option is very useful from a developer’s perspective. If this option is appended to any cURL command, it outputs the C source code that uses libcurl for the specified option. It is a code similar to the command line implementation. 

Syntax: 

curl [URL...] --libcurl [filename]

Example:

curl https://www.geeksforgeeks.org > log.html --libcurl code.c

Output:

The above example downloads the HTML and saves it into log.html and the code in code.c file. The next command shows the first 30 lines of the code.

Sending mail:

As curl can transfer data over different protocols, including SMTP, we can use curl to send mails. 

Syntax: 

curl –url [SMTP URL] –mail-from [sender_mail] –mail-rcpt [receiver_mail] -n –ssl-reqd -u {email}:{password} -T [Mail text file] 

DICT protocol:

The Libcurl defines the DICT protocol which can be used to easily get the definition or meaning of any word directly from the command line. 

Syntax: 

curl [protocol:[dictionary_URL]:[word]

Example:

curl dict://dict.org/d:overclock

Output:

Note: There are a number of other options provided by cURL which can be checked on the main page. The libcurl library has been ported into various programming languages. It’s advisable to visit the individual project site for documentation.

Conclusion

In conclusion, mastering the command line in Linux is pivotal for maximizing efficiency and effectiveness in navigating the operating system, with `curl` emerging as a standout tool due to its versatility and robust capabilities for data transfer across various protocols. Developed by Daniel Stenberg, `curl` facilitates seamless fetching, uploading, and management of data over the Internet. This guide has offered an in-depth exploration of curl‘s features, options, and diverse applications, illuminating its indispensability for Linux users seeking optimal command line functionality.



Previous Article
Next Article

Similar Reads

Ccat – Colorize Cat Command Output command in Linux with Examples
ccat is a command-line tool for Linux and OSX, which is similar to the cat command in Linux. But the difference between cat and ccat is that the ccat shows the content of the file with the syntax highlighted. Currently, ccat supports the following programming languages. JavaScriptJavaRubyPythonGoCJSONInstallation of Ccat First, we are going to see
2 min read
How to Display Command History in Linux | history Command
The command-line interface in Linux provides powerful tools for users, and mastering command history is essential for efficient navigation and retrieval of previously executed commands. The history command is a valuable utility that allows users to view and search through their command history. In this comprehensive guide, we will explore the vario
4 min read
Difference Between wget VS curl
wget and curl are the commands that are used to HTTP requests without any GUI or software, rather we use the Terminal in Linux that provides the respective output or message. The commands are very useful for web crawling, web scraping, testing RESTful APIs, etc. Curl Curl is a free and open-source command-line utility tool that allows the users as
3 min read
Using curl to send email
In this article, we shall see how to harness the power of the "curl" command line tool and Gmail SMTP server in order to send an email programmatically using a bash script, "curl" is a versatile and powerful utility command line tool for making HTTP requests. And is primarily known for transferring data from and to servers using various kinds of pr
3 min read
How to Hide the CURL Progress Bar
CURL or client for URL is a popular command line tool for sending and obtaining data and files using the URL. CURL can easily be installed on multiple operating systems. It is also trendy and is used by lots of people around the world. But sometimes cURL's progress bar can be annoying or might take precious screen real-estate, this can be resolved
3 min read
CURL to Access Page that Required Login from Different Page
In this article, I have illustrated how you can utilize curl, a versatile command-line tool, to mimic the login process and access confidential web pages. I have also shown you how to perform the necessary steps to send POST requests using the correct credentials, no doubt, how to deal with authorization cookies and enable you to send GET requests
5 min read
Using Curl to make REST API requests
REST APIs are essential for modern web applications, enabling programmatic interaction with data and functionality. Curl is a command-line tool for making web requests, often used directly from the terminal. For example, curl -L ip.ba3a.tech fetches IP address details in JSON format, just like visiting the site in a browser. Essentially, Curl print
5 min read
select command in Linux with examples
select command in Linux is used to create a numbered menu from which a user can select an option. If the user enters a valid option then it executes the set of command written in select block and then ask again to enter a number, if a wrong option is entered it does nothing. If user enters nothing and simply press 'enter' the option menu gets print
1 min read
atrm command in Linux with examples
atrm command is used to remove the specified jobs. To remove a job, its job number is passed in the command. A user can only delete jobs that belong to him. Only superuser can delete any job even if that belongs to another user. Syntax: atrm [-V] job [job...] Options: -V : Used to print the version number atrm -V job : Job number of the job which i
1 min read
comm command in Linux with examples
comm compare two sorted files line by line and write to standard output; the lines that are common and the lines that are unique. Suppose you have two lists of people and you are asked to find out the names available in one and not in the other, or even those common to both. comm is the command that will help you to achieve this. It requires two so
5 min read
three90RightbarBannerImg