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?

  • 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 Oct 8 '15 at 20:14
  • possible duplicate of How to wget a file with correct name when redirected? – makgun Oct 8 '15 at 22:34
  • @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 Oct 9 '15 at 6:56
  • Didnt you use progress bar? It has already indicator that shows file location and its name in progress bar – makgun Oct 9 '15 at 6:59
  • 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 Oct 9 '15 at 7:09
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]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.