How to catch the filename saved from "wget" ?


PowerGod

Forum Addict!
Joined
Jun 20, 2011
Messages
4,419
Using "wget" with the parameter "--content-disposition", for example, the downloaded file will be automatically named using the headers taken from the URL.
If the same file is downloaded more then once, wget will add numbers to the filename.

So, how programatically I can catch the filename just saved from wget ?

Using the "-o" parameter to log the messages on a file, I could catch the name somehow, but those messages are not standard, and those phrases can change depending on the language of the OS...


On the internet I don't know what to search... the keywords "wget" and "filename" are used too much...
 
This stack overflow has a lot of discussion on the topic but I think
This other super user content has a better answer.
Pass -nv to wget to get a quieter output.
2>&1 at the end of wget to pipe error text to standard console.
| cut -d\" -f2 gets the right "field" (which I'm going to do some reading into.

Bash:
wget -nv  google.com 2>&1 | cut -d\" -f2

-edit-
Ha, -d\" makes the output a " delimited file which makes it easy to get the file name but feels dirty.
I think a command that extracts the region you are after is more maintainable so came up with
Bash:
 | sed -E 's/.*\"(.*)\".*/\1/'
which extracts the region in "" no matter where it is on the line (but now we have this problem)
 
Last edited:
Can't you download the file in a specific folder, then "ls" its name ?
Maybe the parameter "--content-disposition" can't allow that ?
 
Yes, you could always do that, but you're scripting I don't know of a good way to create a temporary folder reliably in bash without a chunk of work. Other languages have libraries to do that work for you, but if you're scripting you don't get that.
 
Yes, you could always do that, but you're scripting I don't know of a good way to create a temporary folder reliably in bash without a chunk of work. Other languages have libraries to do that work for you, but if you're scripting you don't get that.
Do you mean
Code:
dirname=`mktemp -d dirnameprefix.XXXXXX`
?

(mktemp is in the coreutils package, essential in debian)
 
I didn't know about mktemp, it is also in cygwin, could be useful in many ways :happy:

I think a command that extracts the region you are after is more maintainable so came up with
Bash:
 | sed -E 's/.*\"(.*)\".*/\1/'
which extracts the region in "" no matter where it is on the line (but now we have this problem)

I did exactly the same thing, but using the log file instead of standard output, I was hoping there was a more straightforward way to obtain it, instead of cutting it from other words. +o(

I'll do some experiments with all these solutions.
 
Create a temporary folder is easy, you have to create a random and unique name:
Bash:
$ uuidgen
1566320d-57e7-4eaa-a7a4-3407d469c7b

So you have to:
Code:
$ uuid=$(uuidgen)
$ mkdir /tmp/$uuid
$ #do the wget into that folder
$ #ls into that folder to get the name
 
Back
Top