Linux Curl 命令示例 (第1篇)

CURL 是服务器上用于数据传输的命令行工具。CURL 支持许多协议,例如: DICT, FILE, FTP, FTPS, GOPHER, HTTP, HTTPS, IMAP, IMAPS, LDAP, LDAPS, POP3, POP3S, RTMP, RTSP, SCP, SFTP, SMB, SMBS, SMTP, SMTPS, TELNET, FTP。

CURL 提供100多个命令行选项,在本教程中,我们将介绍一些基本但有用的选项。

(1) 下载并将输出存储在文件中

把下载的数据在终端输出

$ curl http://www.cricbuzz.com

将输出结果保存到文件中

$ curl [url] > [name-of -output-file]

$ curl http://www.cricbuzz.com > cricbuzz.html

或者,还可以使用 -o 选项将输出保存到特定的文件

$ curl -o [name-of-output-file] [url-name]

示例如下:

$ curl -o cricbuzz.html http://www.cricbuzz.com

还存在一个 -O 选项,允许将下载的数据保存在与远程文件名称相同的文件中。

$ curl -O [url]

示例如下:

$ curl -O https://curl.haxx.se/docs/manpage.html

上面的命令将下载的数据保存在一个名为 manpage.html 的文件中

(2) 静默命令输出

如果您不希望 curl 在输出中显示进度细节和错误,那么您可以使用 -s 选项

例如,考虑以下 curl 抛出错误的情况

$ curl https://lti.com

要消除此类错误,使用 -s 选项

$ curl -s https://lti.com

如果您希望 curl 只显示错误而不显示任何其他细节(如默认情况下显示的进度细节),请使用 -s 选项和 -S 选项

$ curl -s -S https://www.lti.com

(3) 下载多个文件

使用该工具,您可以通过一个命令下载多个文件

$ curl -o/O [url1] -o/O [url2]

示例如下:

$ curl -O https://curl.haxx.se/docs/manpage.html -O https://curl.haxx.se/docs/manual.html

(4) 处理 URL 重定向

Suppose, you provide a URL to the Curl command, but the web page
doesn’t exist (say, it has been moved to some other location). In that
case, you can use the -L command line option, which will make curl redo the request on the new place.

假设,您向 Curl 命令提供了一个 URL,但是 web 页面不存在 (比如,它已经移动到其他位置)。在这种情况下,您可以使用 -L 命令行选项,这将使 curl 在新位置上重做请求。

For example, consider a case where-in Curl gives an error like ‘ page moved ‘.

考虑这样一种情况,在 Curl 中抛出一个错误,例如:页面移动

But if you access the web page through a Web browser, you observe a
redirect. Now, to make sure Curl also handles this redirect, use the -L command line option.

但如果您通过 web 浏览器访问网页,则会观察到重定向。为了确保 Curl 也处理这个重定向,使用 -L 命令行选项。

$ curl -L [url]

示例如下:

$ curl -L uber.com

(5) 获取详细信息(-v)

使用 -v 选项,可以检索关于 Curl 操作的详细信息

以 > 和 < 开头的行分别代表 Curl 发送和接收的报头数据,* 表示工具提供的附加信息,示例如下:

$ curl -v https://curl.haxx.se/docs/manpage.html

如果对使用 -v 选项得到的细节不满意,那么可以使用 -trace选项

$ curl --trace info https://curl.haxx.se/docs/manpage.html

(6) 使用 DICT 协议查找单词含义

通过该工具,您可以使用 dict 协议在终端上搜索单词。一个字典服务器 dict.org url 被传递给它,dict.org 服务器支持大约 77 种字典。

要列出终端上支持的所有字典,运行以下命令:

$ curl dict://dict.org/show:db

现在,要在特定的字典中搜索单词,请使用以下命令

$ curl dict://dict.org/d:[word-to-be-searched]:[dictionary-name]

示例如下:

$ curl dict://dict.org/d:command:gcide

备注: gcide 是 The Collaborative International Dictionary of English 的简称。

如果你想在所有的字典中搜索一个单词,运行以下命令

$ curl dict://dict.org/d:[word-to-be-searched]:*

示例如下:

$ curl dict://dict.org/d:command:*

我的开源项目

作者:鸠摩智首席音效师原文地址:https://segmentfault.com/a/1190000043388776

%s 个评论

要回复文章请先登录注册