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?
Bash – Get filename from a wget –content-disposition
bashshell-scriptwget
Best Answer
Here is a way to do it with
wget
andcut
:Explanation,
wget -nv ...
prints out something like this:The
-nv
flag on wget just makes it "non-verbose" (See:man wget
)Since
wget
writes its output toSTDERR
we have to redirect that toSTDOUT
before we can extract the text; to do this we add2>&
at the end of thewget
. Then to get out just the filename at the end I usedcut
. 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]