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

时间:2022-03-19 16:23:44

Is it possible to get the file path with wget from bash?

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

I tried this, but it echo the file not the file path:

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

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

EDIT: I want something like this https://github.com/phoemur/wgetter#api-usage but in bash with wget.

编辑:我想要这样的东西https://github.com/phoemur/wgetter#api-usage,但在bash与wget。

3 个解决方案

#1


1  

You can obtain the file name from wget's output only:

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

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

Try it with google for example:

以谷歌为例:

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

Output:

index.html:
... content

#2


0  

A hack which will get you the file name by ensuring it's the only file in the current directory:

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

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

This should work no matter the encoding or whatever munging wget does to the file name (for example, if a URL containing %0A gets saved with a newline).

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

#3


0  

How about just specifying where to store the files?

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

# 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]

#1


1  

You can obtain the file name from wget's output only:

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

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

Try it with google for example:

以谷歌为例:

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

Output:

index.html:
... content

#2


0  

A hack which will get you the file name by ensuring it's the only file in the current directory:

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

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

This should work no matter the encoding or whatever munging wget does to the file name (for example, if a URL containing %0A gets saved with a newline).

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

#3


0  

How about just specifying where to store the files?

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

# 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]