Mar 29, 2012
tom

Use number of downloaded file in wget as variable in bash script

Question

Can I get the number of downloaded files with wget -r and use that as a variable? I want to write a script that runs multiple wget commands in it (using -q so i can control output) and then at the end, add up the number of downloaded files and echo that to the user. Is this possible?

Asked by E Steven

Answer

To build on Jed’s suggestion and take it the rest of the way, you could do something like this:

MY_VARIABLE=$(wget -r http://foo 2>&1 | grep -c 'saved')

Or, if you wanted to go a touch more complicated, but more reliable and robust, you could pull out the download count given as the summary by wget:

MY_VARIABLE=$(wget -r -nv http://foo 2>&1 | awk '/^Downloaded:/ {print $2}')

Related posts:

  1. How do I pipe a downloaded file to standard output in bash?
  2. wget to print errors, but nothing otherwise
  3. Why is this bash command not echoing into a variable and what can I do to improve?
  4. wget doesn’t take variable http_proxy
  5. Bash script error: ./test: line 5: UID: readonly variable

Leave a comment