Ah, I see the issue. The problem is in the download_url function where we're using curl. Even though we're determining the filename correctly, we need to make sure we're changing into the output directory before running curl, or specify the full path in the curl command. Here's the fixed version:
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}"
# 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"
# Define the completed URLs log file
COMPLETED_URLS_LOG="$OUTPUT_DIR/completed_urls.log"
# Create the completed URLs log if it doesn't exist
touch "$COMPLETED_URLS_LOG"
# Export variables so they are available to backgrounded functions
export OUTPUT_DIR
export USER_AGENT
export COMPLETED_URLS_LOG
# Function to check if a URL has already been downloaded
is_url_completed() {
local url="$1"
# Check if the URL exists in the completed URLs log file
grep -qxF "$url" "$COMPLETED_URLS_LOG"
return $?
}
# Function to mark a URL as completed
mark_url_completed() {
local url="$1"
# Append the URL to the completed URLs log file
echo "$url" >> "$COMPLETED_URLS_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_urls_log="$COMPLETED_URLS_LOG"
# Change to the output directory
cd "$output_dir" || {
echo "ERROR: Could not change to output directory: $output_dir" >&2
return 1
}
# 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
if [ -f "$filename" ]; then
status_msg="RESUMING: $filename <- $url"
resume_flag="-C -"
else
status_msg="STARTING: $filename <- $url"
fi
echo "$status_msg"
# 3. Execute the download (or resume)
curl -A "$user_agent" -S -s --fail $resume_flag "$url" -O
# 4. Log success or failure
if [ $? -eq 0 ]; then
echo "FINISHED: $filename"
mark_url_completed "$url"
else
echo "ERROR: Download failed for $url (File: $filename)" >&2
fi
}
# Export the functions so background processes can execute them
export -f download_url
export -f is_url_completed
export -f mark_url_completed
# --- Main Loop ---
echo "Starting downloads. Output directory: $OUTPUT_DIR"
# 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 URLs log
if is_url_completed "$u"; then
echo "SKIPPING: $u (already completed)"
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."
Key changes made:
Added cd "$output_dir" at the start of the download_url function to ensure we're in the correct directory before downloading.
Simplified the curl command to use -O instead of -o "$full_path" since we're now in the correct directory.
Modified the file existence check to look for just the filename rather than the full path since we're now in the target directory.
Added error checking for the cd command to ensure we can actually change to the output directory.
Added touch "$COMPLETED_URLS_LOG" near the start to ensure the log file exists.
This should now properly download files to your specified output directory. The script:
Changes to the output directory before starting the download
Performs all operations in that directory
Still maintains the completed URLs log in the correct location
Properly handles resume functionality
Each background process will be working in the correct directory independently, so the downloads should now appear in your specified output directory rather than the current working directory.