Linux what is curl

curl command in Linux with Examples

curl is a command line tool to transfer data to or from a server, using any of the supported protocols (HTTP, FTP, IMAP, POP3, SCP, SFTP, SMTP, TFTP, TELNET, LDAP or FILE). curl is powered by Libcurl. This tool is preferred for automation, since it is designed to work without user interaction. curl can transfer multiple file at once.

Syntax:

URL : The most basic uses of curl is typing the command followed by the URL.

This should display the content of the URL on the terminal. The URL syntax is protocol dependent and multiple URLs can be written as sets like:

URLs with numeric sequence series can be written as:

Progress Meter: curl displays a progress meter during use to indicate the transfer rate, amount of data transferred, time left etc.

If you like a progress bar instead of meter, you can use the -# option as in the example above, or –silent if you want to disable it completely.

Example:

Options:

    -o : saves the downloaded file on the local machine with the name provided in the parameters.

Syntax:

Example:

Output:

The above example downloads the file from FTP server and saves it with the name hello.zip.
-O : This option downloads the file and saves it with the same name as in the URL.

Syntax:

Example:

Output:


-C – : This option resumes download which has been stopped due to some reason. This is useful when downloading large files and was interrupted.

Syntax:

Example:


–limit-rate : This option limits the upper bound of the rate of data transfer and keeps it around the given value in bytes.

Syntax:

Example:

Output:

The command limits the download to 1000K bytes.
-u : curl also provides options to download files from user authenticated FTP servers.

Syntax:

Example:

Example:


-T : This option helps to upload a file to the FTP server.

Syntax:

If you want to append a already existing FTP file you can use the -a or –append option.
–libcurl :This option is very useful from a developers perspective. If this option is appended to any cURL command, it outputs the C source code that uses libcurl for the specified option. It is the code similar to the command line implementation.

Syntax:

Example:

Output:

The above example downloads the HTML and saves it into log.html and the code in code.c file. The next command shows the first 30 lines of the code.
-x, –proxy : curl also lets us use a proxy to access the URL.

Syntax:

If the proxy requires authentication, it can be used with the command:
Sending mail : As curl can transfer data over different protocols, including SMTP, we can use curl to send mails.

Syntax:

curl –url [SMTP URL] –mail-from [sender_mail] –mail-rcpt [receiver_mail] -n –ssl-reqd -u : -T [Mail text file]

Syntax:

Example:

Output:

Note: There are a number of other options provided by cURL which can be checked on the man page. The Libcurl library has been ported into various programming languages. It’s advisable to visit the individual project site for documentation.

Источник

What is Curl in Linux, How to use curl command? Complete Guide

In my previous post, I have told you how you can copy files to and from the server over ssh protocol. More details read

But, how will you transfer files to or from the server, if ssh server is not running there? Some other service is active and working properly, for example, HTTP / https, ftp.

Don’t hit a hammer on your head, Today I am going to solve your problem in this article. curl is a powerful command to transfer files to or from servers over 20+ protocols. Keep reading. I will cover what is curl in Linux and how to use curl and other lots of curl options including download single files, multiple files, and how to use proxy in curl. I will write a complete curl cheat sheet in this article.

So what are you waiting for move to the next option.

What is curl in Linux?

Curl is a command line utility to transfer data from or to a server. You can say it is used to download and upload files and data by using one of the supported protocols (DICT, FILE, FTP, FTPS, GOPHER, HTTP, HTTPS, IMAP, IMAPS, LDAP, LDAPS, POP3, POP3S, RTMP, RTSP, SCP, SFTP, SMB, SMBS, SMTP, SMTPS, TELNET and TFTP). The command is designed to work without user interaction.

curl offers a busload of useful tricks like proxy support, user authentication, FTP upload, HTTP post, SSL connections, cookies, file transfer resume, Metalink, and more. As you will see below, the number of features will make your head spin!

curl is powered by libcurl for all transfer-related features. See libcurl(3) for details.

Resolve Curl command not found by Installing it

Today, the curl tool is pre-installed on most Linux distribution, you can start using curl by using curl command.

You can check whether the curl tool is installed on your Linux system, Open terminal and type command curl, and hit enter.

If curl is not installed in your system you will see the message “Curl command not found“. I removed curl from my Ubuntu system. Then I used the curl command to check it, and I found the following result.

Now You can Install on Ubuntu by using following command

$sudo apt install curl

If you want to install on RPM based operating system like CentOS, RHEL, Fedora then you can use following command:

$sudo yum install curl

How to use curl Option

The basic syntax of curl command is as follows:

curl [options] [URL…]

In the above example curl command followed by:

  • options – The Curl options starting with one or two dashes.
  • URL – URL of the remote server.

In its simplest form when used without any option, curl will display the resource specified in the [url] to the standard output.

By mistake, I used following curl command without any option and only followed by domain name cyberpratibha.com only.

The command above printed the source code of the www.cyberpratibha.com homepage in your terminal window. I can’t post the complete source code here. Sorry about it, but you can run command yourself.

I mentioned protocol https but If you don’t specify any protocol curl will try to access files by using default protocol HTTP.

Curl download file in Linux With Example

If you want to download file with curl command, then you can use either the -o or -O option.

Uppercase -O will download and save file with its original name. See Example below

To see downloaded file I used ls command in above example.

Lowercase -o saves the downloaded file with a newname of file, which in the example below is

Curl Download multiple files in Linux

If you want download multiple files by using curl then, It quite easy and simple. you can provide all addresses followed -O options.

For the example:

$curl -O [file1_address] -O [File2_address] -O [file3_address]

See live example below

How to Resume a Download from point of Interruption?

If your downloading process is interrupted by any reason then you a resume download process by using the -C – option.

