BASH

Download Files from a List of URLs with Wget

Automate the downloading of multiple files by providing a text file containing a list of URLs, ideal for fetching assets or data in web development projects.

#!/bin/bash

URL_FILE="$1"
DOWNLOAD_DIR="${2:-.}." # Default to current directory if not specified

# Validate if the URL file exists
if [ ! -f "$URL_FILE" ]; then
    echo "Error: URL file '$URL_FILE' not found." >&2
    echo "Usage: $0 <url_list_file> [download_directory]"
    exit 1
fi

# Create the download directory if it doesn't exist
mkdir -p "$DOWNLOAD_DIR"

# Change to the download directory
cd "$DOWNLOAD_DIR" || {
    echo "Error: Could not change to directory '$DOWNLOAD_DIR'." >&2
    exit 1
}

echo "Starting downloads from '$URL_FILE' to '$DOWNLOAD_DIR'..."

# Read URLs line by line from the file
while IFS= read -r url;
do
    # Skip empty lines
    if [ -n "$url" ]; then
        echo "Downloading: $url"
        # Use wget to download, -q for quiet output, --no-check-certificate for HTTPS sometimes
        wget -q "$url"
        if [ $? -ne 0 ]; then
            echo "Warning: Failed to download $url" >&2
        fi
    fi
done < "$URL_FILE"

echo "All downloads attempted."
How it works: This script simplifies the task of downloading multiple files from a list of URLs. It requires a text file containing one URL per line and an optional target directory for downloads. The script creates the directory if it doesn't exist, then iterates through each URL in the file, using `wget` to download the content. It includes basic error reporting for failed downloads, making it useful for bulk asset fetching or data synchronization.

Need help integrating this into your project?

Our team of expert developers can help you build your custom application from scratch.

Hire DigitalCodeLabs