Common curl commands

Source: Internet
Author: User

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

curl http://www.centos.org

Use the-O/-O option to save the downloaded file to the specified file:
-O: saves the file as the file name specified in the command line.
-O: use the default file name in the URL to save the file to the local device.

1 # download the file to the terminal and name it mygettext.html 2 curl-O mygettext.html http://www.gnu.org/software/gettext/manual/gettext.html3 4 # Save the file to the terminal and name it gettext.html 5 curl-O http://www.gnu.org/software/gettext/manual/gettext.html

You can also use the conversion character ">" to redirect the output.

Obtain multiple files at the same time

1 curl -O URL1 -O URL2

If multiple files are downloaded from the same site at the same time, curl tries to reuse the connection ).

Redirect using 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 then 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     <H1>302 Moved</H1> 9     The document has moved10     <A HREF="http://www.google.com.hk/url?sa=p&amp;hl=zh-CN&amp;pref=hkredirect&amp;pval=yes&amp;q=http://www.google.com.hk/&amp;ust=1379402837567135amp;usg=AFQjCNF3o7umf3jyJpNDPuF7KTibavE4aA">here</A>.11 </BODY>12 </HTML>

The above output indicates that the requested file was transferred to the http://www.google.com.hk.

This can be forcibly redirected by using the-L option

1 # Let curl use address redirection, this will query google.com.hk Site 2 curl-l http://www.google.com

Resumable upload

You can use the-C option to resume the upload of large files, for example:

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

Use network speed limit for Curl
Use the -- limit-rate option to restrict the maximum network usage of curl

1 # download speed up to 1000b/second2 3 curl -- limit-rate 1000b-O http://www.gnu.org/software/gettext/manual/gettext.html

Download modified files within the specified time

When downloading an object, you can judge the last modification date of the object. If the object has been modified within the specified date, the object will be downloaded. Otherwise, the object will not be downloaded.
You can use the-Z option to implement this function:

1 # If the yy.html file in 2011/12/21 after there is an update will be downloaded 2 curl-Z 21-dec-11 http://www.example.com/yy.html

Curl authorization

When you access the page to be authorized, you can use the-u option to provide the user name and password for authorization.

1 curl-u Username: Password url2 3 # The common practice is to enter only the user name in the command line, and then prompt to enter the password, this ensures that the password will not be disclosed when viewing the History 4 curl-u username URL

Download files from the FTP server

Curl also supports FTP download. If the URL specifies a file path rather than a specific file name, curl will list all file names in the directory instead of downloading all files in the 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: ftppass-O ftp: // ftp_server/public_html/XSS. PHP

Upload files to the FTP server

You can use the-T option to upload the specified local file to the FTP server.

# Upload the myfile.txt file to the server curl-u ftpuser: ftppass-T myfile.txt ftp://ftp.testserver.com # upload multiple files simultaneously curl-u ftpuser: ftppass-T "{file1, file2} "ftp://ftp.testserver.com # Get content from standard input save to the file specified by the server curl-u ftpuser: ftppass-T-ftp://ftp.testserver.com/myfile_1.txt

Get more information

Use-V and-trace to obtain more link information

Query words by 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 bash words in the foldoc dictionary. 8. Curl dict: // dict.org/d:bash:foldoc

Set proxy for Curl

-The-x option can be used to add proxy functions for curl.

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

 

Organize other websites

Save and use website Cookie Information

1 # store website cookies in the sugarcookies file. 2. Curl-D. sugarcookies http: // localhost/sugarcrm/index. php3 4 # Use the cookie information saved last time 5 curl-B sugarcookies http: // localhost/sugarcrm/index. PHP

Transfer request data

By default, curl uses the get method to request data. In this method, data is directly transmitted through the URL.
You can use the -- Data/-D Method to specify the POST method to transmit data.

1 # get2 curl-u username https://api.github.com/user? Access_token = xxxxxxxxxx3 4 # post5 curl-u username -- Data "param1 = value1 & param2 = value" https://api.github. com6 7 # You can also specify a file to pass the content in 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 transmitted in the past through post, you must first escape the special characters to the server. For example, the value contains spaces, convert the space to % 20 first, for example:

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

In the new curl version, the data-urlencode option is provided. The parameters provided by this option automatically escape special characters.

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

In addition to the get and post protocols, you can use the-x option to specify other protocols, such:

1 curl -I -X DELETE https://api.github.cim

Upload files

1 curl --form "[email protected]" http://hostname/resource

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.