It is very useful for those people, who don’t have an internet connection. Or internet connection drops during the download of a large file. You don’t need to download the same file from scratch you can continue with the previous downloads.

In following example, I interrupted connection two times by pressing Ctrl+C and one time pressing Ctrl+z .

What would be the result. See by yours

Curl x options For a proxy with Example

If you are using a proxy server then no problem at all, Curl supports different types of proxies including HTTP, HTTPS, and SOCKS.

If you want to download and upload data through a proxy server, then you can use the -x (–proxy) option followed by the proxy server address, It may be IP Address of domain name.

The following command will download the specified web page using a proxy on 192.168.225.46 port 4444:

$curl -x 192.168.225.46:4444 https://www.cyberpratibha.com

If the proxy server requires authentication, use the -U in upper case -U or –proxy-user option followed by the user name and password separated by a colon (user:password):

$curl -U username:password -x 192.168.225.46:4444 https://www.cyberpratibha.com

Conclusion:

So you have seen, the curl command is a very useful utility for Linux lovers if you are into downloading stuff through the command line.

We’ve just covered most used and common options, but curl offers a lot more features.

Once you are done practicing the command line options discussed in this tutorial, you can head to curl’s manual page to know more about it.

If you have any question related this article please leave a comment in the comment box.

Источник

What is a Curl Command And How to Use It?

What is a Curl command? It’s a command available on most Unix based systems. This stands as an abbreviation to “Client URL”. Curl commands are designed to work as a way to check connectivity to URLs and as a great tool to transfer data. Let’s learn how you can start using it.

Curl command supports the below list of protocols:

  • HTTP and HTTPS
  • FTP and FTPS
  • IMAP and IMAPS
  • POP3 and POP3S
  • SMB and SMBS
  • SFTP
  • SCP
  • TELNET
  • GOPHER
  • LDAP and LDAPS
  • SMTP and SMTPS

These are the most important supported protocols, but there are some others as well. Curl is powered by libcurl, which is a free URL transfer library at the client side.

Check Curl Version

Before we begin working with Curl, we need to log into our VPS. If you need help, check out this tutorial about SSH.

First, let’s check what version of Curl is available with the following command:

The output will show the Curl version a list of supported protocols. Now we can look at some Curl command examples

Basic Curl Command Syntax

Let’s learn how to use Curl commands. The basic syntax of Curl looks like this:

The simplest use of Curl is to display the contents of a page. The below example will render the home web page of testdomain.com.

This will render the complete source code of the homepage for the domain. If no protocol is specified curl will interpret this to HTTP.

Curl Command File options

Curl commands can download files from a remote location. You can do it in two different ways:

  • -O will save the file in the current working directory with the same file name as remote.
  • -o lets you specify a different file name or location

An example of this is as shown below:

The above command will save this as testfile.tar.gz.

The above command will save this as newtestfile.tar.gz.

If for some reason, the download gets interrupted, you can resume it using curl. You can do this with the following command:

Using curl, we can also download multiple files, like shown below:

If you want to download multiple files from multiple URL, list all of them in a file. Curl commands can be combined with xargs to download the different URLs.

For instance, if we have a file allUrls.txt which contains a list of all URLs to be downloaded, then the below example can be used to download all files.

Curl Commands for Http

Curl can also be used when there is a proxy server. If you are behind a proxy server listening on port 8090 at sampleproxy.com, download the files as shown below:

In the above example, you can skip -U username:password if the proxy does not require an authentication method.

A typical HTTP request will always contain a header. The HTTP header sends additional information about the remote web server along with the actual request. While through a browser’s developer tools you can check the header information, you can verify it using a curl command.

Below is an example of how to retrieve header information from a website.

Using curl, you can make a GET and a POST request. A GET request will be as follows:

A sample of a POST request will be as shown below:

Over here text=Hello is the POST request parameter. This behavior would be similar to HTML forms.

You can also specify multiple HTTP methods in a single curl command. Do this by using –next option, like this:

This contains a POST request followed by a GET request.

Every HTTP request will have a user agent which is sent as part of the request. This indicates the web browser details of the client. By default, a curl request contains curl and the version number as the user agent details. A sample output is as shown below:

You can change this default user agent information using the below command:

Now the changed output will be:

Curl for Cookies

Curl commands can be used to check what cookies get downloaded on any URL. So, if you are accessing https://www.samplewebsite.com, then you can output to a file, save the cookies and access them using cat or a VIM editor.

Below is a sample of such command:

Similarly, if you have the cookies in a file, then you can send it to the website. An example of such command is as shown below:

Curl for FTP

Curl command supports FTP! You can use them to download files from a remote server.

In the above command, ftp://sampleftpserver is an FTP server which accepts connections. The username and password can be skipped for anonymous FTP connections. Type in the command and watch the progress bar fill up.

You can upload files as well with the command below:

Again, we can skip the username and password for anonymous FTP connections.

Limiting Curl Output

While using a curl you can’t know how big the output will be. You can restrict bandwidth, to make sure it’s not throttled by curl.

The below command restricts the bandwidth to 100K:

Conclusion

Curl is a powerful and widely used command. It’s useful when you’re dependent on the command line. It has several options and supports multiple protocols. That’s a pretty great reason to learn this command!

Remember, if you want to learn some advanced commands, simply refer to the manual that should be in all Unix versions:

We hope this tutorial gave you a good jumping off point for using Curl! How will you use this command? Let us know in the comments!

Edward is an expert communicator with years of experience in IT as a writer, marketer, and Linux enthusiast. IT is a core pillar of his life, personal and professional. Edward’s goal is to encourage millions to achieve an impactful online presence. He also really loves dogs, guitars, and everything related to space.

Источник

Читайте также:  Установить webdav windows 10
Оцените статью