The issue seems to be with how the download_url function is handling the output directory. Let's modify the script to ensure that the files are downloaded to the specified directory.
Updated Script: downloader.sh
bash
#!/bin/bash
# --- Configuration ---
MAX_PROCS=10
URL_FILE="urls3.txt"
# Use OUTPUT_DIR environment variable, or default to ./downloads
OUTPUT_DIR="${OUTPUT_DIR:-./downloads}"
COMPLETED_LOG="${OUTPUT_DIR}/completed_urls.log"
# Define the User Agent centrally
USER_AGENT="Mozilla/5.0 (Windows NT 10.0; rv:109.0) Gecko/20100101 Firefox/115.0"
# ---------------------
# Ensure the output directory exists
mkdir -p "$OUTPUT_DIR"
# Export variables so they are available to backgrounded functions
export OUTPUT_DIR
export USER_AGENT
export COMPLETED_LOG
# Function to handle the download logic for a single URL
download_url() {
local url="$1"
local output_dir="$OUTPUT_DIR"
local user_agent="$USER_AGENT"
local completed_log="$COMPLETED_LOG"
# 1. Determine the filename curl *would* use.
local filename
filename=$(curl -A "$user_agent" -sS --fail -OJ --write-out '%{filename_effective}' --output /dev/null "$url")
# Check if filename discovery failed
if [ $? -ne 0 ] || [ -z "$filename" ]; then
echo "ERROR: Failed to determine filename or connect to URL: $url" >&2
return 1
fi
local full_path="$output_dir/$filename"
local resume_flag=""
local status_msg=""
# 2. Check if the file exists in the output directory
if [ -f "$full_path" ]; then
# If it exists, set the curl resume flag (-C -)
status_msg="RESUMING: $filename <- $url"
resume_flag="-C -"
else
status_msg="STARTING: $filename <- $url"
# resume_flag remains empty
fi
echo "$status_msg"
# 3. Execute the download (or resume)
# Use -o to specify the full path for the output file
curl -A "$user_agent" -S -s --fail $resume_flag -o "$full_path" "$url"
# 4. Log success or failure
if [ $? -eq 0 ]; then
echo "FINISHED: $filename"
# Log the completed URL to the persistent log
echo "$url" >> "$completed_log"
else
echo "ERROR: Download failed for $url (File: $filename)" >&2
# Optional: Clean up partial files if the resume failed mid-download
# rm -f "$full_path"
fi
}
# Export the function so background processes can execute it
export -f download_url
# --- Main Loop ---
echo "Starting downloads. Output directory: $OUTPUT_DIR"
echo "Tracking completed URLs in: $COMPLETED_LOG"
# Initialize the completed log if it doesn't exist
touch "$COMPLETED_LOG"
# Read URLs from the file, ignoring empty lines
grep -v '^[[:space:]]*$' "$URL_FILE" | while read -r u; do
# Skip this URL if it's already in the completed log
if grep -q -F "$u" "$COMPLETED_LOG"; then
echo "SKIPPED: $u (already downloaded)"
continue
fi
# Run the download function in the background (&)
download_url "$u" &
# Throttling mechanism: Wait if MAX_PROCS is reached
while [ "$(jobs -rp | wc -l)" -ge "$MAX_PROCS" ]; do
sleep 0.5
done
done
# Wait for all background jobs to complete before exiting the script
wait
echo "All downloads finished."
Explanation of Changes
The main change is in the download_url function where we use -o "$full_path" instead of just -o "$filename" when calling curl. This ensures that the downloaded file is saved to the specified output directory ($OUTPUT_DIR) instead of the current working directory.
Additionally, we've made sure that the OUTPUT_DIR is properly exported and used within the download_url function to construct the full_path of the output file.
Example Usage
To download files to a specific directory, you can run the script like this:
bash
OUTPUT_DIR=./my_downloads ./downloader.sh
This will download the files to the ./my_downloads directory and log completed URLs to ./my_downloads/completed_urls.log.