Curl Command: A Practical Guide to Curl in Linux with Examples

Curl is a powerful utility that can be used to both download and upload data to servers. The curl command in Linux supports many kinds of protocols, including HTTP/HTTPS, FTP, SMTP, SMB, and more.

The curl command is now also available by default in Windows 10 and later.

This tutorial mainly focuses on HTTP and FTP protocols. We'll begin by exploring how to use the curl command to retrieve data through the HTTP Protocol. After that, we'll demonstrate how to use curl with the FTP protocol for both downloading and uploading files.

Retrieve Web Pages with Curl Command

Let's execute a simple curl command. The following example will retrieve the example.com webpage and display the HTML output in the Linux terminal.

curl https://example.com

The HTML content of example.com will be displayed in the Linux terminal. The output consists of HTML tags and does not represent the rendered webpage as seen in a web browser.

Curl Command example

The curl command in Linux displays data to the terminal by default. To save the retrieved data to a file, you can use either the -o flag (lowercase o) or the -O flag (uppercase O).

In the following example, we use the -o option to specify the output filename:

curl -o example.html https://example.com

This time, the output will not be displayed in the Linux terminal. Instead, the curl command will retrieve the example.com webpage and save it to the example.html file.

Save web page to computer
Save web page to computer

When using the -O (uppercase O) flag, you don't need to specify an output filename. Curl will automatically create an output file with the same name as the remote file. However, the -O option requires a specific remote filename (for example, index.html or index.php).

curl -O https://example.com/index.html

In this instance, the curl command will retrieve the "index.html" file from example.com and save it as "index.html".

It is important to note that you can place command options either before or after the URL, as both approaches yield the same result.

You can also save the data to a file using output redirection. Here's an example:

curl https://example.com > example.html

Display HTTP Response Headers

The curl command in Linux can display HTTP response headers sent by the web server. To view response headers, use the -I option.

curl -I https://example.com

The above command will display the HTTP headers, as shown in the following screenshot.

curl Response Headers
HTTP Response Headers

Setting User Agent with Curl

Web servers and firewalls sometimes block the default curl user agent. One potential solution is to set a different user agent using the --user-agent flag.

In this example, curl will employ the Fedora-Firefox user agent to fetch data from the website:

curl --user-agent 'Mozilla/5.0 (X11; Fedora; Linux x86_64; rv:49.0) Gecko/20100101 Firefox/49.0' https://example.com/

Downloading Files: Practical Examples Using curl Command

The following example will retrieve the 'latest.zip' file from the wordpress.org website and retain the original filename:

curl -O https://wordpress.org/latest.zip

In the following example, the curl command in Linux will download the 'latest.zip' file from wordpress.org and save it as 'wordpress.zip':

curl -o wordpress.zip https://wordpress.org/latest.zip

Download an image using the curl command:

curl -O https://example.com/image1.png

Request the HTTP server to provide compressed versions of the data:

curl --compressed https://example.com/

Downloading and Uploading to FTP Servers

Now, let's explore how to use the curl command with the FTP protocol. For the examples, I will use the following FTP credentials:

FTP Server : 192.168.1.10
FTP Username : ftpuser
FTP Password : ftp123

Specify your username and password using the -u option:

-u username:password

Downloading Files from FTP Servers

The following command will download the file 'file1.txt' from the FTP server. Using the -O option (uppercase O), the original filename will be preserved:

curl -u ftpuser:ftp123 ftp://192.168.1.10/file1.txt -O

In this example, we will download the file 'file1.txt' from the FTP server at 192.168.1.10 and save it as 'file2.txt' using the -o flag (lowercase o):

curl -u ftpuser:ftp123 ftp://192.168.1.10/file1.txt -o 'file1.txt'

Uploading Files to the FTP Server

To upload files, the curl command in Linux utilizes the -T option followed by the name of the file you want to upload:

curl -u ftpuser:ftp123 -T file1.txt ftp://192.168.1.10

The following command will transfer the file 'file1.txt' to the FTP server and store it as 'file2.txt':

curl -u ftpuser:ftp123 -T file1.txt ftp://192.168.1.10/file2.txt

To upload multiple files, enclose the filenames within curly braces {}. Ensure the curly braces are placed within the quotation marks, and there should be no spaces between the filenames:

curl -u curlftp:ftp123 -T '{file1.txt,file2.txt}' ftp://192.168.1.10/

Tips and Tricks for Using the curl Command

Display Command Help:

curl --help

To obtain more detailed information, run the command with the -v or --verbose option:

curl -v https://example.com

Silent Mode: In contrast to verbose mode, this option suppresses error messages and download progress indicators:

curl -s https://example.com -o output.html

Silent Mode with error messages enabled:

curl -s --show-error https://example.com

Display Progress Bar with # Tags:

curl -# https://example.com -o output.html

The --max-filesize flag allows you to specify the maximum size (in bytes) of a file to download. If the file exceeds the specified maximum size, the curl command in Linux will not download it:

curl --max-filesize 1000 -O https://wordpress.org/latest.zip

Maximum Data Transfer Rate: The --limit-rate option sets the maximum download or upload rate. By default, the speed is measured in bytes per second unless a suffix is appended (k for kilobytes, m for megabytes, g for gigabytes).

The following example will utilize a maximum speed of 64 kilobytes per second:

curl --limit-rate 64k http://example.com

The --range flag allows you to specify the byte range you want to transfer. For instance, the following curl command will download the first 100 bytes from the index.html file.

curl --range 0-99 http://example.com/index.html