Skip to content

Instantly share code, notes, and snippets.

@smarteist
Last active September 6, 2024 15:53
Show Gist options
  • Save smarteist/bdc2b3a9f3c74ad1b848355f6257c9d5 to your computer and use it in GitHub Desktop.
Save smarteist/bdc2b3a9f3c74ad1b848355f6257c9d5 to your computer and use it in GitHub Desktop.
bash downloader via curl
#!/bin/bash
# Define the list of URLs
urls=(
"https://example.com/file1.mkv"
"https://example.com/file2.mp3"
)
# Resumable downloader function
download_file() {
url="$1"
fileName=$(basename "$url") # Fixed the missing closing parenthesis
# Get the file size directly using curl's -I (HEAD request) and grep
totalSize=$(curl -sI "$url" | grep -i Content-Length | awk '{print $2}' | tr -d '\r')
[ -z "$totalSize" ] && totalSize="unknown"
echo "File: $fileName | Total Size: $totalSize bytes"
if [ -f "$fileName" ]; then
from=$(stat -c%s "$fileName")
echo "Resuming from byte $from..."
curl -L --progress-bar -C $from -o "$fileName" "$url"
else
curl -L --progress-bar -o "$fileName" "$url"
fi
echo "Finished: $fileName"
}
# Download each file
for url in "${urls[@]}"; do
download_file "$url"
echo
done
echo "All downloads complete."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment