【问题标题】:How to download a file using curl如何使用 curl 下载文件
【发布时间】:2014-05-13 01:22:12
【问题描述】:

我在 mac OS X 上,不知道如何通过命令行从 URL 下载文件。它来自一个静态页面,所以我认为复制下载链接然后使用 curl 可以解决问题,但事实并非如此。

我引用了this StackOverflow question,但这不起作用。我还引用了this article,但也没有用。

我的尝试:

curl -o https://github.com/jdfwarrior/Workflows.git
curl: no URL specified!
curl: try 'curl --help' or 'curl --manual' for more information

.

wget -r -np -l 1 -A zip https://github.com/jdfwarrior/Workflows.git
zsh: command not found: wget

如何通过命令行下载文件?

【问题讨论】:

  • -o 选项意味着 curl 将输出写入 而不是 stdout。
  • 你用 github URL 做了这个吗?
  • zsh: command not found: wget 表示没有安装 wget 包。因此,要使用 wget,您必须先安装 wget。 @亚历克斯科里

标签: macos curl command-line terminal


【解决方案1】:

-o --output 选项意味着 curl 将输出写入您指定的文件而不是标准输出。你的错误是把 url 放在-o 之后,所以 curl 认为 url 是一个要写入的文件,因此没有指定 url。 -o后面需要一个文件名,然后是url:

curl -o ./filename https://github.com/jdfwarrior/Workflows.git

并且 wget 在 OS X 上默认不可用。

【讨论】:

  • 我无法使用上述命令下载文件。我尝试了以下两个命令: curl -o "test.zip" -k github.com/jonreid/XcodeCoverage.git & curl -o "test.zip" -k github.com/jonreid/XcodeCoverage/archive/master.zip 第二个命令应该可以工作,但它不工作。你能帮我吗?
  • 只是好奇,但是当您可以使用 git clone https://github.com/jonreid/XcodeCoverage.git 时,为什么还要使用 curl 呢?
  • @DShah url 已被重定向,因此您需要添加 -L 标志以指示 cURL 遵循任何重定向,以便您到达最终端点。这会起作用:curl -L -o "test.zip" -k github.com/jonreid/XcodeCoverage/archive/master.zip
  • 这里有一个简单的很好的解释,这可能很有用。 5-curl-commands-to-download-files
【解决方案2】:
curl -OL https://github.com/jdfwarrior/Workflows.git

-O:该选项用于将输出写入一个文件,该文件的名称类似于我们得到的远程文件。在这个 curl 中,该文件将是 Workflows.git

-L:如果服务器报告请求的页面已移动到不同的位置(用 Location: 标头和 3XX 响应代码指示),则使用此选项,此选项将使 curl 在新位置重做请求.

参考:curl man page

【讨论】:

    【解决方案3】:

    您的问题最简单的解决方案是保留原始文件名。在这种情况下,您只需要使用大写字母 o(“-O”)作为选项(而不是零 = 0!)。所以它看起来像:

    curl -O https://github.com/jdfwarrior/Workflows.git
    

    【讨论】:

      【解决方案4】:

      有几个选项可以将 curl 输出到文件

       # saves it to myfile.txt
      curl http://www.example.com/data.txt -o myfile.txt -L
      
      # The #1 will get substituted with the url, so the filename contains the url
      curl http://www.example.com/data.txt -o "file_#1.txt" -L 
      
      # saves to data.txt, the filename extracted from the URL
      curl http://www.example.com/data.txt -O -L
      
      # saves to filename determined by the Content-Disposition header sent by the server.
      curl http://www.example.com/data.txt -O -J -L
      
      # -O Write output to a local file named like the remote file we get
      # -o <file> Write output to <file> instead of stdout (variable replacement performed on <file>)
      # -J Use the Content-Disposition filename instead of extracting filename from URL
      # -L Follow redirects  
      

      【讨论】:

        猜你喜欢
        • 2014-04-06
        • 2016-05-02
        • 1970-01-01
        • 2012-09-03
        • 2015-02-20
        • 1970-01-01
        • 1970-01-01
        • 2016-09-26
        • 2011-01-03
        相关资源
        最近更新 更多