BASH
Batch Download Files from a List of URLs
Automate downloading multiple files from a list of URLs using `wget` in a bash script, ideal for bulk content retrieval and asset management.
#!/bin/bash
# Define the file containing the list of URLs, one URL per line
URL_LIST_FILE="./urls.txt"
# Define the destination directory for downloaded files
DOWNLOAD_DIR="./downloads"
# Create the download directory if it doesn't exist
mkdir -p "$DOWNLOAD_DIR"
# Check if the URL list file exists
if [ ! -f "$URL_LIST_FILE" ]; then
echo "Error: URL list file '$URL_LIST_FILE' not found!" >&2
exit 1
fi
echo "Starting batch download from '$URL_LIST_FILE' to '$DOWNLOAD_DIR'"
# Read each URL from the file and download it
while IFS= read -r URL || [[ -n "$URL" ]]; do
# Skip empty lines
if [ -z "$URL" ]; then
continue
fi
echo "Downloading: $URL"
# Use wget to download the file
# -P: Prefix for saving files (directory)
# -nc: No clobber, don't overwrite existing files
# --show-progress: Display progress bar (GNU wget specific, might not work on BSD wget)
# -q: Quiet (suppress output), remove this if you want full wget logs per file
wget -P "$DOWNLOAD_DIR" -nc "$URL" --show-progress
# Check wget's exit status
if [ $? -eq 0 ]; then
echo "Downloaded successfully."
else
echo "Failed to download $URL."
fi
echo "-------------------------------------"
done < "$URL_LIST_FILE"
echo "All downloads attempted. Please check the '$DOWNLOAD_DIR' directory."
exit 0
How it works: This script automates the process of downloading multiple files specified in a plain text file, where each line contains a URL. It first ensures the target download directory exists and validates the presence of the URL list file. It then iterates through each URL using a `while read` loop, employing `wget -P` to save files to the specified directory and `-nc` to prevent overwriting existing files. Error handling is included to report successful or failed downloads for each URL, making it highly useful for bulk asset fetching or data synchronization.