Linux wget:下载的文件名
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8574038/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
wget: downloaded file name
提问by Crazy_Bash
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
我正在为 bash 编写脚本,我需要使用 wget 获取下载文件的名称并将名称放入 $string
for example if I downloading this file below, I want to put it's name mxKL17DdgUhcr.jpg to $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]
采纳答案by dogbane
Use the basename
command to extract the filename from the url. For example:
使用该basename
命令从 url 中提取文件名。例如:
url=http://pics.sitename.com/images/191211/mxKL17DdgUhcr.jpg
filename=$(basename "$url")
wget "$url"
回答by pgl
You can just specify the filename before downloading, with the -O
option to wget
:
您可以在下载前指定文件名,并-O
选择wget
:
wget -O myfile.html http://www.example.com/
回答by holygeek
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"
回答by knittl
I guess you already have the full URL of the file somewhere in a variable? Use bash parameter expansion to strip the prefix:
我猜你已经在某个变量的某个地方拥有了文件的完整 URL?使用 bash 参数扩展去除前缀:
echo ${url##*/}
回答by kev
~ $ 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'
回答by Ashraf
So you want to give the file / image name as 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
我认为这可以帮助你
回答by est
wget --server-response -q -O - "https://very.long/url/here" 2>&1 |
grep "Content-Disposition:" | tail -1 |
awk 'match(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}
, /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
这是正确的版本,因为可能有多个 301/302 重定向,最后还有一个Content-Disposition:
用于设置文件名的标头
Guessing file name based on URL is not always correct.
根据 URL 猜测文件名并不总是正确的。
回答by Ian Mackinnon
To handle URL-encoded filenames:
要处理 URL 编码的文件名:
$ wget --server-response -q https://hostname/filename-that-i-liek.zip 2>&1 | awk -F"filename=" '{if () print }'
"filename-that-i-liek.zip"
$
回答by Francisco Guerreiro
an alternative to @Gowtham Gopalakrishnan's answer is simply
@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://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
例子
#!/bin/bash
file=$(wget 2>&1 | grep Saving | cut -d ' ' -f 3 | sed -e 's/[^A-Za-z0-9._-]//g')
回答by PizzaBeer
I like this because wget
already tells you the filename it's saving. The sed strips non-filename characters ie. the apostrophes.
我喜欢这个因为wget
已经告诉你它正在保存的文件名。sed 去除非文件名字符,即。撇号。