wget:下载的文件名

发布于 2024-12-21 20:43:29 字数 503 浏览 2 评论 0原文

我正在为 Bash 编写一个脚本,我需要使用 wget 获取下载文件的名称并将该名称放入 $string 中。

例如,如果我下载下面的此文件,我想将其名称 mxKL17DdgUhcr.jpg 放入 $string

wget http://pics.sitename.com/images/191211/mxKL17DdgUhcr.jpg
45439 (44K) [image/jpeg]
Saving to: «mxKL17DdgUhcr.jpg»

100%[===================================================================================================>] 45 439      --.-K/s   в 0s

2011-12-20 12:25:33 (388 MB/s) - «mxKL17DdgUhcr.jpg» saved [45439/45439]

I'm writing a script for Bash and I need to get the name of the downloaded file using wget and put the name into $string.

For example, if I downloading this file below, I want to put its name, mxKL17DdgUhcr.jpg, to $string.

wget http://pics.sitename.com/images/191211/mxKL17DdgUhcr.jpg
45439 (44K) [image/jpeg]
Saving to: «mxKL17DdgUhcr.jpg»

100%[===================================================================================================>] 45 439      --.-K/s   в 0s

2011-12-20 12:25:33 (388 MB/s) - «mxKL17DdgUhcr.jpg» saved [45439/45439]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(11

狠疯拽 2024-12-28 20:43:29
wget --server-response -q -O - "https://very.long/url/here" 2>&1 | 
  grep "Content-Disposition:" | tail -1 | 
  awk 'match($0, /filename=(.+)/, f){ print f[1] }' )

这是正确的版本,因为可能有多个 301/302 重定向,最后还有一个用于设置文件名的 Content-Disposition: 标头

根据 URL 猜测文件名并不总是正确的。

wget --server-response -q -O - "https://very.long/url/here" 2>&1 | 
  grep "Content-Disposition:" | tail -1 | 
  awk 'match($0, /filename=(.+)/, f){ print f[1] }' )

This is the correct version as there are may be several 301/302 redirects and finally a Content-Disposition: header to set the file name

Guessing file name based on URL is not always correct.

顾北清歌寒 2024-12-28 20:43:29

使用 basename 命令从 URL 中提取文件名。例如:

url=http://pics.sitename.com/images/191211/mxKL17DdgUhcr.jpg
filename=$(basename "$url")
wget "$url"

Use the basename command to extract the filename from the URL. For example:

url=http://pics.sitename.com/images/191211/mxKL17DdgUhcr.jpg
filename=$(basename "$url")
wget "$url"
丶情人眼里出诗心の 2024-12-28 20:43:29

您可以在下载前使用 -O 选项指定 wget 的文件名:

wget -O myfile.html http://www.example.com/

You can just specify the filename before downloading, with the -O option to wget:

wget -O myfile.html http://www.example.com/
葬﹪忆之殇 2024-12-28 20:43:29

正如 PizzaBeer 提到的wget 表示他要去哪里保存文件。这很重要,因为它将通过在文件名末尾添加数字来确保不会覆盖现有文件。

因此,这是我使用 grep 来缩小好行范围的解决方案(由于 wget 的工作原理,需要 --line-buffered ,请参阅 此处)和 sed 来提取文件名。

wget --content-disposition $1 2>&1 | grep "Saving to" --line-buffered | sed -r 's/Saving to: ‘(.*)’/\1/'

您可以将其存储在一个变量中,该变量将在下载结束时填充。

As PizzaBeer mentioned, wget says where he's going to save the file. And that's important because it will ensure to not overwrite existing files by adding a number at the end of the filename.

So here's my solution with grep to narrow down the good line (--line-buffered is needed because of how wget works, see here) and sed to extract the filename.

wget --content-disposition $1 2>&1 | grep "Saving to" --line-buffered | sed -r 's/Saving to: ‘(.*)’/\1/'

You can store this in a variable, which will be populated at the end of the download.

一绘本一梦想 2024-12-28 20:43:29

您可以像这样明确指定名称:

url='http://pics.sitename.com/images/191211/mxKL17DdgUhcr.jpg'
file=`basename "$url"`
wget "$url" -O "$file"

You can be explicit about the name like this:

