卷曲以在以下位置后获取远程文件名
使用curl 下载文件时,如何跟踪链接位置并将其用作输出文件名(事先不知道远程文件名)?
例如,如果单击下面的链接,您将下载一个名为“pythoncomplete.vim”的文件。然而,使用curl的-O和-L选项,文件名只是原始的远程名称,一个笨拙的“download_script.php?src_id = 10872”。
curl -O -L http://www.vim.org/scripts/download_script.php?src_id=10872
为了下载具有正确文件名的文件,您必须提前知道文件名:
curl -o pythoncomplete.vim -L http://www.vim.org/scripts/download_script.php?src_id=10872
如果您可以在不提前知道文件名的情况下下载文件,那就太好了,如果没有,是否有其他方法可以快速下载通过命令行下拉重定向文件?
When downloading a file using curl, how would I follow a link location and use that for the output filename (without knowing the remote filename in advance)?
For example, if one clicks on the link below, you would download a filenamed "pythoncomplete.vim." However using curl's -O and -L options, the filename is simply the original remote-name, a clumsy "download_script.php?src_id=10872."
curl -O -L http://www.vim.org/scripts/download_script.php?src_id=10872
In order to download the file with the correct filename you would have to know the name of the file in advance:
curl -o pythoncomplete.vim -L http://www.vim.org/scripts/download_script.php?src_id=10872
It would be excellent if you could download the file without knowing the name in advance, and if not, is there another way to quickly pull down a redirected file via command line?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
远程端使用 Content-Disposition 标头发送文件名。
如果您指定
--remote-header-name
/-J
,curl 7.21.2 或更高版本会自动执行此操作。参数的扩展版本将是:
The remote side sends the filename using the Content-Disposition header.
curl 7.21.2 or newer does this automatically if you specify
--remote-header-name
/-J
.The expanded version of the arguments would be:
如果您有最新版本的
curl
(7.21.2 或更高版本),请参阅@jmanning2k 的回答。如果您有旧版本的
curl
(例如 Snow Leopard 附带的 7.19.7),请执行两个请求:一个HEAD
从响应标头获取文件名,然后一个GET
:If you have a recent version of
curl
(7.21.2 or later), see @jmanning2k's answer.I you have an older version of
curl
(like 7.19.7 which came with Snow Leopard), do two requests: aHEAD
to get the file name from response header, then aGET
:如果您可以使用
wget
而不是curl
:If you can use
wget
instead ofcurl
:我想要一个同时适用于较旧和较新 Mac 的解决方案,而 David 为 Snow Leopard 提供的遗留代码在 Mavericks 下表现不佳。这是我根据 David 的代码创建的一个函数:
定义了这个函数后,您可以运行:
I wanted a solution that worked on both older and newer Macs, and the legacy code David provided for Snow Leopard did not behave well under Mavericks. Here's a function I created based on David's code:
With this defined, you can run:
请注意,某些配置错误的网络服务器将使用“文件名”作为键来提供名称,其中 RFC2183 指定它应该是“文件名”。 curl 只处理后一种情况。
Please note that certain malconfigured webservers will serve the name using "Filename" as key, where RFC2183 specifies it should be "filename". curl only handles the latter case.
我和约翰·库珀有同样的问题。我没有得到文件名,但得到了位置文件名。他的回答也有效,但有两个命令。
这个oneliner为我工作....
url="https://download.mozilla.org/?product=firefox-latest-ssl&os=linux64&lang=de";url=$(curl -L --head -w '%{url_ effective}' $url 2>/dev/null | tail -n1) ; curl -O $url
窃取并添加了一些内容
https://unix.stackexchange.com/ questions/126252/resolve-filename-from-a-remote-url-without-downloading-a-file
I had the same Problem like John Cooper. I got no filename but a Location File name back. His answer also worked but are 2 commands.
This oneliner worked for me....
url="https://download.mozilla.org/?product=firefox-latest-ssl&os=linux64&lang=de";url=$(curl -L --head -w '%{url_effective}' $url 2>/dev/null | tail -n1) ; curl -O $url
Stolen and added some stuff from
https://unix.stackexchange.com/questions/126252/resolve-filename-from-a-remote-url-without-downloading-a-file
使用上述 Apache Archiva 工件存储库的答案来提取最新版本的示例。卷曲返回位置行,文件名位于该行的末尾。需要删除文件名末尾的 CR。
An example using the answer above for Apache Archiva artifact repository to pull latest version. The curl returns the Location line and the filename is at the end of the line. Need to remove the CR at end of file name.
curl
没有应用grep
和其他 Unix-Fu 操作,而是附带了一个内置的“Write Out”选项变量[1],专门用于此类大小写,例如[1] https://everything.curl.dev/usingcurl/verbose/writeout#available-write-out-variables
instead of applying
grep
and other Unix-Fu operations,curl
ships with a builtin "Write Out" option variable[1] specifically for such a case, e.g.[1] https://everything.curl.dev/usingcurl/verbose/writeout#available-write-out-variables
使用上面提出的解决方案,我编写了这个辅助函数
curl2file。
[更新]
用法:
Using the solution proposed above, I wrote this helper function
curl2file.
[UPDATED]
Usage: