Curl在下面的位置抓取远程文件名 [英] Curl to grab remote filename after following location
问题描述
使用curl下载文件时,如何查看链接位置并将其用于输出文件名(不提前知道远程文件名)?
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)?
例如,如果点击下面的链接,你会下载一个文件名为pythoncomplete.vim。但是使用curl的-O和-L选项,文件名只是原来的远程名称,一个笨拙的download_script.php?src_id = 10872。
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
如果你可以在不知道名字的情况下下载文件,这将是非常好的,如果没有,是否有另一种方法通过命令行快速下拉重定向文件? p>
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?
推荐答案
如果您有最近版本的 curl
(7.21.2或更高版本),请参阅 @ jmanning2k的回答。
If you have a recent version of curl
(7.21.2 or later), see @jmanning2k's answer.
我有一个旧版本 curl
(如同Snow Leopard附带的7.19.7),请执行两个请求:a HEAD
文件名从响应头,然后一个 GET
:
I you have an older version of curl
(like 7.19.7 which came with Snow Leopard), do two requests: a HEAD
to get the file name from response header, then a GET
:
url="http://www.vim.org/scripts/download_script.php?src_id=10872"
filename=$(curl -sI $url | grep -o -E 'filename=.*$' | sed -e 's/filename=//')
curl -o $filename -L $url
这篇关于Curl在下面的位置抓取远程文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!