Original address: http://www.thegeekstuff.com/2012/04/curl-examples/
download a single file and print the output to standard output by default (STDOUT) in
Curl http://www.centos.org
through -o/-o option to save the downloaded file to the specified file:
-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.html
2 curl-omygettext.html http://www.gnu.org/software/gettext/manual/gettext.html
3
4 # Save the file locally and name it gettext.html
5 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
9 The document has moved
Ten <a href= "http://www.google.com.hk/url?sa=p&hl=zh-cn&pref=hkredirect&pval=yes& AMP;Q=HTTP://WWW.GOOGLE.COM.HK/&UST=1379402837567135AMP;USG=AFQJCNF3O7UMF3JYJPNDPUF7KTIBAVE4AA "> Here</a>.
</BODY>
</HTML>
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 is queried
2 Curl-l http://www.google.com
Breakpoint Continuation
Using the-C option, you can use the breakpoint continuation feature for large files, such as:
1 # When the file ends the process before the download is complete
2 $ curl-ohttp://www.gnu.org/software/gettext/manual/gettext.html
3############## 20.1%
4
5 # Continue to download the file by adding the-C option, files that have already been downloaded will not be downloaded again
6 Curl-c--ohttp://www.gnu.org/software/gettext/manual/gettext.html
7############### 21.1%
the CURL Use network speed limit
Limit the maximum network usage of curl with the--limit-rate option
1 # download Speed Max no more than 1000b/second
2
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 not be downloaded.
2 curl-z 21-dec-11 http://www.example.com/yy.html
CURL Authorized
A user name and password can be authorized with the-u option when accessing a page that requires authorization
1 Curl-uusername:password URL
2
3 # It is common practice to enter only the user name at the command line and then prompt for a password to ensure that the password is not compromised when viewing history
4 Curl-uusername URL
from FTP Server Download File
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-uftpuser:ftppass-o ftp://ftp_server/public_html/
3
4 # Download xss.php file
5 Curl-uftpuser:ftppass-o ftp://ftp_server/public_html/xss.php
Upload files to FTP Server
The-t option uploads the specified local file to the FTP server
# Upload the MyFile.txt file to the server
CURL-UFTPUSER:FTPPASS-T MyFile.txt ftp://ftp.testserver.com
# Upload Multiple files simultaneously
Curl-uftpuser:ftppass-t "{file1,file2}" ftp://ftp.testserver.com
# Save content from standard input to file specified by server
Curl-uftpuser: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:bash
3
4 # List all available dictionaries
5 Curl Dict://dict.org/show:db
6
7 # Query The meaning of bash words in the Foldoc dictionary
8 Curl Dict://dict.org/d:bash:foldoc
to be CURL Set up proxy
-X option to add agent functionality to curl
1 # Specify the proxy host and port
2 curl-xproxysever.test.com:3128 http://google.co.in
Other web site collation
Save and use the site Cookies Information
1 # Save cookies from the website to the Sugarcookies file
2 Curl-dsugarcookies http://localhost/sugarcrm/index.php
3
4 # Use Last saved cookie information
5 Curl-bsugarcookies 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 # GET
2 Curl-uusername Https://api.github.com/user?access_token=XXXXXXXXXX
3
4 # POST
5 curl-uusername--data "Param1=value1¶m2=value" https://api.github.com
6
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-xdelete Https://api.github.cim
Uploading files
1 Curl--form "[email protected]" Http://hostname/resource
Curl Common Commands