命令行curl上传文件_命令行基础知识:使用cURL下载文件

命令行curl上传文件

Client URL, or simple cURL is a library and command-line utility for transferring data between systems. It supports a myriad of different protocols and tends to be installed by default on many Unix-like operating systems. Because of it’s general availability, it is a great choice for when you need to quickly download a file to your local system.

客户端URL或简单的cURL是用于在系统之间传输数据的库和命令行实用程序。 它支持多种不同的协议,并且倾向于默认安装在许多类Unix操作系统上。 由于它具有一般可用性,因此当您需要快速将文件下载到本地系统时,它是一个不错的选择。

入门 (Getting started)

To follow along at home, you will need to have the curl utility installed. As mentioned, it’s pretty standard issue on Unix-like operating systems such as Linux and macOS.

要在家中继续学习,您需要安装curl工具。 如前所述,这是类似Unix的操作系统(例如Linux和macOS)上的非常标准的问题。

If you don’t have the curl command available, please consult your favorite package manager. Even if it’s not installed, your package manager more than likely has it available to install.

如果没有可用的curl命令,请咨询您喜欢的软件包管理器。 即使未安装,程序包管理器也很有可能安装它。

The commands we will be issuing will be pretty safe as they will be downloading files from the Internet and non-destructive in nature. Obviously, downloading files off of the Internet can be sketchy, so be sure you are downloading from reputable sources.

我们将发布的命令将非常安全,因为它们将从Internet下载文件,并且本质上是非破坏性的。 显然,从Internet上下载文件可能是粗略的,因此请确保从信誉良好的来源下载文件。

Also, if you plan to run any scripts you have downloaded, it’s good practice to check their contents before making them executable and running them. A quick cat and looking over the code is often sufficient depending on the size of the file and your knowledge of the code you’re reviewing.

另外,如果您打算运行已下载的任何脚本,则最好在使其可执行并运行之前检查其内容。 快速cat和看在代码根据文件的大小和你的代码,你正在评论的知识往往是足够的。

取得远端档案 (Fetching remote files)

Out of the box, without any command-line arguments, the curl command will fetch a file and display it’s contents to the standard output.

开箱即用,没有任何命令行参数, curl命令将获取文件并将其内容显示到标准输出中。

Let’s give it a try by downloading the robots.txt file from your favorite development blog:

通过从您喜欢的开发博客下载robots.txt文件来进行尝试:

$ curl https://alligator.io/robots.txt
User-agent: *
Sitemap: https://alligator.io/sitemap.xml

Not much to it! Give curl a URL and it will fetch the resource and display it’s contents.

没什么大不了的! 给curl一个URL,它将获取资源并显示其内容。

保存远程文件 (Saving remote files)

Fetching a file and display it’s contents is all well and good, but what if you want to actually save the file to your system?

提取文件并显示其内容很好,但是如果要将文件实际保存到系统中怎么办?

To save the remote file to your local system, with the same filename as the server you’re downloading from, add the --remote-name argument, or simply, -O:

要将远程文件保存到本地系统中,并使用与要下载的服务器相同的文件名,请添加--remote-name参数,或简单地使用-O

$ curl -O https://alligator.io/robots.txt
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100    56  100    56    0     0    251      0 --:--:-- --:--:-- --:--:--   251

Instead of displaying the contents of the file, curl displays a nice little text-based progress meter and saves the file to the same name as the remote file’s name. You can check on things with the cat command:

curl不会显示文件的内容,而是显示一个不错的,基于文本的小进度条,并将文件保存为与远程文件名相同的名称。 您可以使用cat命令检查内容:

$ cat robots.txt
User-agent: *
Sitemap: https://alligator.io/sitemap.xml

将远程文件保存到特定文件名 (Saving remote files to a specific filename)

What if you already had a local file with the same name as the file on the remote server?

如果您已经拥有一个与远程服务器上的文件同名的本地文件,该怎么办?

Unless you are okay with overwriting your local file of the same name, you will want to add the -o or --output argument followed by the name of the local file you’d like to save the contents to:

除非可以覆盖相同名称的本地文件,否则,您将需要添加-o--output参数,后跟想要将内容保存到的本地文件的名称:

$ curl -o gator-bots.txt https://alligator.io/robots.txt
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100    56  100    56    0     0    221      0 --:--:-- --:--:-- --:--:--   221

Which downloads the remote robots.txt file to the locally named gator-bots.txt file:

gator-bots.txt远程robots.txt文件下载到本地命名的gator-bots.txt文件:

$ cat gator-bots.txt
User-agent: *
Sitemap: https://alligator.io/sitemap.xml

跟随重定向 (Following redirects)

Thus far all of the examples have included fully qualified URLs that include the https:// protocol. If you happened to try to fetch the robots.txt file and only specified alligator.io, you would be presented with an error about a redirect as we redirect requests from http:// to https:// (as one should):

到目前为止,所有示例都包括包含https://协议的标准URL。 如果您碰巧尝试获取robots.txt文件,并且仅指定了alligator.io ,那么当我们将请求从http://重定向到https:// (如应有的那样),您将看到关于重定向的错误:

$ curl alligator.io/robots.txt
Redirecting to https://alligator.io/robots.txt

No big deal though. curl has a flag you can pass in. The --location or -L argument tells curl to redo the request to the new location when a 3xx response code is encountered:

不过没什么大不了的。 curl有一个可以传递的标志--location-L参数告诉curl在遇到3xx响应代码时将请求重做到新位置:

$ curl -L alligator.io/robots.txt
User-agent: *
Sitemap: https://alligator.io/sitemap.xml

Of course if so desired, you can combine the -L argument with some of the aforementioned arguments to download the file to your local system.

当然,如果需要的话,可以将-L参数与前面提到的一些参数结合使用,以将文件下载到本地系统。

结论 (Conclusion)

curl is a great utility for quickly and easily downloading files from a remote system. While it’s similar to rsync in functionality, I find it to be a bit easier to work with since there’s less to remember in terms of arguments for some of the more common / basic tasks.

curl是一个很棒的实用程序,可用于快速轻松地从远程系统下载文件。 尽管它在功能上类似于rsync ,但我发现它的使用要容易一些,因为在一些较常见/基本任务的参数方面,您无需记住。

Like most of the simple yet powerful command-line utilities we discuss, this post really only covers the tip of the iceberg. With support for many different protocols and the added upload capabilities, curl has a ton to offer.

就像我们讨论的大多数简单但功能强大的命令行实用程序一样,本文实际上仅涵盖了冰山一角。 凭借对许多不同协议的支持以及附加的上传功能, curl可以提供大量功能。

Ready to learn more? Check out the manual page for curl by running man curl.

准备了解更多? 检查出的手册页curl通过运行man curl

翻译自: https://www.digitalocean.com/community/tutorials/workflow-downloading-files-curl

命令行curl上传文件

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值