html
When scripting automated downloads, wget's default output can clutter logs with progress bars and status messages. The standard approaches have limitations:
# Default (verbose) behavior - shows progress
wget https://example.com/file.zip
# --no-verbose still shows download completion lines
wget --no-verbose https://example.com/files*.zip
# --quiet suppresses everything including errors (bad for debugging)
wget --quiet https://example.com/missing-file.pdf
The most reliable method combines wget flags with shell redirection:
# Show only errors while suppressing all other output
wget --no-verbose --output-file=/dev/null https://example.com/file 2>&1 | grep -i "error\|failed"
# Alternative using temporary files
wget --output-file=wget.log https://example.com/file
if [[ $? -ne 0 ]]; then
cat wget.log | grep -i "error\|failed\|warning"
rm wget.log
fi
For batch processing in scripts:
#!/bin/bash
urls=(
"https://valid-site.com/file1"
"https://broken-site.com/missing"
)
for url in "${urls[@]}"; do
if ! wget --no-verbose --output-file=/dev/null "$url" 2>&1 | grep -q "error"; then
echo "Success: $url"
else
echo "ERROR downloading $url" >&2
fi
done
For production environments, consider these enhancements:
# Capture both errors and HTTP status codes
wget --server-response --spider --output-file=response.log https://example.com
http_code=$(grep "HTTP" response.log | tail -1 | awk '{print $2}')
# Timeout handling
wget --timeout=30 --tries=2 --output-file=/dev/null https://example.com 2> error.log
if [[ -s error.log ]]; then
echo "Download failed with:"
cat error.log
fi
Remember to test your error handling with different failure scenarios - DNS failures, connection timeouts, 404 errors, and SSL issues.
When automating scripts or running wget in cron jobs, we often need precise control over its output behavior. The default verbose output shows progress bars and download statistics, while --quiet suppresses everything - including crucial error messages we might want to log.
Here are three reliable approaches to achieve error-only output:
# Method 1: Combining quiet with error logging
wget --quiet --output-file=wget.log URL
[ -s wget.log ] && cat wget.log
# Method 2: Using stderr redirection
wget --output-file=/dev/null --no-verbose URL 2>&1 >/dev/null | grep -i error
# Method 3: Custom output filtering
wget --no-verbose URL 2>&1 | grep -v '^--' | grep -v '^$'
For production environments, I recommend this robust pattern that handles both logging and exit status:
#!/bin/bash
LOG_FILE="/var/log/wget_errors.log"
URL="https://example.com/large-file.zip"
if ! wget --quiet --output-file="$LOG_FILE" "$URL"; then
echo "[$(date)] Download failed:" >> "$LOG_FILE"
cat "$LOG_FILE"
exit 1
fi
Silent operation with error visibility is crucial for:
- Cron job monitoring
- CI/CD pipeline builds
- Resource-constrained environments
- Automated deployment scripts
For more complex scenarios where you need to process the output:
TMPOUT=$(mktemp)
TMERR=$(mktemp)
wget --output-file="$TMPOUT" --no-verbose URL 2>"$TMERR"
if [ $? -ne 0 ]; then
echo "ERROR:" >&2
cat "$TMERR" >&2
fi
rm "$TMPOUT" "$TMERR"