Bash – Get filename from a wget –content-disposition

bashshell-scriptwget

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?

Best Answer

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]

Related Question