17 Linux curl command usage examples

cURL is a computer software project and it produces two products – libcurl and curl.

libcurl

A free and easy-to-use client-side URL transfer library, supporting DICT, FILE, FTP, FTPS, GOPHER, HTTP, HTTPS, IMAP, IMAPS, LDAP, LDAPS, POP3, POP3S, RTMP, RTSP, SCP, SFTP, SMTP, SMTPS, TELNET and TFTP.

libcurl supports HTTPS certificates, HTTP POST, HTTP PUT, FTP uploading, kerberos, HTTP form based upload, proxies, cookies, user+password authentication, file transfer resume, http proxy tunneling and more!

curl

A command line tool for getting or sending files using URL syntax. Since curl uses libcurl, curl supports the same wide range of common Internet protocols that libcurl does.

Lets see how to use the curl command line tool:

1. Basic Usage


$ curl http://www.exampleurl.com/

This is the most simple use of curl. curl defaults to displaying the output it retrieves to the standard output specified on the system (usually the terminal window). So running the command above would, on most systems, display the www.exampleurl.com source-code in the terminal window.

2. Writing the curl output to a file

a. Specifying a file name

If you want to specify the file name use the “-o (lowercase o)” flag with curl as below:


$ curl -o localfile.html http://www.exampleurl.com/

This will store the source code for www.exampleurl.com into a file named localfile.html. While retrieving output, curl will display a progress-bar showing how much of the output has downloaded.

b. Downloading output to a file that has the same name as on the system it originates from

If you want to save the output to a file that has the same name as at it origin the use the “-O (uppercase O)” flag as below:


$ curl -O http://www.exampleurl.com/examplepage.html

If no file name part is specified in the URL, this will fail.

Note: curl does not show a progress bar when it displays the output in the terminal window.

c. Downloading multiple files


$ curl -O http://www.exampleurl.com/examplepage1.html -O http://www.exampleurl.com/examplepage2.html

3. Follow HTTP redirects

curl does not follow so-called redirects by default. If the server responds that the file (examplepage.html) is moved to a different location (indicated with a Location: header and a 3XX response code), use the -L flag as shown below:


$ curl -OL www.exampleurl.com/examplepage.html

4. Retreiving a ftp document


$ curl ftp://ftp.example.com/test.txt

5. Get a directory listing of an FTP site:


$ curl ftp://ftp.example.com/

6. Download a file using username and password

a. FTP


$ curl ftp://name:passwd@<machine.domain>:port/full/path/to/file

or specify them with the -u flag like


$ curl -u name:passwd ftp://<machine.domain>:port/full/path/to/file

b. HTTP


$ curl http://name:passwd@<machine.domain>/full/path/to/file

or specify user and password separately like in


$ curl -u name:passwd http://<machine.domain>/full/path/to/file

7. Download a file from ssh server using sftp


$ curl -u username sftp://test.example.com/test.txt

8. Download a file from an SSH server using SCP using a private key to authenticate


$ curl -u username: --key ~/.ssh/id_dsa --pubkey ~/.ssh/id_dsa.pub scp://test.example.com/~/testfile.txt

9. Upload a file on ftp server

a. To upload the data on standard input to a ftp server:


$ curl -T - ftp://ftp.ftpserver.com/filename

b. To upload a local file with a new name using username and password:


$ curl -T localfile -u user:passwd ftp://ftp.ftpserver.com/newfile

c. To upload a local file to the remote site, and use the local file name at the remote site too:


$ curl -T localfile -u user:passwd ftp://ftp.ftpserver.com/

d. To upload a local file to get appended to the remote file:


$ curl -T localfile -a ftp://ftp.ftpserver.com/remotefile

10. Upload all data on stdin to a specified HTTP site


$ curl -T - http://www.example.com/newfile

Note: HTTP server must have been configured to accept PUT before this can be done successfully

11. Delete a file through FTP after you have downloaded it


$ curl -O ftp://ftpsever.com/mydownloadfile -Q '-DELE mydownloadfile'

12. Rename a file after ftp upload


$ curl -T uploadfile ftp://upload.com/dir/ -Q "-RNFR uploadfile" -Q "-RNTO newfilename"

13. Posting HTML form data using GET method

Suppose your form HTML code is:


<form method="GET" action="http://www.example.com/register.php">

<input type=text name="personname">

<input type=submit name=register value=" OK ">

</form>

If you intend to fill name as “my name” and then your can use the below command to post the form:


$ curl "http://www.example.com/register.php?personname=my%20name&register=OK"

The url provided here is the same as you will get when posting the form via browser.

14. Posting HTML form data using POST method

Suppose your form HTML code is:


<form method="POST" action="http://www.example.com/register.php">

<input type=text name="personname">

<input type=submit name=register value=" OK ">

</form>

If you intend to fill name as “my name” and then your can use the below command to post the form:


$ curl --data "personname=my%20name&register=OK" http://www.example.com/register.php

15. Debugging

You can use the curl’s option –verbose (-v as a short option) to display what kind of commands curl sends to the server, as well as a few other informational texts.


$ curl -v ftp://ftp.ftpserver.com/

To get even more detailed information you can use the –trace or –trace-ascii options with a given file name to log to, as shown below:


$ curl --trace tracelog.txt www.example

16. Send Mail using SMTP Protocol

curl can also be used to send mail using the SMTP protocol.

$ curl --mail-from username@<domain.com> --mail-rcpt foouser@<testdomain.com> smtp://mailserver.com

Once you run the above command it will wait for you to provide the mail data. Compose your message as shown below and type . (period) as the last line, it will send the email immediately.


Subject: Testing

This is a test mail

.

17. Continue/Resume a previous download

To continue downloading a document where it was previously aborted, you can use curl as shown below:


$ curl -C - -o file ftp://ftp.server.com/documents/details.txt

 

References:

1. http://curl.haxx.se/docs/manual.html
2. http://curl.haxx.se/docs/httpscripting.html
3. http://curl.haxx.se/docs/faq.html

Leave a Comment

Back to top