如何在bash中从wget命令返回文件路径?

时间:2014-05-12 11:18:28

标签: linux bash wget

是否可以从bash获取带有wget的文件路径?

我试过这个,但它回显文件而不是文件路径:

file=$(wget -qO- https://ninite.com/java/ninite.exe)
echo "$file"

编辑: 我希望像https://github.com/phoemur/wgetter#api-usage这样的东西,但是在wash的bash中。

3 个答案:

答案 0 :(得分:1)

您只能从wget的输出中获取文件名:

file=$(LANG=C wget URL 2>&1 | sed -n "s/.*- \`\(.*\)' saved.*/\1/p")
echo "$file:"
cat "$file"

以谷歌为例:

file=$(LANG=C wget google.de 2>&1 | sed -n "s/.*- \`\(.*\)' saved.*/\1/p")
echo "$file:"
cat "$file"

输出:

index.html:
... content

答案 1 :(得分:0)

通过确保它是当前目录中唯一的文件来获取文件名的hack:

mkdir foo
cd foo
wget http://example.org
for file in *
do
    path="$file"
done

无论编码或文件名wget如何改变(例如,如果包含%0A的网址使用换行符保存),这都应该有效。

答案 2 :(得分:0)

如何指定存储文件的位置?

# wget -P /root/test example.com
--2014-05-12 09:52:58--  http://example.com/
Resolving example.com (example.com)... 93.184.216.119, 2606:2800:220:6d:26bf:1447:1097:aa7
Connecting to example.com (example.com)|93.184.216.119|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 1270 (1.2K) [text/html]
Saving to: `/root/test/index.html'

100%[===========================================================================================================>] 1,270       --.-K/s   in 0s      

2014-05-12 09:52:58 (95.5 MB/s) - `/root/test/index.html' saved [1270/1270]