Welcome to bytebang » The blog about all and nothing » Download multiple files with similar names within bash

Download multiple files with similar names within bash

Dec 15 2015

The Problem

Assume you have a web server with interesting *.pdf files on it and all files are stored in the same directory. If you want to download them you would have to fire up firefox (or whatever browser you are using) and enter the URL  for the document once.

  1. http://my.server.tld/some/directory/Document_01.pdf
  2. http://my.server.tld/some/directory/Document_02.pdf
  3. http://my.server.tld/some/directory/Document_03.pdf
  4. ...

This is annoying and time consuming. Fortunately there is a tool called wget that can be used together with bash to automate this task:

The Solution

Beside many other functions, wget allows the user to download files from a webserver. If you want to download the first file from our example then you have to issue the following command: wget http://my.server.tld/some/directory/Document_01.pdf

While this is already easing the pain (because you can do it from the commandline in a headless system) you still have to issue the command n times to get n documents. This can also be automated - as long as the documents have the same name, but another 'index'.

$ for i in {01..30};do wget "http://my.server.tld/some/directory/Document_$i.pdf"; done

This will run the command wget 30 times and it will download all files from Document_01.pdf, Document_02.pdf .... Document_30.pdf.

Happy scripting !

Get Social

(c) 2024, by bytebang e.U. - Impressum - Datenschutz / Nutzungsbedingungen