Blog / Others/ Batch Download Images with wget Using a Bash Script

Batch Download Images with wget Using a Bash Script

Background and Objective

To acquire a template from a foreign website, I attempted to use the wget tool to fetch web resources. Some image paths were embedded within a JavaScript file, for example:

patterns: [
    'assets/images/layout/bg/arches.png',
    'assets/images/layout/bg/blu_stripes.png',
    // ... more paths
]

The goal is to extract the image filenames (e.g., arches.png) from this code and download them in batch.

Corrected Bash Script

The original script contained a syntax error. Here is the corrected, standard version:

#!/bin/bash
# Batch download PNG files

text="png.list"
url="http://www.example.com/assets/images/layout/bg"

download_files() {
    while read -r line
    do
        filename=$(echo "$line" | tr -d '[:space:]')
        if [ -n "$filename" ]; then
            echo "Downloading: $filename"
            wget -q "$url/$filename"
        fi
    done < "$text"
}

download_files

Script Explanation and Usage

  1. Prepare the file list: Save the image filenames (e.g., arches.png), one per line, in a text file named png.list.
  2. Update the URL: Modify the url variable in the script to the correct base path.
  3. Run the script: In the terminal, grant execute permission and run it:
    chmod +x download.sh
    ./download.sh

Extracting Filenames from a JS File

Use this command to quickly extract PNG filenames from a JavaScript snippet and create the png.list file:

grep -o "'[^']*.png'" customizer.js | sed "s/'//g" > png.list

This command finds all single-quoted strings ending with .png, removes the quotes, and writes the result to the file.

Important Notes

  • Ensure wget is installed on your system.
  • If the target site requires authentication or has anti-scraping measures, you may need to add parameters like --user-agent or --header to the wget command.
  • Respect the website's robots.txt rules and terms of service to avoid placing excessive load on the server.

Post a Comment

Your email will not be published. Required fields are marked with *.