wget downloads files by URL and by default saves them to disk under the name from the address. It can resume interrupted downloads, follow links recursively and follows redirects on its own. For a one-off "download this file" it's simpler than curl; for API requests with methods and headers curl is handier.
Syntax
wget [options] URL [URL…]
The work log (Connecting…, 200 OK, progress) goes to stderr, the body — to the file.
Options
Where to save
| Option | What it does |
|---|
-O file | save to this file; -O - — to stdout |
-P directory | save into a directory (the current one by default) |
-nc | don't download if the file already exists (otherwise you get name.1) |
-N | download only if the file on the server is newer than the local one |
-c | resume a partially downloaded file (the server must support Range) |
--content-disposition | take the file name from the Content-Disposition header |
What to output
| Option | What it does |
|---|
-q | quiet: no log |
-nv | brief: one line per file |
-S | show the server's response headers |
-o file / -a file | write the log to a file / append to a file |
--spider | download nothing, just check that the URL exists |
The request
| Option | What it does |
|---|
--header 'Name: value' | add a header; can be repeated |
-U string | a custom User-Agent |
--user u --password p | HTTP authentication (Basic/Digest) |
--post-data 'a=1&b=2' / --post-file file | send a POST |
--no-check-certificate | don't verify the TLS certificate |
--max-redirect=N | at most N redirects (0 — don't follow them) |
-T sec | timeout for connecting and reading |
-t N | number of attempts (20 by default; 0 — infinite) |
--limit-rate=200k | limit the speed |
-i file | take the list of URLs from a file (one per line) |
Recursive download
| Option | What it does |
|---|
-r | recursively follow links from HTML |
-l N | recursion depth (5 by default) |
-np | don't ascend above the starting directory |
-nd | don't create directories — all files into one folder |
-nH | don't create a directory named after the host |
-A patterns / -R patterns | download only / don't download files by suffix or pattern: -A 'pdf,txt' |
-k | rewrite links in downloaded HTML to local ones |
-p | also download everything needed to display the page (images, CSS) |
-m | mirror: same as -r -N -l inf --no-remove-listing |
Exit codes
| Code | Meaning |
|---|
0 | all good |
1 | generic error |
2 | option parsing error |
3 | file write error |
4 | network error: couldn't connect, timeout, name doesn't resolve |
5 | TLS certificate verification failed |
6 | authentication error (401) |
7 | protocol error |
8 | the server responded with an error: 404, 500… |
wget or curl
| Task | wget | curl |
|---|
| save a file | by default | needs -O or -o |
| follow redirects | by default | needs -L |
| HTTP error → non-zero code | by default (8) | needs -f |
| download a site recursively | -r | can't |
| PUT/DELETE methods, arbitrary body, JSON APIs | awkward | -X, -d, -H |
Examples
# download a file under its own name
wget http://localhost:8080/backup.tar.gz
# into a specific file, quietly
wget -q -O /tmp/page.html http://localhost:8080/
# to stdout — for a pipeline
wget -qO- http://localhost:8080/api/status | jq .
# only the response headers, discard the body
wget -S -q -O /dev/null http://localhost:8080/
# check whether the URL exists (code 0) or not (code 8)
wget -q --spider http://localhost:8080/health && echo ok
# resume an interrupted download
wget -c http://localhost:8080/big.iso
# several files from a list, into the downloads directory
wget -q -i urls.txt -P downloads
# download all .txt files the page links to, without a directory tree
wget -r -l 1 -nd -A txt http://localhost:8080/index.html
# a quick timeout and a single attempt — for scripts
wget -q -T 5 -t 1 http://10.0.0.5/ || echo "code $?"
# Basic authentication and a custom header
wget -qO- --user admin --password secret --header 'Accept: application/json' http://localhost:8080/private/
Pitfalls
-O creates the file even on error. wget -O out.html http://…/nope gets a 404 and leaves an empty out.html. Check the exit code, not whether the file exists.
- Running it again doesn't overwrite the file, it creates
name.1, name.2… To overwrite, use -O name; to avoid re-downloading — -nc or -N.
-c requires Range support on the server. If the server doesn't support it (e.g. python3 -m http.server), wget downloads the whole file again.
-O with several URLs concatenates them into one file.
-t defaults to 20 attempts. "Connection refused" and 404 aren't retried, but timeouts and dropped connections are — up to 20 times, so wget against an unreachable address can hang for minutes. In scripts, set -T and -t 1.
--spider still connects to the server — it's not an offline check; with -r it walks the links but doesn't save files.
- The log goes to stderr.
wget … | grep on the log won't work: add 2>&1 first.
-A still downloads HTML with -r to find links in it, and deletes it afterwards.
See also
curl · htpasswd · nginx · ab · python3