curl Command in Linux with Examples
curl is a command-line utility for transferring data to or from a server, employing a range of internet protocols such as HTTP, HTTPS, FTP, SCP, and SFTP.
Whether you want to download a file, test a REST API, or simply verify that a website is up and running, curl is your best friend. It is accessed directly from the terminal — no need to fire up a browser or install some slick app.
Syntax of curl Command
curl [options] [URL]
Here,
[
options
]:
Can be various command-line flags that modify the behavior ofcurl
[
URL
]:
Specifies the location from which to fetch or send data.
Why is curl So Popular in Linux?
- It's installed by default on the majority of Linux distros such as Ubuntu, Debian, CentOS, etc.
- It handles an enormous set of protocols such as HTTP, HTTPS, FTP, and many more.
- You can utilize it to download files, upload files, make API calls, and even debug web applications.
- It's scriptable – excellent for automation and cron jobs.
- Lightweight and quick — launches in seconds.
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` command allows you to send custom HTTP requests with various methods such as GET, POST, PUT, DELETE, etc. For example, to send a GET request:
curl -X GET https://api.sampleapis.com/coffee/hot

In the same way, to send a POST request with data:
curl -X POST -d "key1=value1&key2=value2" https://api.sampleapis.com/coffee/hot
In this case, the `-d` flag is used to send data to be sent with the request.
Downloading Files Using curl Command
curl is also generally used to download a file from the web. To download a file, you simply provide the URL of the file as the argument:
-o: saves the downloaded file to the local host with the specified name in 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
If you want to upload a file to a server, for example using FTP (File Transfer Protocol), curl
can do that in just one line:
curl -T uploadfile.txt ftp://example.com/upload/
-T uploadfile.txt
: This tells curl which file to upload (in this case, a file calleduploadfile.txt
).ftp://example.com/upload/
: This is the destination FTP URL where the file will be uploaded.
Handling Authentication
Sometimes the API or site you're trying to access is protected with a username and password. In those cases, you can put your credentials in the command itself using the -u flag.
curl -u username:password https://example.com/api
-u username:password
: This sends your login details securely with the request.https://example.com/api
: The protected API or resource you want to access.
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 appended to any curl command, it outputs the C source code that uses libcurl for the specified option.
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:
If we 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:
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
Lastly, understanding the command line in Linux is essential to maximize efficiency and effectiveness in using the system, with `curl` being an outstanding tool due to its flexibility and robust data transfer capabilities across various protocols. Developed by Daniel Stenberg, `curl` provides simple fetching, uploading, and data manipulation through the Internet. This book has given an in-depth elaboration of what curl can do, how it works, and its various applications, highlighting the need for Linux users seeking full command line utility.