简体   繁体   English

如何使用 curl 下载文件

[英]How to download a file using curl

I'm on mac OS X and can't figure out how to download a file from a URL via the command line.我在 mac OS X 上,无法弄清楚如何通过命令行从 URL 下载文件。 It's from a static page so I thought copying the download link and then using curl would do the trick but it's not.它来自一个静态页面,所以我认为复制下载链接然后使用curl可以解决问题,但事实并非如此。

I referenced this StackOverflow question but that didn't work.我引用了这个 StackOverflow 问题,但这没有用。 I also referenced this article which also didn't work.我还参考了这篇文章,它也不起作用。

What I've tried:我试过的:

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

How can a file be downloaded through the command line?如何通过命令行下载文件?

The -o --output option means curl writes output to file you specicify instead of stdout, you put the url after -o , so the curl thinks the url is a file to write and no url specified. -o --output选项意味着 curl 将输出写入您指定的文件而不是标准输出,您将 url 放在-o之后,因此 curl 认为该 url 是一个要写入的文件并且没有指定 url。 You need a file name after the -o , then the url.-o之后需要一个文件名,然后是 url。 Since the url is HTTPS-based, maybe you also need the -k option:由于 url 是基于 HTTPS 的,也许您还需要-k选项:

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

And wget is not available by default on OS X.并且 wget 在 OS X 上默认不可用。

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

-O : This option used to write the output to a file which named like remote file we get. -O :此选项用于将输出写入一个名为我们得到的远程文件的文件。 In this curl that file would be Workflows.git .在这个 curl 中,该文件将是Workflows.git

-L : This option used if the server reports that the requested page has moved to a different location (indicated with a Location: header and a 3XX response code), this option will make curl redo the request on the new place. -L :如果服务器报告请求的页面已移动到不同的位置(用 Location: 标头和 3XX 响应代码表示),则使用此选项,此选项将使 curl 在新位置重做请求。

Ref: curl man page参考: curl 手册页

The easiest solution for your question is to keep the original filename.您问题的最简单解决方案是保留原始文件名。 In that case, you just need to use a capital o ("-O") as option (not a zero=0!).在这种情况下,您只需要使用大写 o(“-O”)作为选项(而不是零 = 0!)。 So it looks like:所以它看起来像:

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

There are several options to make curl output to a file有几个选项可以将 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  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM