Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linux wget:下载的文件名_Linux_Bash_Wget - Fatal编程技术网

Linux wget:下载的文件名

Linux wget:下载的文件名,linux,bash,wget,Linux,Bash,Wget,我正在为bash编写一个脚本,我需要使用wget获取下载文件的名称,并将名称放入$string中 wget http://pics.sitename.com/images/191211/mxKL17DdgUhcr.jpg 45439 (44K) [image/jpeg] Saving to: «mxKL17DdgUhcr.jpg» 100%[=============================================================================

我正在为bash编写一个脚本,我需要使用wget获取下载文件的名称,并将名称放入$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]
例如,如果我下载下面的这个文件,我想把它的名称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]

您只需在下载之前指定文件名,并将
-O
选项设置为
wget

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

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

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

您可以像这样明确说明名称:

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

我猜你已经在变量中的某个地方有了文件的完整URL了?使用bash参数展开来去除前缀:

echo ${url##*/}

因此,您希望将文件/图像名称作为参数

试试这个

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 ;;
我想这对你有帮助

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重定向,最后还有一个
内容处置:
头来设置文件名


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

要处理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}

@Gowtham Gopalakrishnan答案的替代方案 只是

wget——服务器响应-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"
$

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

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

因此,我的解决方案是使用
grep
来缩小好的行(
--需要行缓冲
,因为
wget
的工作方式,请参见)和
sed
来提取文件名

wget--content disposition$12>&1|grep“保存到”--行缓冲的sed-r's/保存到:'(.*)/\1/'

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

非常有效。谢谢!警告:这不适用于包含重定向或动态内容的URL。参考est的答案以获得正确的解决方案。我喜欢它!但是如果有URL参数,它也不会很有效。例如
https://github.com/awslabs/aws-well-architected-labs/blob/master/Reliability/300_Testing_for_Resiliency_of_EC2_RDS_and_S3/Code/Python/server.py?raw=1
我喜欢这种方法,但不幸的是,Debian衍生品中的awk(例如Ubuntu)不支持
match
中的第三个参数。虽然并不总是完全准确,但这是正确的方法。在Ubuntu中,您可以使用:
wget--server response-q-O--https://very.long/url/here“2>&1 | grep”内容配置:“| tail-1 | awk-F”filename=“{print$2}”
实现它的现代简便方法:
wget{link}”--内容配置
虽然没有其他答案那么“聪明”,但这种方法实际上具有简单和可预测的优点
#!/bin/bash
file=$(wget $1 2>&1 | grep Saving | cut -d ' ' -f 3 | sed -e 's/[^A-Za-z0-9._-]//g')