Get filename from a wget --content-disposition

2

Inside of a bash script I am dynamically downloading a file and so I use wget --content-disposition to ensure the file names are correct, but how would I go about retrieving the name of the file wget saved it as?

Roflmuffin

Posted 2015-10-08T10:14:59.203

Reputation: 21

Do you wanna look for filename while it is downloading or before downloading? İf you wanna see filename which server send you can type wget --spider – makgun – 2015-10-08T20:14:42.257

possible duplicate of How to wget a file with correct name when redirected?

– makgun – 2015-10-08T22:34:09.833

@makgun, I would preferably like to get the filename while it is downloading. The intent is that I will do some processing on the file once it has completed the download. – Roflmuffin – 2015-10-09T06:56:54.840

Didnt you use progress bar? It has already indicator that shows file location and its name in progress bar – makgun – 2015-10-09T06:59:44.823

Sorry I'm quite new to bash & pipes, how would I go about piping that output into another application (such as grep) where I can grep -e the contents? – Roflmuffin – 2015-10-09T07:09:55.410

Do you wanna pipe it on-the-go? Look for awk -W interactive NOT GAWK OR OTHER. I don't have time to write exact command but you can google it. Or before downloading use spider mod or use -O option to add specific filename – makgun – 2015-10-09T07:14:04.077

Answers

1

Here is a way to do it with wget and cut:

wget -nv https://upload.wikimedia.org/wikipedia/commons/5/54/Golden_Gate_Bridge_0002.jpg 2>&1 |cut -d\" -f2

Explanation, wget -nv ... prints out something like this:

2016-11-15 14:58:44 URL:https://upload.wikimedia.org/wikipedia/commons/5/54/Golden_Gate_Bridge_0002.jpg [1072554/1072554] -> "Golden_Gate_Bridge_0002.jpg.22" [1]

The -nv flag on wget just makes it "non-verbose" (See: man wget)

Since wget writes its output to STDERR we have to redirect that to STDOUT before we can extract the text; to do this we add 2>& at the end of the wget. Then to get out just the filename at the end I used cut. The -d\" is to specify that we are using " as a delimiter. The -f2 specifies that we want the second "column", i.e., the data inbetween the first and the second delimiters ".

First column: 2016-11-15 14:58:48 URL:https://upload.wikimedia.org/wikipedia/commons/5/54/Golden_Gate_Bridge_0002.jpg [1072554/1072554] -> "Golden_Gate_Bridge_0002.jpg.23`" [1]

varlogtim

Posted 2015-10-08T10:14:59.203

Reputation: 130