user3551444
user3551444

Reputation: 109

How to return file path from wget command in bash?

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

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.

Upvotes: 2

Views: 2567

Answers (3)

bsmoo
bsmoo

Reputation: 1059

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]

Upvotes: 0

hek2mgl
hek2mgl

Reputation: 158210

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

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

Upvotes: 1

l0b0
l0b0

Reputation: 58958

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

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).

Upvotes: 1

Related Questions