Downloading files with wget
- Version
- unversioned
- Review
- Draft
Summary#
wget downloads files over HTTP and HTTPS and saves them to disk by default.
Syntax#
wget https://example.com/release.tar.gz
wget -O release.tar.gz https://example.com/download
wget -c https://example.com/large.iso
wget -q -O - https://example.com/health
wget --tries=3 --timeout=10 https://example.com/file
-O names the output file and -O - writes to standard output. -c continues a partial file, -q hides progress, and --tries and --timeout bound retries and waiting.
Example#
This example needs python3 to run a temporary web server on a free loopback port.
(
set -eu
dir=$(mktemp -d)
server=
trap 'if [ -n "$server" ]; then
kill "$server" 2>/dev/null || true
wait "$server" 2>/dev/null || true
fi
rm -rf -- "$dir"' EXIT
mkdir "$dir/site"
echo 'release notes' > "$dir/site/notes.txt"
python3 -c '
import functools, http.server, sys
handler = functools.partial(http.server.SimpleHTTPRequestHandler, directory=sys.argv[1])
server = http.server.ThreadingHTTPServer(("127.0.0.1", 0), handler)
with open(sys.argv[2], "w") as output:
print(server.server_port, file=output)
server.serve_forever()
' "$dir/site" "$dir/port" 2>/dev/null &
server=$!
until [ -s "$dir/port" ]; do
kill -0 "$server"
sleep 0.1
done
read -r port < "$dir/port"
wget -q --no-proxy -O "$dir/copy.txt" "http://127.0.0.1:$port/notes.txt"
cat "$dir/copy.txt"
wget -q --no-proxy -O "$dir/none" "http://127.0.0.1:$port/none" || echo "status $?"
)
Expected output#
release notes
status 8
Common mistakes#
-O writes every download into the one named file and truncates it first, even when the request then fails. Check the exit status before using the file. Status 8 means the server answered with an error; GNU Wget prints ERROR 404: and Wget2 prints HTTP ERROR response 404 for a missing page. Do not use --no-check-certificate to get past a TLS error. Quote URLs that contain & or ?. Verify downloaded software with a checksum or signature.
Version applicability#
This entry describes GNU Wget 1.25 and Wget2 2.3. Fedora provides wget through Wget2, whose messages and some options differ.
Sources#
- Attribution
- DevLookup contributors
- License
- CC-BY-4.0