Config Server Firewall

Learn How to use Curl Command in Linux with Examples

Curl is a powerful utility that can use 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.

This tutorial Mainly Focuses on HTTP and FTP Protocol. First we will see how to use curl command to retrieve data through the HTTP Protocol, Then we will use curl with FTP protocol to Download and Upload files.

Retrieve Web Pages with curl command

Let's just run a simple curl command. Following example will retrieve example.com webpage and print html output to the Linux terminal.

curl http://example.com

The data of the example.com will be displayed to the Linux terminal. The output includes HTML Tags, it is not the result you see on the web browser.

Save Data to a File. curl command in Linux displays data to the terminal by default. To save data to a file, we need to use either -o flag (Lowercase 0) or -O flag (Uppercase o).

In the following example, we use -o option (lowercase 0). With -o option we specify the output file name.

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

This time output will not be sent to the Linux terminal. curl will retrieve the example.com and save it to the example.html file.

With The -O (Uppercase O) flag, we do not specify output file name. It will automatically create an output file with the same name as the remote file. But, The -O option require specific remote file name (For example, index.html or index.php).

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

This time curl command will download the "index.html" file from the example.com and save it as "index.html".

Also note that you can put command options before or after the URL, it Makes No difference.

It is also possible to save the data to a file using output redirection as shown below.

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

Display HTTP Response Headers

The curl command in Linux can display HTTP response-headers sent by the Web Server. To show response-headers, use the -I option.

curl -I http://example.com

This will dump the HTTP Headers as shown in following screenshot.

curl command in linux display HTTP response-headers

Set User Agent in Curl Command

Sometimes Web Server and Firewall blocks default curl user agent. One possible solution for this is to set different user agent with --user-agent Flag.

In the following example, curl will use Fedora-Firefox user agent to retrieve data from the website.

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

Examples, Download Files with curl command

Following example will download the 'latest.zip' file from the wordpress.org website and preserve the original file name:

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

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

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

Download images and save using output redirection:

curl http://example.com/image1.jpg > /home/user/image1.jpg

Ask HTTP Server to provide compressed versions of the Data.

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

Connect to FTP Server with curl Command

Now let's see how to use Linux curl command with FTP protocol. For the examples I will use following FTP Credentials.

To specify username and password we use the -u option.

curl -u username:password

Download Files From FTP Server.

Following command will download 'file1.txt' From the FTP Server. We are going to use -O option (Uppercase o), So the original filename will be preserved.

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

In the following example, we will download the 'file1.txt' From the FTP Server 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'

Upload Files to the FTP Server

To upload files, curl command in Linux use the -T option followed by the name of the file wants to upload.

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

Upload with a different filename. Following command will take 'file1.txt' and upload to the FTP Server as 'file2.txt'.

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

Upload Multiple Files. To upload multiple files, put file names inside the curly brackets.

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

Linux Curl Command Tips and Tricks

Display Command Help:

curl --help

Run curl command in Verbose Mode. Run the command with the -v / --verbose option to get more information:

curl -v http://example.com

Silent Mode. This opposite to verbose mode, Command will not show any error message or download progress.

curl -s http://example.com -o output

Silent Mode with error messages enabled:

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

Display Progress bar With # tags:

curl -# http://example.com -o output_file

Max File Size. the --max-filesize flag use to specify the maximum size (in bytes) of a file to download. If file is larger than the max size, curl command in Linux will not download the file.

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

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

Following example will use maximum speed of 64 kilobytes per second.

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

Byte Range. --range flag use specify the byte range we want to transfer. For example, following curl command will download first 100 bytes from the index.html file.

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