Linux Curl Common commands detailed _linux shell

Source: Internet
Author: User
Tags curl gettext urlencode

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 by using the-o/-o option:

-O: Save the file as a file name specified in the command line

-O: Saves files to local using the default file name in the URL

# Download the file locally and name it mygettext.html
Curl-o mygettext.html http://www.gnu.org/software/gettext/manual/gettext.html 
# Save the file locally and name it gettext.html
 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

 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).

Redirecting through 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 is sent

Header as a request, and then redirect the request to the new address.

For example, when accessing Google.com, the address is automatically redirected to the google.com.hk.

Curl http://www.google.com
 <HTML>
 <HEAD>
  <meta http-equiv= content-type "content=" text /html;charset=utf-">
  <TITLE> moved</title>
 </HEAD>
 <BODY>
  <H> Moved 
 

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

This can be forced redirection by using the-l option

1 # Let Curl use address redirection, the GOOGLE.COM.HK site will be queried at this time

2 Curl-l http://www.google.com

Breakpoint Continuation

Using the-C option allows you to use breakpoint continuation functions on large files, such as:

# When the file ends the process before the download is complete
 $ curl-o http://www.gnu.org/software/gettext/manual/gettext.html
 ##############       .%
 # Continue downloading the file by adding the-C option, the downloaded files will not be downloaded
 curl-c-o http://www.gnu.org/software/gettext/manual/gettext.html
 ###############      .%

Using network speed limit for curl

Limit the maximum network use of curl through the--limit-rate option

# download Speed Max no more than 1000b/second
Curl--limit-rate 1000b-o http://www.gnu.org/software/gettext/manual/gettext.html
Download the modified file in the specified time

When downloading a file, you can make a judgment on the date of the last modification of the file and download it if it has been modified within the specified date.
This feature can be achieved by using the-Z option:

# If the yy.html file is updated after 2011/12/21, it will be downloaded.
Curl-z 21-dec-11 http://www.example.com/yy.html

Curl Authorization

When accessing a page that requires authorization, you can provide a username and password with the-u option to authorize

Curl-u Username:password URL
# The usual practice is to enter a username at the command line and then prompt for a password to ensure that the password is not compromised when viewing the history
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 rather than a specific file name to download, curl lists all the file names under that directory instead of downloading all the files in that directory.

# list all folders and files under public_html
Curl-u Ftpuser:ftppass-o ftp://ftp_server/public_html/
# download xss.php file
Curl-u Ftpuser:ftppass-o ftp://ftp_server/public_html/xss.php
Uploading files to an FTP server

Upload the specified local file to the FTP server with the-t option

# upload MyFile.txt files to server

curl-u ftpuser:ftppass-t myfile.txt ftp://ftp.testserver.com

# Simultaneously uploading multiple files

curl-u Ftpuser:ftppass-t "{file1,file2}" ftp://ftp.testserver.com

# Save from standard input to the server-specified file

curl-u ftpuser:ftppass- T-ftp://ftp.testserver.com/myfile_1.txt

Get more information

Get more link information by using-V and-trace

Query words by dictionary

# Query The meaning of bash words
Curl Dict://dict.org/d:bash
# List all available dictionaries
Curl Dict://dict.org/show:db
# Query The meaning of bash words in the Foldoc dictionary
Curl Dict://dict.org/d:bash:foldoc

Set up agents for curl

-X option to add agent functionality to curl

# Specify proxy host and port
Curl-x proxysever.test.com:3128 http://google.co.in

Other website finishing

Save and use site cookie information

# Save cookies from the website to sugarcookies file
curl-d sugarcookies http://localhost/sugarcrm/index.php
# using the last saved cookie information
Curl-b sugarcookies http://localhost/sugarcrm/index.php
Passing Request data

The default curl uses a Get method to request data, which passes data directly through the URL
You can specify that the post is used to pass data by--data/-d

# get
Curl-u username Https://api.github.com/user?access_token=XXXXXXXXXX
# POST
Curl-u username--data "Param=value&param=value" https://api.github.com
# You can also specify a file to pass the contents of the file as data to the server side
Curl--data @filename Https://github.api.com/authorizations

Note: By default, passing the past data in the post mode if there are special characters, the first need to pass the special character escape on the server side, such as the value contains a space, you need to first convert the space to%20, such as:

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.

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

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

Curl-i-X DELETE HTTPS://API.GITHUB.CIM

Uploading files

Curl--form "fileupload= @filename. txt" Http://hostname/resource

Related Article

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.