如何使用curl下载文件

时间:2014-04-02 01:33:37

标签: macos curl command-line terminal

我在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

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

4 个答案:

答案 0 :(得分:23)

-o --output选项意味着curl将输出写入你指定的文件而不是stdout,你把网址放在-o之后,所以curl认为url是要写的文件而没有指定url。您需要-o之后的文件名,然后是网址。由于网址是基于HTTPS的,因此您可能还需要-k选项:

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

默认情况下,OS X上没有wget。

答案 1 :(得分:3)

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

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

答案 2 :(得分:2)

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

-O:此选项用于将输出写入文件,该文件的名称类似于我们获得的远程文件。在这种卷曲中,该文件将为Workflow.git

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

参考:curl man page

答案 3 :(得分:0)

有几种方法可以将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