BASH: Looping through an array to download multiple files via wget

Category: Software and Systems Engineering
Author: Sean O'Donnell
Wed, Jun. 28th, 2006 @ 4:07:08 (MDT)

I don't know about you, but I (sometimes) hate constantly downloading multiple files from the command-line, especially when they're large files, as I generally walk away and forget to finish downloading the rest of the files I need/want to grab.

However, a key benefit to being able to download files from the command-line, is being able to create your own Shell Script template that can be easily customized to meet your needs (arguably).

The example below will demonstrate a simple way to download multiple files, using simple (BASH) Unix Shell Scripting to iterate through an array of file names and download them using the 'wget' executable.

#!/bin/bash
####################
#
# file: wget.sh
#
# Download the OpenSuSE 10.1 (x86) Linux ISO CD's
#
# author: Sean O'Donnell <sean@seanodonnell.com>
#
####################

mirror=http://download.opensuse.org/distribution/SL-10.1/iso

file[0]=SUSE-Linux-10.1-GM-i386-CD1.iso
file[1]=SUSE-Linux-10.1-GM-i386-CD2.iso
file[2]=SUSE-Linux-10.1-GM-i386-CD3.iso
file[3]=SUSE-Linux-10.1-GM-i386-CD4.iso

for (( i = 0 ; i < ${#file[@]} ; i++ ))
do
        wget $mirror/${file[$i]}
done

unset file
unset mirror
exit

The script above is intended to download the entire OpenSuSE 10.1 (x86) ISO CD-Set, from the execution of (1) single command.

In order to execute the script, you'll need to change the permissions to allow the script to be executed by the intended user/group.

$ chmod +x wget.sh;
$ ./wget.sh

Assuming that the link in this example is still active, you should now be downloading the OpenSuSE 10.1 x86 Linux ISO CD-set.

Copyleft (<) 1998-2019 www.seanodonnell.com