Linux curl 命令初学者教程(5 个示例)
在此页
- Linux curl 命令
- Q1。 curl 命令如何工作?
- Q2。如何使 curl 使用相同的下载文件名?
- Q3。如何使用 curl 下载多个文件?
- Q4。如何解决移动的问题?
- Q5。如何从中断点恢复下载?
- 结论
虽然 Web 浏览器是用户从 Internet 下载内容的主要媒介,但也有一些 Linux 命令可以让您执行此操作。这些工具在没有 GUI 的无头系统上派上用场。
在本教程中,我们将讨论一个这样的命令 - curl - 除了其他功能之外,您还可以从 Web 下载内容。请注意,本文中讨论的示例是在 Ubuntu 16.04 LTS 上测试的。
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 inter?
action.
curl offers a busload of useful tricks like proxy support, user authen?
tication, FTP upload, HTTP post, SSL connections, cookies, file trans?
fer 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.
以下是一些 Q&A 风格的示例,可以让您更好地了解 curl 的工作原理。
Q1。 curl 命令如何工作?
基本用法相当简单——只需将 URL 作为输入传递给 curl 命令,并将输出重定向到一个文件。
例如:
curl http://releases.ubuntu.com/18.04/ubuntu-18.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.如何解决移动的问题?
有时,当您将 URL 传递给 curl 命令时,您会收到类似“已移动”或“已永久移动”的错误。当输入 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 命令是一个有用的实用程序。我们只是触及了表面,因为该工具提供了更多的功能。练习完本教程中讨论的命令行选项后,您可以前往 curls 手册页了解更多信息。