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 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 of Curl Command

Following is the basic syntax for curl command:

curl [options] [URL...]

Where,
options: Options used for curl command
URL: The targeted URL

Curl Command Examples

1. Basic usage of the curl command

When we use curl 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 www.example.com page located here.

2. Send POST request with the Curl command

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 file with the original name and -o (lowercase) option saves file with given name.

Following command uses -O (uppercase) option to download file with the original name which will save 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 using Curl command

To resume uncompleted download by using 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. Follow the redirect using Curl command

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 -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. Download multiple files using the curl command

You can use the command like below to download multiple files using curl command:

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 using Curl command.

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

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 queries only IP address.

9. Send cookies using Curl command

You can send cookies with curl using -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 using Curl command

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 Using Curl Command

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 curl command -u option along with -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 curl command -u option along with -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

You may be blocked from accessing or downloading content when you use curl agent. So 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 with curl command. 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 curl command. With -z option you have to specify the date.

In the following example, the curl command will download the file if it is 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 curl command.

In the following example, the curl command will download the file if it is updated after 28 Feb 2019.

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

17. Check if the website is Down using the curl command

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 above command, if you get output like HTTP/2 200 then 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