curl命令是一个强大且多功能的工具,用于通过各种协议(如HTTP、HTTPS、FTP等)在服务器之间传输数据。它代表“Client URL”,通常用于发起Web请求,允许用户下载文件、通过POST请求发送数据,甚至可以直接从命令行与RESTful API进行交互。curl命令可以处理各种任务,从简单的文件下载到复杂的multipart表单,由于其灵活性和提供详细输出的能力,它常被用于脚本自动化,是Linux环境中不可或缺的实用工具。

本教程将讨论curl命令的使用,包括从Web下载文件等功能。请注意,本文中的示例在Ubuntu 24.04上进行了测试。

Linux curl命令语法

curl命令允许你通过Linux命令行下载和上传数据。以下是其语法:

curl [options] [URL...]

以下是命令手册中关于该命令的说明:

curl is a tool to transfer data from or to a server, using one of the
supported protocols (DICT, FILE, FTP, FTPS, GOPHER, HTTP, HTTPS, IMAP,
IMAPS, LDAP, LDAPS, POP3, POP3S, RTMP, RTSP, SCP, SFTP, SMTP, SMTPS,
TELNET and TFTP). The command is designed to work without user
interaction.

curl offers a busload of useful tricks like proxy support, user
authentication, FTP upload, HTTP post, SSL connections, cookies, file
transfer resume, Metalink, and more. As you will see below, the number
of features will make your head spin!

curl is powered by libcurl for all transfer-related features. See
libcurl(3) for details.

以下是一些问答式的示例,可以帮助你更好地理解curl的工作方式。

Q1. curl命令如何工作?

基本用法非常简单——只需将URL作为输入传递给curl命令,然后将输出重定向到一个文件。例如:

curl http://releases.ubuntu.com/24.04/ubuntu-24.04-desktop-amd64.iso.torrent > test.torrent

注意,你也可以使用-o选项。

-o, --output <file>
    Write output to <file> instead of stdout.

回到我们的示例,数据被下载到系统上的“test.torrent”文件中,同时命令行上显示了以下输出:

以下是命令手册中关于输出中显示的进度表的说明:

curl normally displays a progress meter during operations, indicating
the amount of transferred data, transfer speeds and estimated time
left, etc.

curl displays this data to the terminal by default, so if you invoke
curl to do an operation and it is about to write data to the terminal,
it disables the progress meter as otherwise it would mess up the output
mixing progress meter and response data.

If you want a progress meter for HTTP POST or PUT requests, you need to
redirect the response output to a file, using shell redirect (>), -o
[file] or similar.

It is not the same case for FTP upload as that operation does not spit
out any response data to the terminal.

If you prefer a progress "bar" instead of the regular meter, -# is your
friend.

Q2. 如何让curl使用下载文件的原始文件名?

在上一个示例中,我们需要显式指定下载文件的名称。然而,你可以强制curl使用正在下载的文件名作为本地文件名。这可以通过-O命令行选项来实现。

curl -O http://releases.ubuntu.com/18.04/ubuntu-18.04-desktop-amd64.iso.torrent

在这种情况下,系统上会生成一个名为“ubuntu-18.04-desktop-amd64.iso.torrent”的文件。

Q3. 如何使用curl下载多个文件?

这也很简单——按照以下方式传递URL即可:

curl -O [URL1] -O [URL2] -O [URL3] ...

例如:

curl -O http://releases.ubuntu.com/18.04/ubuntu-18.04-desktop-amd64.iso.torrent -O http://releases.ubuntu.com/18.04/ubuntu-18.04-live-server-amd64.iso.torrent

上述命令执行后,输出中会显示两个URL的下载进度。

Q4. 如何解决“已移动”问题?

有时,当你向curl命令传递一个URL时,会收到“Moved”或“Moved Permanently”错误。这通常是因为输入URL重定向到了另一个URL。例如,你打开一个网站(比如oneplus.com),它会重定向到你所在国家的URL(比如oneplus.in),因此你会收到类似以下的错误:

如果你希望curl跟随重定向,请改用-L命令行选项。

curl -L http://www.oneplus.com

Q5. 如何从中断点继续下载?

有时,下载在中途会被中断。为了节省时间和数据,当你重新尝试时,可能希望从中断的位置继续下载。curl允许你通过-C命令行选项来实现这一功能。

例如:

curl -C - -O http://releases.ubuntu.com/18.04/ubuntu-18.04-desktop-amd64.iso

以下截图展示了curl命令在下载中断后继续恢复下载的情景。

总结

可以看到,curl命令是一个非常实用的工具,适合喜欢通过命令行下载文件的用户。我们在这里只是浮光掠影,该工具还提供了更多功能。当你练习完本教程中讨论的命令行选项后,可以前往curl的手册页面了解更多信息。