Curl Common Commands

Source: Internet
Author: User
Tags gettext urlencode

Original address: http://www.thegeekstuff.com/2012/04/curl-examples/


Download a single file and print the output to standard output (STDOUT) by default

Curl http://www.centos.org

Save the downloaded file to the specified file with the-o/-o option:
-O: Save the file as the file name specified on the command line
-O: Save the file to local using the default file name in the URL

1 # Download the file locally and name it MYGETTEXT.HTML2 Curl-o mygettext.html HTTP://WWW.GNU.ORG/SOFTWARE/GETTEXT/MANUAL/GETTEXT.HTML3 4 # Save the file locally and name GETTEXT.HTML5 Curl-o http://www.gnu.org/software/gettext/manual/gettext.html

You can also use the turn character ">" to turn output to output

Get multiple files at the same time

1 Curl-o url1-o URL2

If you download multiple files from the same site at the same time, Curl tries to reuse the links (connection).

Redirection with the-l option
By default, curl does not send HTTP location headers (redirection). When a requested page is moved to another site, an HTTP loaction header is sent as a request, and the request is redirected to the new address.
For example: When you access google.com, the address is automatically redirected to google.com.hk.

1 Curl http://www.google.com 2 <HTML> 3 <HEAD> 4     <meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "> 5     <title>302 moved</title> 6 </HEAD> 7 <BODY> 8     

The above output indicates that the requested file was transferred to HTTP://WWW.GOOGLE.COM.HK.

This is possible by forcing redirection by using the-l option

1 # Let Curl use address redirection, at which point the GOOGLE.COM.HK site 2 curl-l http://www.google.com is queried

Breakpoint Continuation

Using the-C option, you can use the breakpoint continuation feature for large files, such as:

1 # When the file ends before the download completes the process 2 $ curl-o http://www.gnu.org/software/gettext/manual/gettext.html3 ##############             20.1%4 5 # Continuing to download the file by adding the-C option, the downloaded file will not be re-downloaded 6 curl-c-o http://www.gnu.org/software/gettext/manual/gettext.html7 ########## #####            21.1%

Using network speed limiting for curl
Limit the maximum network usage of curl with the--limit-rate option

1 # download Speed Max no more than 1000b/second2 3 Curl--limit-rate 1000b-o http://www.gnu.org/software/gettext/manual/gettext.html

Download files that have been modified in the specified time

When downloading a file, you can determine the last modified date of the file, if the file has been modified within the specified date, download it, otherwise it will not be downloaded.
This feature can be achieved by using the-Z option:

1 # If the yy.html file has been updated since 2011/12/21, it will be downloaded 2 curl-z 21-dec-11 http://www.example.com/yy.html

Curl Authorization

A user name and password can be authorized with the-u option when accessing a page that requires authorization

1 curl-u username:password URL2 3 # It is common practice to enter only the user name at the command line, followed by a prompt for a password, which guarantees that the password will not be disclosed when the history is viewed 4 curl-u username URL

Downloading files from an FTP server

Curl also supports FTP downloads, and if you specify a file path in the URL instead of a specific file name to download, curl lists all the file names in that directory rather than downloading all the files in that directory

1 # List all folders and files under public_html 2 curl-u ftpuser:ftppass-o ftp://ftp_server/public_html/3 4 # Download xss.php file 5 curl-u ftpuser:ft Ppass-o ftp://ftp_server/public_html/xss.php

Uploading files to an FTP server

The-t option uploads the specified local file to the FTP server

# upload MyFile.txt files to server curl-u ftpuser:ftppass-t myfile.txt ftp://ftp.testserver.com# simultaneously uploads multiple files curl-u ftpuser:ftppass-t " {file1,file2} "ftp://ftp.testserver.com# saved from standard input to a file specified by the server curl-u ftpuser:ftppass-t-ftp://ftp.testserver.com/ Myfile_1.txt

For more information

Get more link information by using-V and-trace

Querying words through a dictionary

1 # Query The meaning of bash words 2 Curl DICT://DICT.ORG/D:BASH3 4 # List all available dictionaries 5 Curl DICT://DICT.ORG/SHOW:DB6 7 # Query The meaning of the bash word in the Foldoc dictionary 8 Curl Dict://dict.org/d:bash:foldoc

Set up a proxy for curl

-X option to add agent functionality to curl

1 # Specify the proxy host and Port 2 curl-x proxysever.test.com:3128 http://google.co.in

Other web site collation

Saving and using Web site cookie information

1 # Save the cookie information of the website to the Sugarcookies file 2 curl-d sugarcookies http://localhost/sugarcrm/index.php3 4 # Use last saved cookie information 5 curl -B sugarcookies http://localhost/sugarcrm/index.php

Passing Request data

Default curl uses Get method to request data, in this way directly through the URL to pass the data
You can use the--data/-d method to specify that data is passed by post

1 # GET2 Curl-u username https://api.github.com/user?access_token=XXXXXXXXXX3 4 # POST5 curl-u username--data "Param1=v Alue1&param2=value "HTTPS://API.GITHUB.COM6 7 # You can also specify a file to pass the contents of the file as data to the server-side 8 Curl--data @filename https:// Github.api.com/authorizations

Note: By default, if there are special characters in the data passed through post, you first need to pass the special character escape to the server side, if the value contains spaces, you need to convert the space to%20 first, such as:

1 curl-d "value%201" http://hostname.com

In the new version of Curl, a new option--data-urlencode is provided, and the parameters provided by this option automatically escape special characters.

1 Curl--data-urlencode "value 1" http://hostname.com

In addition to using the Get and post protocols, you can specify additional protocols through the-X option, such as:

1 curl-i-X DELETE HTTPS://API.GITHUB.CIM

Uploading files

1 Curl--form "[email protected]" Http://hostname/resource

Http://curl.haxx.se/docs/httpscripting.html

Curl Common Commands

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.