Simply curl or command-line tool and library for transferring data with URLs. curl provides a wide range of support to download files and folders with a command-line interface. curl can be used with a lot of different types of protocols. Below are the protocols currently supported by the curl.
- Telnet and TFTP
- SSL certificates
- HTTP POST, HTTP PUT, FTP uploading, HTTP form based upload, proxies, HTTP/2, cookies, user+password authentication (Basic, Plain, Digest, CRAM-MD5, NTLM, Negotiate and Kerberos), file transfer resume, proxy tunneling
Keep your breath because there are a lot of examples about curl where we look at them one by one.
curl Command Syntax
The syntax of the curl command is like below.
curl [options...] <url>
curl Command Man
Man page of curl can get with the following command where the man page provides detailed help information about the curl command.
$ man curl
curl Command Help
Simple fast help about parameters and options can get with the following command. The -h option will be used to list help information. Help information provides more basic information about the options and their meanings.
$ curl -h
Fetch And Display with HTTP and HTTPS Connections
One of the most used types of curl is downloading HTML or similar files with HTTP/s. curl will automatically detect the transfer protocol which is HTTPS in this example.
$ curl https://www.poftut.com
From the command output, we can see that the content of the requested URL is directly printed to the standard output which is the terminal in this case. Alternatively, the following command can be used to redirect the output into a file.
$ curl https://www.poftut.com > poftut.html
Download with HTTP/s and Redirect
In the previous example, the downloaded file is printed out the standard output which is our terminal. But this is generally not a practical usage. The downloaded file can be redirected to a file with a bash redirect feature like below. In this example, we can see some statistical information about the download. This information provides total size, received, average download speed, total time and times spend for download.
$ curl https://www.poftut.com > poftut.html
Download with HTTP/s
There is a parameter used to save the downloaded file into a file with no extra command. This parameter is
$ curl -o poftut.html https://www.poftut.com
Limit Download Rate
Another useful feature of curl is limiting download rate. This can be very useful in situations where there is limited internet bandwidth and it should be shared with other applications. In these examples, we have limited bandwidth to 1K and this will make our download take some time.
$ curl --limit-rate 1k -o poftut.html https://www.poftut.com
Download Sequential Files
System administrators generally prefer using sequential file names for backups or similar operations. Also, log files that reside in
/var/log are generally names sequentially. So downloading them one by one specifying the full name of the file is drudgery work. curl can download these files like the following example.
$ curl ftp://www.poftut.com/backup[1-9].tar
Set SSL Version For HTTPS
While using secure protocols like HTTPS, FTPS, POP3S SSL protocol will be used to create encrypted channels. SSL has a different version where may create incompatible situations. SSL version can be specified with the
--sslv3 parameter. But keep in mind that curl uses the GnuTLS library for cryptographic operations and GnuTLS should have support for SSLv2.
$ curl --sslv2 -o poftut.html https://www.poftut.com
Verbose and Debug Mode
While downloading and uploading files with curl there will be operations that occur background. There will be also problems that we can not know what is happening. curl can provide details about the operations with the
-v parameter like below.
$ curl --ssl -v -o poftut.html https://www.poftut.com
There is another mode where there will be no output to the terminal. This can be used for clean download.
$ curl -s -o poftut.html https://www.poftut.com
Specify User and Password
While using authentication required protocols and servers curl can provide these credentials like username and password.
$ curl -u ismail:mypassword -o data.tar ftp://poftut.com/backup.tar
Set Cookie For Connection
As we know HTTP is a stateless protocol. To preserve user session cookies are used. While using HTTP protocol existing cookies can be used with
-b parameter like below.
$ curl -b aerf34fawfeawf -o statistics.html https://www.poftut.com/statistics.html