wget
GNU wget is a non-interactive downloader for HTTP, HTTPS and FTP: it resumes broken downloads, retries on its own and can mirror a whole site for offline use. It is the go-to tool for bulk downloads, cron jobs and scripts where nobody watches the terminal.
166.1 Why is it used?
- Download files that keep working after a network glitch: -c resumes, -t retries (20 by default).
- Fetch a list of URLs (-i), rate-limit (--limit-rate) and wait between requests (-w) to be polite to servers.
- Mirror a site or a docs folder with working local links (-m / -r -k -p).
- Archive HTTP traffic in WARC format; download over FTP/FTPS with the same tool.
- Linux: package wget; macOS: brew install wget; Windows: wget.exe (in PowerShell 5.1 wget is an alias of Invoke-WebRequest).
166.2 How it works
- Lab: client 192.168.186.20 (GNU Wget 1.24.5 on Alpine), srv.lab.test = 192.168.186.10 (HTTP 80, HTTPS 443 with a lab CA, stub proxy 3128, vsftpd FTP/FTPS).
- wget sends a GET, saves the body to a file named after the URL and prints progress to stderr.
- With -c it checks the local file size and sends a Range header for the rest; the server answers 206 Partial Content.
- Recursive mode parses HTML, follows links up to a depth and can rewrite them for offline use.
- Step 1
1.Client already has 200000 bytes of big.bin
- Step 2
2.wget -c sends GET /files/big.bin with Range: bytes=200000-
- Step 3
3.Server 192.168.186.10:80 replies 206 Partial Content
- Step 4
4.Content-Range: bytes 200000-524287/524288
- Step 5
5.Only the remaining 324288 bytes are appended; file complete
- Step 6
6.Without -c wget saves a new copy big.bin.1
166.3 Startup, logging and input files
- -V shows GNU Wget 1.24.5 (not busybox wget); -h lists options by group; -b runs in background and logs to wget-log.
- -o FILE writes messages to a log, -a FILE appends; -q quiet, -nv one line per file, -d debug (full request/response).
- -i list.txt downloads every URL in a file; -F -B base treats input as HTML and resolves links against the base.
- -e CMD runs any .wgetrc command (e.g. -e robots=off).
Terminal
U=http://srv.lab.testwget -V | head -n 2wget -b $U/files/big.binwget -nv $U/files/b.txtwget -d -O /dev/null $U/files/hello.txtwget -nv -i list.txtOutput
$ wget -V | head -n 2GNU Wget 1.24.5 built on linux-musl.$ wget -b $U/files/big.binContinuing in background, pid 67.Output will be written to 'wget-log'.$ wget -nv $U/files/b.txt2026-11-09 08:31:56 URL:http://srv.lab.test/files/b.txt [7/7] -> "b.txt" [1]$ wget -d -O /dev/null $U/files/hello.txtDEBUG output created by Wget 1.24.5 on linux-musl.---request begin---GET /files/hello.txt HTTP/1.1Host: srv.lab.testUser-Agent: Wget/1.24.5---request end------response begin---HTTP/1.0 200 OKServer: n166-lab/1.0Content-Length: 20---response end---$ wget -nv -i list.txt... URL:http://srv.lab.test/files/a.txt [7/7] -> "a.txt" [1]... (b.txt, c.txt)Downloaded: 3 files, 21 in 0s (687 KB/s)166.4 Network, retries and timeouts
- -w 2 waits 2 s between downloads (3 files = 4.08 s); --random-wait varies it (2.77 s).
- --limit-rate=100k: the 512 KB file took 5.15 s.
- -t 3 --waitretry=1 --retry-on-http-error=503: two 503s, third try 200 OK.
- --retry-connrefused retries a closed port too, then gives up with exit 4.
- -T 3 sets DNS, connect and read timeouts together; --read-timeout=2 failed on a 4-second page.
Terminal
time wget -nv -w 2 -i list.txttime wget -q --limit-rate=100k $U/files/big.binwget -t 3 --waitretry=1 --retry-on-http-error=503 $U/flakytime wget -T 3 -t 1 http://192.168.186.99/wget --read-timeout=2 -t 1 $U/slowOutput
$ time wget -nv -w 2 -i list.txt2026-11-09 08:32:03 URL:http://srv.lab.test/files/a.txt [7/7] -> "a.txt" [1]2026-11-09 08:32:05 URL:http://srv.lab.test/files/b.txt [7/7] -> "b.txt" [1]2026-11-09 08:32:07 URL:http://srv.lab.test/files/c.txt [7/7] -> "c.txt" [1]real 0m 4.08s$ time wget -q --limit-rate=100k $U/files/big.binreal 0m 5.15s$ wget -t 3 --waitretry=1 --retry-on-http-error=503 $U/flakyHTTP request sent, awaiting response... 503 Service UnavailableRetrying.--2026-11-09 08:32:23-- (try: 2) http://srv.lab.test/flaky--2026-11-09 08:32:24-- (try: 3) http://srv.lab.test/flakyHTTP request sent, awaiting response... 200 OK2026-11-09 08:32:24 (864 KB/s) - 'flaky' saved [11/11]$ time wget -T 3 -t 1 http://192.168.186.99/Connecting to 192.168.186.99:80... failed: Operation timed out.Giving up.real 0m 3.01s$ wget --read-timeout=2 -t 1 $U/slowHTTP request sent, awaiting response... Read error (Operation timed out) in headers.166.5 Output files and resume
- -O name sets the file name, -qO- prints to stdout, -P dir saves into a folder.
- -c resumes (206 Partial Content, "324288 (317K) remaining"); without -c you get big.bin.1.
- -N downloads only if newer (304 Not Modified = skipped); -nc never overwrites ("already there").
- --spider only checks that the file exists; -S shows server headers.
- -x creates host/path folders, -nH drops the host folder, --cut-dirs=1 drops one path level.
Terminal
wget -qO- $U/files/hello.txthead -c 200000 full.bin > big.binwget -c $U/files/big.binwget -N $U/files/hello.txtwget -nc $U/files/hello.txtwget -S --spider $U/files/big.binwget -nv -x -nH --cut-dirs=1 $U/docs/sub/c.htmlOutput
$ wget -qO- $U/files/hello.txthello from n166 lab$ wget -c $U/files/big.binHTTP request sent, awaiting response... 206 Partial ContentLength: 524288 (512K), 324288 (317K) remaining [application/octet-stream] [ skipping 150K ]...2026-11-09 08:32:39 (21.1 MB/s) - 'big.bin' saved [524288/524288]$ wget -N $U/files/hello.txtHTTP request sent, awaiting response... 304 Not ModifiedFile 'hello.txt' not modified on server. Omitting download.$ wget -nc $U/files/hello.txtFile 'hello.txt' already there; not retrieving.$ wget -S --spider $U/files/big.binSpider mode enabled. Check if remote file exists. HTTP/1.0 200 OK Server: n166-lab/1.0 Content-Length: 524288 Last-Modified: Mon, 09 Nov 2026 08:31:33 GMTLength: 524288 (512K) [application/octet-stream]Remote file exists.$ wget -nv -x -nH --cut-dirs=1 $U/docs/sub/c.html... -> "sub/c.html" [1]| Option | Behaviour when the file already exists |
|---|---|
| (none) | Saves a new copy: big.bin.1 |
| -c | Resumes with Range, appends the rest |
| -N | Downloads only if the server copy is newer (304 = skip) |
| -nc | Never downloads again ("already there") |
| --backups=3 | Keeps old copies as .1 .2 .3 |
166.6 Credentials and HTTP options
- No credentials on a Basic-auth folder: 401 and exit 6; --user / --password send them; --ask-password prompts (keeps it out of history).
- --header adds a header, -U sets User-Agent.
- --post-data sends a form, --save-cookies / --load-cookies keep a session (without the cookie: 403).
- --method=DELETE / PUT with --body-data for APIs.
- --content-disposition uses the server's file name; --max-redirect=1 on a 3-hop chain gives exit 8 (default limit 20).
Terminal
wget $U/private/secret.txt; echo exit $?wget -nv --user=lab --password=lab166 $U/private/secret.txtwget -nv --post-data='user=lab&pass=lab166' --save-cookies cookies.txt -O login.txt $U/loginwget -qO- --load-cookies cookies.txt $U/memberswget -qO- --method=PUT --body-data='qty=3' $U/api/item/42wget -nv --content-disposition 'http://srv.lab.test/download?id=7'wget --max-redirect=1 $U/r/3Output
$ wget $U/private/secret.txt; echo exit $?HTTP request sent, awaiting response... 401 Unauthorized Username/Password Authentication Failed.exit 6$ wget -nv --user=lab --password=lab166 $U/private/secret.txt2026-11-09 08:32:53 URL:http://srv.lab.test/private/secret.txt [20/20] -> "secret.txt" [1]$ wget -qO- --load-cookies cookies.txt $U/membersWelcome, member lab$ wget -qO- --method=PUT --body-data='qty=3' $U/api/item/42{"method":"PUT","item":"42","body":"qty=3"}$ wget -nv --content-disposition 'http://srv.lab.test/download?id=7'2026-11-09 08:32:46 URL:http://srv.lab.test/download?id=7 [15/15] -> "report-7.csv" [1]$ wget --max-redirect=1 $U/r/3HTTP request sent, awaiting response... 302 FoundLocation: /r/2 [following]--2026-11-09 08:33:08-- http://srv.lab.test/r/2Location: /r/1 [following]1 redirections exceeded.166.7 HTTPS (TLS) options
- Lab CA not trusted: "cannot verify ... certificate" and exit 5.
- --ca-certificate=ca.crt trusts the lab CA; --no-check-certificate only warns and verifies nothing.
- --secure-protocol=TLSv1_3 forces a version; a name not in the SAN (cdn.lab.test) still fails.
- Others: --certificate / --private-key (mTLS), --ca-directory, --crl-file, --pinnedpubkey, --hsts-file, --https-only.
Terminal
S=https://srv.lab.testwget $S/files/hello.txtwget -nv --ca-certificate=/root/ca.crt $S/files/hello.txtwget --ca-certificate=/root/ca.crt -O x.txt https://cdn.lab.test/files/hello.txtOutput
$ wget $S/files/hello.txt; echo exit $?Connecting to srv.lab.test (srv.lab.test)|192.168.186.10|:443... connected.ERROR: cannot verify srv.lab.test's certificate, issued by 'CN=n166 Lab CA': Unable to locally verify the issuer's authority.To connect to srv.lab.test insecurely, use `--no-check-certificate'.exit 5$ wget -nv --ca-certificate=/root/ca.crt $S/files/hello.txt2026-11-09 08:33:13 URL:https://srv.lab.test/files/hello.txt [20/20] -> "hello.txt" [1]$ wget --ca-certificate=/root/ca.crt -O x.txt https://cdn.lab.test/files/hello.txtERROR: no certificate subject alternative name matches requested host name 'cdn.lab.test'.166.8 WARC, FTP and FTPS
- --warc-file=site also writes site.warc.gz with warcinfo, request, response, metadata and resource records.
- FTP without a user logs in as anonymous; -S shows the FTP dialogue (password hidden as "Turtle Power!").
- --no-passive-ftp sends PORT (server connects back); quoted globs like 'note*.txt' fetch matching files.
- Wrong FTP password: "Login incorrect", exit 6.
- ftps:// does AUTH TLS on 21; with default TLS session resume this wget build crashed (exit 139); --no-ftps-resume-ssl worked.
- --ftps-clear-data-connection sends PROT C (only control encrypted); --ftps-implicit uses port 990.
Terminal
wget -nv --warc-file=site $U/docs/index.htmlzcat site.warc.gz | grep -a -E '^WARC-(Type|Target-URI)'wget -S --ftp-user=ftplab --ftp-password=ftp166 --no-passive-ftp ftp://srv.lab.test/reports/q3.txtwget -nv --ca-certificate=/root/ca.crt --no-ftps-resume-ssl ftps://srv.lab.test/pub/READMEOutput
$ zcat site.warc.gz | grep -a -E '^WARC-(Type|Target-URI)'WARC-Type: warcinfoWARC-Type: requestWARC-Target-URI: <http://srv.lab.test/docs/index.html>WARC-Type: responseWARC-Type: metadataWARC-Type: resource$ wget -S --ftp-user=ftplab --ftp-password=ftp166 --no-passive-ftp $F/reports/q3.txt220 (vsFTPd 3.0.5)--> USER ftplab331 Please specify the password.--> PASS Turtle Power!230 Login successful.--> CWD /home/ftplab/reports--> PORT 192,168,186,20,132,247200 PORT command successful. Consider using PASV.--> RETR q3.txt226 Transfer complete.$ wget -nv --ca-certificate=/root/ca.crt --no-ftps-resume-ssl $FS/pub/README2026-11-09 08:33:50 URL: ftps://srv.lab.test/pub/README [32] -> "README" [1]166.9 Recursive retrieval and accept/reject
- -r recursion, -l 2 depth 2 (d.html at depth 3 was not fetched), -np never goes to the parent.
- -p also fetches page requisites (CSS, images); -k converts links for offline use; -m = mirror (infinite depth + timestamps).
- -A pdf keeps only PDFs (HTML pages are fetched for links then deleted); -R 'png,css' skips types.
- -H allows other hosts, limited by -D domain list; --exclude-directories skips folders.
Terminal
wget -nv -r -l 2 -np -k -p $U/docs/index.htmlwget -nv -m -np $U/docs/wget -r -np -nd -A pdf $U/docs/index.htmlwget -q -r -l 1 -H -D srv.lab.test,cdn.lab.test $U/docs/index.htmlOutput
$ wget -nv -r -l 2 -np -k -p $U/docs/index.html... -> "srv.lab.test/docs/index.html" [1]... -> "srv.lab.test/docs/style.css" [1]... -> "srv.lab.test/docs/img/logo.png" [1]... (a.html, b.html, report.pdf)... -> "srv.lab.test/docs/sub/c.html" [1]Downloaded: 8 files, 581 in 0.008s (69.6 KB/s)$ wget -nv -m -np $U/docs/Downloaded: 9 files, 618 in 0s (5.15 MB/s)$ wget -r -np -nd -A pdf $P 2>&1 | grep -E 'Removing|saved'... - 'index.html.tmp' saved [279/279]Removing index.html.tmp since it should be rejected.... - 'report.pdf' saved [25/25]$ find . -type f | sort./cdn.lab.test/robots.txt./cdn.lab.test/static/app.js./srv.lab.test/index.html166.10 Environment, config files and exit codes
- http_proxy sends requests to the proxy with the full URL in the request line; no_proxy=host goes direct.
- WGETRC=file points to a custom rc file; ~/.wgetrc is read on every run; --no-config ignores rc files; -e sets one option.
- Exit codes matter in scripts because -q prints nothing.
Terminal
http_proxy=http://192.168.186.10:3128 wget -qO- $U/files/hello.txtwget -e user_agent=n166-via-e -qO- $U/echo | grep User-Agentwget -q -T 2 -t 1 http://192.168.186.99/; echo $?wget -q $U/status/404; echo $?Output
$ http_proxy=$PX wget -qO- $U/files/hello.txtstub proxy got: GET http://srv.lab.test/files/hello.txt HTTP/1.1User-Agent: Wget/1.24.5$ wget -e user_agent=n166-via-e -qO- $U/echo | grep User-AgentUser-Agent: n166-via-e$ wget -q -T 2 -t 1 http://192.168.186.99/; echo $?4$ wget -q $U/status/404; echo $?8| Exit code | Meaning | Lab example |
|---|---|---|
| 0 | Success | hello.txt downloaded |
| 1 / 2 / 3 | Generic / parse error / file I/O | - |
| 4 | Network failure | Timeout to 192.168.186.99, refused port 81 |
| 5 | SSL verification failure | HTTPS without the lab CA |
| 6 | Authentication failure | Wrong HTTP or FTP password |
| 7 | Protocol error | - |
| 8 | Server error response | 404, too many redirects |
166.11 Common mistakes
- Using --no-check-certificate as a fix: nothing is verified; use --ca-certificate.
- Forgetting -c on a re-run: you get big.bin.1 instead of a resumed file.
- Expecting -r -l 2 to fetch deeper pages: use -l 3 or -m.
- Unquoted -A '*.mp3' or FTP globs: the shell expands them first.
- Typing wget in Windows PowerShell 5.1 (alias of Invoke-WebRequest): type wget.exe.
166.12 Interview tips
- wget vs curl: wget = non-interactive downloader (20 retries, -c resume, -r/-m mirroring); curl = fine control of one request, many protocols, libcurl in apps.
- Rule of thumb: mirrors and cron downloads with wget, API calls with curl.
- Resume = Range request + 206 Partial Content with Content-Range.
- Exit codes 4 / 5 / 6 / 8 = network / SSL / auth / server error.