Learn to use the Curl command in Terminal

In this tutorial, we’ll show you how to use the curl tool with practical examples and detailed explanations of the curl options most popular in everyday use.

Hopefully you will open up the Linux distribution console to follow the guide on this page so you can better understand how this works. Curl In Linux Terminal.

We also recommend that you test any online tutorials or guides on a virtual machine (vmware or virtualbox) before deploying to a production server, so that you don’t mess with your running system when something goes wrong.

You can learn how to install VMware on Ubuntu, CentOS, and VirtualBox on Ubuntu, Fedora and CentOS.

What is curl?

Curl is a command-line utility for moving data to or from servers that is designed to work without user intervention. With curl, you can download or upload data using any of the supported protocols including HTTP, HTTPS, SCP, SFTP and FTP.

Curl provides a number of options that allow you to resume transfers, bandwidth limitation, proxy support, user authentication, and more.

How to install Curl Utility

The curl package is usually installed by default on most Linux distributions today.

To check if the curl package is installed on your system, open your console, and type curl, And hit enter. If you have curl installed, the system will print the output curl: try 'curl --help' or 'curl --manual' for more informationOtherwise, you’ll see something likecurl command not found.

If curl is not installed, you can install it easily using the distro’s package manager.

Install Curl on Ubuntu and Debian

sudo apt install curl

Install Curl on CentOS and Fedora

sudo yum install curl

Basic syntax of Curl

Before discussing how to use the curl command, let’s start by reviewing the basic syntax.

The curl utility expression takes the following form:

curl [options] [URL...]
  • options Curl options start with a strip or two
  • URL – Remote server URL.

How to use curl

In its simplest form when used without any options, curl will return the resource specified in [url] For standard output.

In the next example, we visit the From homepage example.com:

curl example.com

The above command will print the source code from the main page example.com In your device window.

If you don’t specify a protocol, curl will try to guess which protocol to use and will rely on it by default HTTP.

How to save an output wrap to a file

To save the result of the curl command, you can use the flag -o or -O.

Flag with lowercase o, -o It will save the file with the specified name, which in the example below is vue-v2.5.16.js:

curl -o vue-v2.5.16.js  https://cdn.jsdelivr.net/npm/vue/dist/vue.js

Meanwhile, large flags with the letter Oh -O It will save the file with its original name, which in the example below is vue.js:

curl -O https://cdn.jsdelivr.net/npm/vue/dist/vue.js

How to download multiple files using Curl

To download multiple files simultaneously, you can use multiple tags -O Followed by the URL of the file you want to download. In the following example, we download the Arch Linux and Debian iso file:

curl -O http://mirrors.edge.kernel.org/archlinux/iso/2018.06.01/archlinux-2018.06.01-x86_64.iso / -O https://cdimage.debian.org/debian-cd/current/amd64/iso-cd/debian-9.4.0-amd64-netinst.iso

How to continue downloading with Curl

You can continue downloading using the options -C -. This option is especially useful if the connection drops while downloading a large file and instead of starting the download from scratch, you can proceed with the previous download.

For example, if you downloaded an Ubuntu 18.04 iso file, use the following command:

curl -O http://releases.ubuntu.com/18.04/ubuntu-18.04-live-server-amd64.iso

Suddenly your connection stops and the download is interrupted, and when the Internet connection becomes smooth again, you can continue downloading with the following command:

curl -C - -O http://releases.ubuntu.com/18.04/ubuntu-18.04-live-server-amd64.iso

Get HTTP headers from URLs with Curl

To fetch the HTTP headers from the specified source, use the tag -I:

curl -I --http2 https://www.ubuntu.com/

Test if website supports HTTP / 2 with Curl

To check if a specific URL supports the new HTTP / 2 protocol, fetch the HTTP header with -I Along with flags --http2:

curl -I --http2 -s https://www.linuxid.net | grep HTTP
output :
HTTP/2 200
curl -I --http2 -s https://www.ubuntu.com/ | grep HTTP 
output :
HTTP/1.1 200 OK

As you can see from the above output if the site supports HTTP / 2, curl will be printed HTTP/2.0 200, Otherwise it will be printed HTTP/1.1 200.

If you have a curl version 7.47.0 Or later, you don’t need to use tags --http2 Because HTTP / 2 is enabled by default for all HTTPS connections.

In the command above, we also used flags -s, Which tells curl to run in silent mode and hides the progress meter and error messages.

Follow the redirects using cURL

If you try to take google.co.id home page without www , You will see the following:

curl google.com

curl-follow-redirects

As you can see from the above output google.com It redirects to the www version and since curl doesn’t follow the HTTP Location by default you won’t get the source from the Google homepage.

In this case, you can use tags -L Which directs curl to follow any redirects until it reaches its final destination:

curl -L google.com

Change user agent wrap

Sometimes while downloading files, remote servers can be set up to block User-Agent Curl or the page you want to download may present completely different content depending on the visitor’s device and browser.

In such a case, we can simulate a different browser, and use tags -A:

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

The above command will emulate Firefox 60 requesting a page from getfedora.org.

Determine the maximum conversion rate

To reduce the speed of transferring data, use the--limit-rate. The value can be expressed in bytes, in kilobytes, with a suffix k, Megabyte with the suffix m And gigabytes with the suffix g.

The following command will download the Go binaries and limit the download speed to 1MB:

The following command will download the Go binaries and limit the download speed to 1MB:

curl --limit-rate 1m -O https://dl.google.com/go/go1.10.3.linux-amd64.tar.gz

This option is useful when you don’t want curl to consume all available bandwidth.

How to transfer files via FTP using Curl

To access an FTP server that is protected with the curl command, you must pass the flag -u And specify the username and password as shown below:

curl -u FTP_USERNAME:FTP_PASSWORD ftp://ftp.example.com/

The above command will list all files and directories in the user’s home directory.

You can download files from an FTP server by:

curl -u FTP_USERNAME:FTP_PASSWORD ftp://ftp.example.com/file.tar.gz

To upload files to an FTP server, use -T Followed by the name of the file you want to upload:

curl -T FileUpload.tar.gz -u FTP_USERNAME:FTP_PASSWORD ftp://ftp.example.com/

How to send cookies with Curl

When placing orders using curl, cookies are not sent or stored by default. Sometimes you may need to perform HTTP requests with specific cookies to access remote resources or to debug errors.

To send cookies to the server, use the key -b It is followed by the name of the file containing the cookie or chain.

In the following example, we download the Java JDK rpm file jdk-10.0.2_linux-x64_bin.rpm On the home page and sends a cookie named oraclelicensevaluable a.

curl -L -b "oraclelicense=a" -O http://download.oracle.com/otn-pub/java/jdk/10.0.2+13/19aef61b38124481863b1413dce1855f/jdk-10.0.2_linux-x64_bin.rpm

Using Curl with agents

Curl supports different types of proxies including HTTP, HTTPS, and SOCKS.

To transfer data through a proxy server, use the option -x (--proxy) Followed by the proxy URL

To download the linux.com webpage using a proxy on port 192.168.44.1 and port 8888, you’ll use something like this:

curl -x 192.168.44.1:8888 http://linux.com/

If the proxy server requires authentication, use the option -U (--proxy-user(Followed by a username and password separated by a colon)user:password):

curl -U doni:Passvv0rd -x 192.168.44.1:8888 http://linux.com/

Conclusion

The examples shown in this tutorial are the most used curl options and are intended to try to help you understand how the curl command works.

If you want to learn more about curl, visit the Curl Documentation page.

.

Source link

Leave a Reply

Your email address will not be published. Required fields are marked *