卷曲以在以下位置后获取远程文件名

发布于 2024-11-27 02:29:24 字数 495 浏览 1 评论 0原文

使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

单挑你×的.吻 2024-12-04 02:29:24

远程端使用 Content-Disposition 标头发送文件名。

如果您指定 --remote-header-name / -J,curl 7.21.2 或更高版本会自动执行此操作。

curl -O -J -L $url

参数的扩展版本将是:

curl --remote-name --remote-header-name --location $url

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.

curl -O -J -L $url

The expanded version of the arguments would be:

curl --remote-name --remote-header-name --location $url
陪你搞怪i 2024-12-04 02:29:24

如果您有最新版本的 curl(7.21.2 或更高版本),请参阅@jmanning2k 的回答

如果您有旧版本的 curl (例如 Snow Leopard 附带的 7.19.7),请执行两个请求:一个 HEAD 从响应标头获取文件名,然后一个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"

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: 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"
み零 2024-12-04 02:29:24

如果您可以使用 wget 而不是 curl

wget --content-disposition $url

If you can use wget instead of curl:

wget --content-disposition $url
走走停停 2024-12-04 02:29:24

我想要一个同时适用于较旧和较新 Mac 的解决方案,而 David 为 Snow Leopard 提供的遗留代码在 Mavericks 下表现不佳。这是我根据 David 的代码创建的一个函数:

function getUriFilename() {
    header="$(curl -sI "$1" | tr -d '\r')"

    filename="$(echo "$header" | grep -o -E 'filename=.*

定义了这个函数后,您可以运行:

url="http://www.vim.org/scripts/download_script.php?src_id=10872"
filename="$(getUriFilename $url)"
curl -L $url -o "$filename"
)" if [[ -n "$filename" ]]; then echo "${filename#filename=}" return fi filename="$(echo "$header" | grep -o -E 'Location:.*

定义了这个函数后,您可以运行:


)"
    if [[ -n "$filename" ]]; then
        basename "${filename#Location\:}"
        return
    fi

    return 1
}

定义了这个函数后,您可以运行:

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:

function getUriFilename() {
    header="$(curl -sI "$1" | tr -d '\r')"

    filename="$(echo "$header" | grep -o -E 'filename=.*

With this defined, you can run:

url="http://www.vim.org/scripts/download_script.php?src_id=10872"
filename="$(getUriFilename $url)"
curl -L $url -o "$filename"
)" if [[ -n "$filename" ]]; then echo "${filename#filename=}" return fi filename="$(echo "$header" | grep -o -E 'Location:.*

With this defined, you can run:


)"
    if [[ -n "$filename" ]]; then
        basename "${filename#Location\:}"
        return
    fi

    return 1
}

With this defined, you can run:

独自唱情﹋歌 2024-12-04 02:29:24

请注意,某些配置错误的网络服务器将使用“文件名”作为键来提供名称,其中 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.

樱花细雨 2024-12-04 02:29:24

我和约翰·库珀有同样的问题。我没有得到文件名,但得到了位置文件名。他的回答也有效,但有两个命令。
这个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

つ低調成傷 2024-12-04 02:29:24

使用上述 Apache Archiva 工件存储库的答案来提取最新版本的示例。卷曲返回位置行,文件名位于该行的末尾。需要删除文件名末尾的 CR。

url="http://archiva:8080/restServices/archivaServices/searchService/artifact?g=com.imgur.backup&a=snapshot-s3-util&v=LATEST"
filename=$(curl --silent -sI -u user:password $url | grep Location | awk -F\/ '{print $NF}' | sed 's/\r$//')
curl --silent -o $filename -L -u user:password $url

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.

url="http://archiva:8080/restServices/archivaServices/searchService/artifact?g=com.imgur.backup&a=snapshot-s3-util&v=LATEST"
filename=$(curl --silent -sI -u user:password $url | grep Location | awk -F\/ '{print $NF}' | sed 's/\r$//')
curl --silent -o $filename -L -u user:password $url
流星番茄 2024-12-04 02:29:24

curl 没有应用 grep 和其他 Unix-Fu 操作,而是附带了一个内置的“Write Out”选项变量[1],专门用于此类大小写,例如

$ curl -OJsL "http://www.vim.org/scripts/download_script.php?src_id=10872" -w "%{filename_effective}"
pythoncomplete.vim

[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.

$ curl -OJsL "http://www.vim.org/scripts/download_script.php?src_id=10872" -w "%{filename_effective}"
pythoncomplete.vim

[1] https://everything.curl.dev/usingcurl/verbose/writeout#available-write-out-variables

老街孤人 2024-12-04 02:29:24

使用上面提出的解决方案,我编写了这个辅助函数curl2file。

[更新]

function curl2file() {
    url=$1
    url=$(curl -o /dev/null -L --head -w '%{url_effective}' $url 2>/dev/null | tail -n1) ; curl -O $url
}

用法:

curl2file https://cloud.tsinghua.edu.cn/f/4666d28af98a4e63afb5/?dl=1

Using the solution proposed above, I wrote this helper function curl2file.

[UPDATED]

function curl2file() {
    url=$1
    url=$(curl -o /dev/null -L --head -w '%{url_effective}' $url 2>/dev/null | tail -n1) ; curl -O $url
}

Usage:

curl2file https://cloud.tsinghua.edu.cn/f/4666d28af98a4e63afb5/?dl=1
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文