wget:下载的文件名
我正在为 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
这是正确的版本,因为可能有多个 301/302 重定向,最后还有一个用于设置文件名的
Content-Disposition:
标头根据 URL 猜测文件名并不总是正确的。
This is the correct version as there are may be several 301/302 redirects and finally a
Content-Disposition:
header to set the file nameGuessing file name based on URL is not always correct.
使用
basename
命令从 URL 中提取文件名。例如:Use the
basename
command to extract the filename from the URL. For example:您可以在下载前使用
-O
选项指定wget
的文件名:You can just specify the filename before downloading, with the
-O
option towget
:正如 PizzaBeer 提到的,
wget
表示他要去哪里保存文件。这很重要,因为它将通过在文件名末尾添加数字来确保不会覆盖现有文件。因此,这是我使用
grep
来缩小好行范围的解决方案(由于wget
的工作原理,需要--line-buffered
,请参阅 此处)和 sed 来提取文件名。您可以将其存储在一个变量中,该变量将在下载结束时填充。
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 howwget
works, see here) andsed
to extract the filename.You can store this in a variable, which will be populated at the end of the download.
您可以像这样明确指定名称:
You can be explicit about the name like this:
处理 URL 编码的文件名:
To handle URL-encoded filenames:
我喜欢这个,因为
wget
已经告诉您它正在保存的文件名。 sed 删除非文件名字符,即。撇号。I like this because
wget
already tells you the filename it's saving. The sed strips non-filename characters ie. the apostrophes.@Gowtham Gopalakrishnan 答案的替代方案
很简单:
wget --server-response -q "https://very.long/url/here" 2>&1 | awk -F"filename=" '{if ($2) print $2}'
它仅输出在内容配置中设置的文件的名称。
例子
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
我想您已经在变量中的某个位置拥有了该文件的完整 URL。使用 Bash 参数扩展来去除前缀:
I guess you already have the full URL of the file somewhere in a variable. Use Bash parameter expansion to strip the prefix:
所以你想将文件/图像名称作为参数。
试试这个:
我想这可以帮助你
So you want to give the file / image name as a parameter.
Try this:
I think this could help you