What is Curl in Linux, How to use? You should check these methods

What is Curl in Linux, How to use? You should check these methods

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.

Article Contents

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.

vijay@Ubuntu-19:~$curl
curl: try 'curl --help' or 'curl --manual' for more information
vijay@Ubuntu-19:~$

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.

vijay@Ubuntu-19:~$curl
bash: /usr/bin/curl: No such file or directory
vijay@Ubuntu-19:~$

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.

curl https://www.cyberpratibha.com

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

$curl -O http://releases.ubuntu.com/19.04/ubuntu-19.04-desktop-amd64.iso.torrent
vijay@Ubuntu-19:~/Downloads$curl -O http://releases.ubuntu.com/19.04/ubuntu-19.04-desktop-amd64.iso.torrent
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 80338  100 80338    0     0  62229      0  0:00:01  0:00:01 --:--:-- 62277
vijay@Ubuntu-19:~/Downloads$ls
ubuntu-19.04-desktop-amd64.iso.torrent
vijay@Ubuntu-19:~/Downloads$

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

vijay@Ubuntu-19:~/Downloads$curl -o newfilename.torrent http://releases.ubuntu.com/19.04/ubuntu-19.04-desktop-amd64.iso.torrent
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 80338  100 80338    0     0  37699      0  0:00:02  0:00:02 --:--:-- 37699
vijay@Ubuntu-19:~/Downloads$ls
newfilename.torrent  ubuntu-19.04-desktop-amd64.iso.torrent
vijay@Ubuntu-19:~/Downloads$

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

$curl -O http://releases.ubuntu.com/18.04/ubuntu-18.04.2-desktop-amd64.iso.torrent -O http://releases.ubuntu.com/16.04/ubuntu-16.04.6-desktop-amd64.iso.torrent
vijay@Ubuntu-19:~/Downloads$curl -O http://releases.ubuntu.com/18.04/ubuntu-18.04.2-desktop-amd64.iso.torrent -O http://releases.ubuntu.com/16.04/ubuntu-16.04.6-desktop-amd64.iso.torrent
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 76500  100 76500    0     0  40052      0  0:00:01  0:00:01 --:--:-- 40052
100 63840  100 63840    0     0  56098      0  0:00:01  0:00:01 --:--:-- 68059
vijay@Ubuntu-19:~/Downloads$ls
newfilename.torrent
ubuntu-16.04.6-desktop-amd64.iso.torrent
ubuntu-18.04.2-desktop-amd64.iso.torrent
ubuntu-19.04-desktop-amd64.iso.torrent
vijay@Ubuntu-19:~/Downloads$

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.

If Appreciate My Work, You should consider:

Leave a Comment