17 Curl Command Examples in Linux

17 Curl Command Examples in Linux
17 Curl Command Examples in Linux

Curl Command in Linux

Curl utility in Linux is used to transfer files to or from a server using protocols like HTTP, HTTPS, FTP, SCP, SFTP, etc. This command is easy to use and can work without user interaction. Curl command can also be used to download or upload files with supported options like proxy support, resume the transfer, etc. Alternatively to transfer files we can use wget command.

Install Curl

Most of the Linux systems today come with curl command preinstalled. So to check whether it is installed on your system already or not type curl in the command line. If you get output like curl: try 'curl --help' or 'curl --manual' for more information this the curl is installed on your system otherwise install it using below command.

Install Curl on Ubuntu or Debian

Now install curl on Ubuntu or on Debian based systems run below command:

sudo apt install curl

Install Curl on Fedora or CentOS

Install curl on Fedora or on CentOS run below command:

sudo yum install curl

Basic Syntax

Following is the basic syntax:

curl [options] [URL...]

Where,
options: Options used with the curl command
URL: The targeted URL

Curl Command Examples

1. Basic usage:

When we use this command without any options it will return source code for the given URL.

For example, run below command to retrieve source code for www.example.com:

curl www.example.com

You will get the source code of the www.example.com page located here.

2. Send a POST request

To send a POST request using curl command --data option is used. You can send parameters with it like given below:

curl --data "name=Mona&age=20" https://www.example.com/update_info

3. Download a file using the curl command

To download a file using curl command -O (uppercase) or -o (lowercase) option is used. While -O (uppercase) option saves the file with the original name and -o (lowercase) option saves a file with the given name.

Following command uses -O (uppercase) option to download file with the original name which will save the file with the name example_file.zip.

curl -O http://www.example.com/example_file.zip

Now run below command which downloads example_file.zip and saves it with name new_file.zip

curl -O new_file.zip http://wwww.example.com/example_file.zip

4. Resume uncompleted downloads

To resume uncompleted download by using the curl command -C - option is used with the option -O .

For example, download Ubuntu ISO file using below command and then stop it:

curl -O http://ubuntu.biz.net.id/18.04.2/ubuntu-18.04.2-desktop-amd64.iso

Now you can resume above uncompleted download using below command:

curl -C - -O http://ubuntu.biz.net.id/18.04.2/ubuntu-18.04.2-desktop-amd64.iso

5. How to follow the redirect

Sometimes the non-WWW version is redirected to WWW version of the website. If you visit yahoo.com using curl command you will be notified that redirected means the page is redirected to www.yahoo.com:

curl www.yahoo.com

The output should be:

redirected

At such time you should use the -L option which follows the given URL until it reaches to destination URL.

curl -L yahoo.com

6. Limit maximum transfer rate

By using --limit-rate option you can limit the maximum transfer rate like given below:

curl --limit-rate 300K -O http://ubuntu.biz.net.id/18.04.2/ubuntu-18.04.2-desktop-amd64.iso

7. How to download multiple files using Curl Command

You can use curl like below to download multiple files:

curl -O http://ubuntu.biz.net.id/16.04.0/ubuntu-16.04.0-desktop-amd64.iso -O http://ubuntu.biz.net.id/18.04.2/ubuntu-18.04.2-desktop-amd64.iso

Above command will download Ubuntu 16.04 ISO file and Ubuntu 18.04 ISO file.

8. Check your external IP address.

You can also check your IP address by using curl command. Run below command to check the IP address and other IP address related info:

curl ipinfo.io

or to check any IP address info type:

curl ipinfo.io/157.133.152.70

Alternatively, you can also use the curl ifconfig.mecommand. It query only IP address.

9. Send cookies using Curl command

You can send cookies with curl using the -b option. In the following example, we are downloading Ubuntu ISO file with curl and sending cookie “name:john123” with it.

curl -b "name=john123" -O http://ubuntu.biz.net.id/18.04.2/ubuntu-18.04.2-desktop-amd64.iso

10. How to check HTTP headers

Headers are used to send additional information about itself so to check such headers you can use following curl command:

curl -I http://www.yahoo.com

11. How to Set HTTP Authentication

Whenever you need authentication for accessing the page by using username and password you can do such things using -u option in Curl.

In following command username and password is set for authentication for accessing www.mywebsite.com

curl -u username:password ftp://www.mywebsite.com/my_file.zip

12. Download or Upload files from or to FTP server

You can download files from FTP server using the curl command -u option along with the -O option is used like given below:

curl -u username:password -O ftp://example_server/example_file.tar.gz

You can upload files from FTP server using the curl command -u option along with the -T option is used like given below:

curl -u username:password -T ftp://example_server/example_file.tar.gz

For authentication, you can pass username and password.

13. Set Firefox as a User Agent for Curl

When you blocked from accessing or downloading content when you use a Curl agent. Then to customize the user agent name you can use -A option.

In the below example curl will act as Firefox user-agent:

curl -A "Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Firefox/60.0" https://example.com/

14. Using proxies with Curl Command

You can also use proxies like HTTP, HTTPS, and SOCKS. In curl command to transfer data through a proxy, server -x option is used.

In the following example, we will use 192.168.143.106:8080 as a proxy to download example_file.zip file:

curl -x 192.168.143.106:8080 -O http://www.example.com/example_file.zip

You can also authenticate with the username and password like given below, remember here we have used -U (uppercase) option for authentication:

curl -U username:password -x 192.168.143.106:8080 -O http://www.example.com/example_file.zip

15. Download a file, if updated before the specific time

To download a file which updated before a specific time then you can do by using -z option in the curl command. With the -z option you have to specify the date.

In the following example, the command downloads the file if the file updated before 28 Feb 2019.

curl -z -28-Feb-19 http://www.mywebsite.com/xyz.zip

16. Download a file, if updated after a specific time

To download the file which updated after a specific time then you can do by using -z option.

In the following example, the command downloads the file if the file updated after 28 Feb 2019.

curl -z 28-Feb-19 http://www.mywebsite.com/xyz.zip

17. Check if the website is Down

Sometimes we are not able to access a website at such time to check whether the website is down, you can use -Is option.

To check whether www.facebook.com is available or run below command:

curl -Is https://www.facebook.com -L | grep HTTP/

For the above command, if you get output like HTTP/2 200 then the website is available otherwise if you get output like HTTP/2 503 Service Unavailable then the specified website is down.

Conclusion

You have successfully learned 15 Curl command examples in Linux. If you have any queries please don’t forget to comment below.

1 COMMENT

LEAVE A REPLY

Please enter your comment!
Please enter your name here