url='http://pics.sitename.com/images/191211/mxKL17DdgUhcr.jpg'
file=`basename "$url"`
wget "$url" -O "$file"
我是有多爱你 2024-12-28 20:43:29

处理 URL 编码的文件名:

URL="http://www.example.com/ESTAD%C3%8DSTICA(2012).pdf"
BASE=$(basename ${URL})             # ESTAD%C3%8DSTICA(2012).pdf
FILE=$(printf '%b' ${BASE//%/\\x})  # ESTADÍSTICA(2012).pdf
wget ${URL}

To handle URL-encoded filenames:

URL="http://www.example.com/ESTAD%C3%8DSTICA(2012).pdf"
BASE=$(basename ${URL})             # ESTAD%C3%8DSTICA(2012).pdf
FILE=$(printf '%b' ${BASE//%/\\x})  # ESTADÍSTICA(2012).pdf
wget ${URL}
兲鉂ぱ嘚淚 2024-12-28 20:43:29
#!/bin/bash
file=$(wget $1 2>&1 | grep Saving | cut -d ' ' -f 3 | sed -e 's/[^A-Za-z0-9._-]//g')

我喜欢这个,因为 wget 已经告诉您它正在保存的文件名。 sed 删除非文件名字符,即。撇号。

#!/bin/bash
file=$(wget $1 2>&1 | grep Saving | cut -d ' ' -f 3 | sed -e 's/[^A-Za-z0-9._-]//g')

I like this because wget already tells you the filename it's saving. The sed strips non-filename characters ie. the apostrophes.

墨小沫ゞ 2024-12-28 20:43:29
~ $ URL='http://pics.sitename.com/images/191211/mxKL17DdgUhcr.jpg'
~ $ echo ${URL##*/}
mxKL17DdgUhcr.jpg
~ $ wget $URL -O ${URL##*/}
--18:34:26--  http://pics.sitename.com/images/191211/mxKL17DdgUhcr.jpg
           => `mxKL17DdgUhcr.jpg'
~ $ URL='http://pics.sitename.com/images/191211/mxKL17DdgUhcr.jpg'
~ $ echo ${URL##*/}
mxKL17DdgUhcr.jpg
~ $ wget $URL -O ${URL##*/}
--18:34:26--  http://pics.sitename.com/images/191211/mxKL17DdgUhcr.jpg
           => `mxKL17DdgUhcr.jpg'
无力看清 2024-12-28 20:43:29

@Gowtham Gopalakrishnan 答案的替代方案
很简单:

wget --server-response -q "https://very.long/url/here" 2>&1 | awk -F"filename=" '{if ($2) print $2}'

它仅输出在内容配置中设置的文件的名称。

例子

$ wget --server-response -q https://hostname/filename-that-i-liek.zip 2>&1 | awk -F"filename=" '{if ($2) print $2}'
"filename-that-i-liek.zip"

An alternative to @Gowtham Gopalakrishnan's answer
is simply:

wget --server-response -q "https://very.long/url/here" 2>&1 | awk -F"filename=" '{if ($2) print $2}'

Which just outputs the name of the file that is set in the content disposition.

Example

$ wget --server-response -q https://hostname/filename-that-i-liek.zip 2>&1 | awk -F"filename=" '{if ($2) print $2}'
"filename-that-i-liek.zip"
明媚如初 2024-12-28 20:43:29

我想您已经在变量中的某个位置拥有了该文件的完整 URL。使用 Bash 参数扩展来去除前缀:

echo ${url##*/}

I guess you already have the full URL of the file somewhere in a variable. Use Bash parameter expansion to strip the prefix:

echo ${url##*/}
诗化ㄋ丶相逢 2024-12-28 20:43:29

所以你想将文件/图像名称作为参数。

试试这个:

echo -n "Give me the name of file in http://pics.sitename.com/images/191211/ :"

read $string

sudo wget http://pics.sitename.com/images/191211/$string ;;

我想这可以帮助你

So you want to give the file / image name as a parameter.

Try this:

echo -n "Give me the name of file in http://pics.sitename.com/images/191211/ :"

read $string

sudo wget http://pics.sitename.com/images/191211/$string ;;

I think this could help you

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文